Skip to content

Add additional type entity filter to schema builder api #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ GraphQLJpaSchemaBuilder defaultGraphQLJpaSchemaBuilder(
.toManyDefaultOptional(properties.isToManyDefaultOptional())
.enableRelay(properties.isEnableRelay());

EnableGraphQLJpaQuerySchemaImportSelector.getPackageNames().stream().forEach(builder::entityPath);
EnableGraphQLJpaQuerySchemaImportSelector.getPackageNames().stream().forEach(builder::additionalType);

restrictedKeysProvider.ifAvailable(builder::restrictedKeysProvider);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public interface GraphQLSchemaBuilder {
*/
GraphQLSchemaBuilder entityPath(String path);

/**
* Add package path to scan for additional entities to be included in GraphQL Schema.
*
* @param type GraphQL entitys package path
* @return this builder instance
*/
GraphQLSchemaBuilder additionalType(String type);

/**
* Add package path to scan for entities to be included in GraphQL Schema.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
private final Relay relay = new Relay();

private final List<String> entityPaths = new ArrayList<>();
private final List<String> additionalTypes = new ArrayList<>();

private final Supplier<BatchLoaderRegistry> batchLoadersRegistry = BatchLoaderRegistry::getInstance;
private final Function<String, EntityType<?>> entityObjectTypeResolver = entityTypeMap::get;
Expand Down Expand Up @@ -190,6 +191,7 @@
.getEntities()
.stream()
.filter(Predicate.not(entityCache::containsKey))
.filter(this::isAdditionalType)
.forEach(entity -> schema.additionalType(getEntityObjectType(entity)));

if (enableSubscription) {
Expand Down Expand Up @@ -1715,6 +1717,12 @@
return false;
}

private boolean isAdditionalType(EntityType<?> entityType) {
return additionalTypes
.stream()
.anyMatch(additionalType -> entityType.getJavaType().getName().startsWith(additionalType));
}

@SuppressWarnings("unchecked")
private GraphQLOutputType getGraphQLTypeFromJavaType(Class<?> clazz) {
if (clazz.isEnum()) {
Expand Down Expand Up @@ -1887,6 +1895,15 @@
return this;
}

@Override
public GraphQLJpaSchemaBuilder additionalType(String type) {
Assert.assertNotNull(type, () -> "type can't be null");

Check warning on line 1900 in schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java

View check run for this annotation

Codecov / codecov/patch

schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java#L1900

Added line #L1900 was not covered by tests

additionalTypes.add(type);

Check warning on line 1902 in schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java

View check run for this annotation

Codecov / codecov/patch

schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java#L1902

Added line #L1902 was not covered by tests

return this;

Check warning on line 1904 in schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java

View check run for this annotation

Codecov / codecov/patch

schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java#L1904

Added line #L1904 was not covered by tests
}

public GraphQLJpaSchemaBuilder queryByIdFieldNameCustomizer(Function<String, String> queryByIdFieldNameCustomizer) {
this.queryByIdFieldNameCustomizer = queryByIdFieldNameCustomizer;

Expand Down