Auto-register Querydsl DataFetcher's only for top-level queries

Closes gh-93
This commit is contained in:
Rossen Stoyanchev
2021-07-20 13:42:26 +01:00
parent c148eb40b4
commit 4078799e34
3 changed files with 20 additions and 6 deletions

View File

@@ -40,10 +40,12 @@ import org.springframework.graphql.execution.GraphQlSource;
/**
* {@link EnableAutoConfiguration Auto-configuration} that creates a
* {@link GraphQlSourceBuilderCustomizer}s to detect Spring Data repositories
* with Querydsl support and register them as {@code DataFetcher}s.
* with Querydsl support and register them as {@code DataFetcher}s for any
* queries with a matching return type.
*
* @author Rossen Stoyanchev
* @since 1.0.0
* @see QuerydslDataFetcher#registrationTypeVisitor(List, List)
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)

View File

@@ -40,10 +40,12 @@ import org.springframework.graphql.execution.GraphQlSource;
/**
* {@link EnableAutoConfiguration Auto-configuration} that creates a
* {@link GraphQlSourceBuilderCustomizer}s to detect Spring Data repositories
* with Querydsl support and register them as {@code DataFetcher}s.
* with Querydsl support and register them as {@code DataFetcher}s for any
* queries with a matching return type.
*
* @author Rossen Stoyanchev
* @since 1.0.0
* @see QuerydslDataFetcher#registrationTypeVisitor(List, List)
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)

View File

@@ -161,9 +161,11 @@ public abstract class QuerydslDataFetcher<T> {
}
/**
* Create a {@link GraphQLTypeVisitor} that finds fields whose type matches
* the domain type of the the given repositories and registers them as
* {@link DataFetcher}s.
* Create a {@link GraphQLTypeVisitor} that finds queries with a return type
* whose name matches to the domain type name of the given repositories and
* registers {@link DataFetcher}s for those queries.
* <p><strong>Note:</strong> currently, this method will match only to
* queries under the top-level "Query" type in the GraphQL schema.
* @param executors repositories to consider for registration
* @param reactiveExecutors reactive repositories to consider for registration
* @return the created visitor
@@ -529,13 +531,21 @@ public abstract class QuerydslDataFetcher<T> {
public TraversalControl visitGraphQLFieldDefinition(
GraphQLFieldDefinition fieldDefinition, TraverserContext<GraphQLSchemaElement> context) {
if (this.executorMap.isEmpty()) {
return TraversalControl.QUIT;
}
GraphQLType fieldType = fieldDefinition.getType();
GraphQLFieldsContainer parent = (GraphQLFieldsContainer) context.getParentNode();
if (!parent.getName().equals("Query")) {
return TraversalControl.ABORT;
}
DataFetcher<?> dataFetcher = (fieldType instanceof GraphQLList ?
getDataFetcher(((GraphQLList) fieldType).getWrappedType(), false) :
getDataFetcher(fieldType, true));
if (dataFetcher != null) {
GraphQLFieldsContainer parent = (GraphQLFieldsContainer) context.getParentNode();
GraphQLCodeRegistry.Builder registry = context.getVarFromParents(GraphQLCodeRegistry.Builder.class);
if (!hasDataFetcher(registry, parent, fieldDefinition)) {
registry.dataFetcher(parent, fieldDefinition, dataFetcher);