Skip to content

Commit ed94288

Browse files
authored
Add additional type entity filter to schema builder api (#522)
* Add additional type entity filter to schema builder api * Apply additional type from schema import selector
1 parent 85e79a2 commit ed94288

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaBuilderAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ GraphQLJpaSchemaBuilder defaultGraphQLJpaSchemaBuilder(
126126
.toManyDefaultOptional(properties.isToManyDefaultOptional())
127127
.enableRelay(properties.isEnableRelay());
128128

129-
EnableGraphQLJpaQuerySchemaImportSelector.getPackageNames().stream().forEach(builder::entityPath);
129+
EnableGraphQLJpaQuerySchemaImportSelector.getPackageNames().stream().forEach(builder::additionalType);
130130

131131
restrictedKeysProvider.ifAvailable(builder::restrictedKeysProvider);
132132

schema/src/main/java/com/introproventures/graphql/jpa/query/schema/GraphQLSchemaBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public interface GraphQLSchemaBuilder {
5050
*/
5151
GraphQLSchemaBuilder entityPath(String path);
5252

53+
/**
54+
* Add package path to scan for additional entities to be included in GraphQL Schema.
55+
*
56+
* @param type GraphQL entitys package path
57+
* @return this builder instance
58+
*/
59+
GraphQLSchemaBuilder additionalType(String type);
60+
5361
/**
5462
* Add package path to scan for entities to be included in GraphQL Schema.
5563
*

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ public class GraphQLJpaSchemaBuilder implements GraphQLSchemaBuilder {
162162
private final Relay relay = new Relay();
163163

164164
private final List<String> entityPaths = new ArrayList<>();
165+
private final List<String> additionalTypes = new ArrayList<>();
165166

166167
private final Supplier<BatchLoaderRegistry> batchLoadersRegistry = BatchLoaderRegistry::getInstance;
167168
private final Function<String, EntityType<?>> entityObjectTypeResolver = entityTypeMap::get;
@@ -190,6 +191,7 @@ public GraphQLSchema build() {
190191
.getEntities()
191192
.stream()
192193
.filter(Predicate.not(entityCache::containsKey))
194+
.filter(this::isAdditionalType)
193195
.forEach(entity -> schema.additionalType(getEntityObjectType(entity)));
194196

195197
if (enableSubscription) {
@@ -1715,6 +1717,12 @@ protected boolean isNotIgnoredOrder(Attribute<?, ?> attribute) {
17151717
return false;
17161718
}
17171719

1720+
private boolean isAdditionalType(EntityType<?> entityType) {
1721+
return additionalTypes
1722+
.stream()
1723+
.anyMatch(additionalType -> entityType.getJavaType().getName().startsWith(additionalType));
1724+
}
1725+
17181726
@SuppressWarnings("unchecked")
17191727
private GraphQLOutputType getGraphQLTypeFromJavaType(Class<?> clazz) {
17201728
if (clazz.isEnum()) {
@@ -1887,6 +1895,15 @@ public GraphQLJpaSchemaBuilder entityPath(String path) {
18871895
return this;
18881896
}
18891897

1898+
@Override
1899+
public GraphQLJpaSchemaBuilder additionalType(String type) {
1900+
Assert.assertNotNull(type, () -> "type can't be null");
1901+
1902+
additionalTypes.add(type);
1903+
1904+
return this;
1905+
}
1906+
18901907
public GraphQLJpaSchemaBuilder queryByIdFieldNameCustomizer(Function<String, String> queryByIdFieldNameCustomizer) {
18911908
this.queryByIdFieldNameCustomizer = queryByIdFieldNameCustomizer;
18921909

0 commit comments

Comments
 (0)