diff --git a/spring-graphql-docs/src/docs/asciidoc/index.adoc b/spring-graphql-docs/src/docs/asciidoc/index.adoc index e9023ab0..9b376295 100644 --- a/spring-graphql-docs/src/docs/asciidoc/index.adoc +++ b/spring-graphql-docs/src/docs/asciidoc/index.adoc @@ -553,6 +553,10 @@ See <>. | For access to the context from the `DataFetchingEnvironment`. See <>. +| `DataFetchingFieldSelectionSet` +| For access to the selection set for the query through the `DataFetchingEnvironment`. +See <>. + | `DataFetchingEnvironment` | For direct access to the underlying `DataFetchingEnvironment`. See <>. @@ -682,6 +686,14 @@ To access the `GraphQLContext` from the `DataFetchingEnvironment`, declare a met parameter of the same type. +[[controllers-schema-mapping-selection-set]] +==== `DataFetchingFieldSelectionSet` + +To access the `DataFetchingFieldSelectionSet` from the `DataFetchingEnvironment`, declare +a method parameter of the same type. This is useful to peek ahead at the fields that need +to be fetched. + + [[controllers-schema-mapping-environment]] ==== `DataFetchingEnvironment` diff --git a/spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/DataFetchingEnvironmentMethodArgumentResolver.java b/spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/DataFetchingEnvironmentMethodArgumentResolver.java index 7d1193e8..5df9cb2a 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/DataFetchingEnvironmentMethodArgumentResolver.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/DataFetchingEnvironmentMethodArgumentResolver.java @@ -17,12 +17,14 @@ package org.springframework.graphql.data.method.annotation.support; import graphql.GraphQLContext; import graphql.schema.DataFetchingEnvironment; +import graphql.schema.DataFetchingFieldSelectionSet; import org.springframework.core.MethodParameter; import org.springframework.graphql.data.method.HandlerMethodArgumentResolver; /** - * Resolver for {@link DataFetchingEnvironment} or {@link GraphQLContext} arguments. + * Resolver for {@link DataFetchingEnvironment} as well as arguments of type + * {@link GraphQLContext} or {@link DataFetchingFieldSelectionSet}. * * @author Rossen Stoyanchev * @since 1.0.0 @@ -32,7 +34,8 @@ public class DataFetchingEnvironmentMethodArgumentResolver implements HandlerMet @Override public boolean supportsParameter(MethodParameter parameter) { Class type = parameter.getParameterType(); - return (type.equals(DataFetchingEnvironment.class) || type.equals(GraphQLContext.class)); + return (type.equals(DataFetchingEnvironment.class) || type.equals(GraphQLContext.class) || + type.equals(DataFetchingFieldSelectionSet.class)); } @Override @@ -41,9 +44,15 @@ public class DataFetchingEnvironmentMethodArgumentResolver implements HandlerMet if (type.equals(GraphQLContext.class)) { return environment.getGraphQlContext(); } - else { + else if (type.equals(DataFetchingFieldSelectionSet.class)) { + return environment.getSelectionSet(); + } + else if (type.equals(DataFetchingEnvironment.class)) { return environment; } + else { + throw new IllegalStateException("Unexpected method parameter type: " + parameter); + } } }