Support DataFetchingFieldSelectionSet as an argument

This commit is contained in:
Rossen Stoyanchev
2021-10-15 17:19:22 +01:00
parent 28b3d13b0b
commit cedc6ede3d
2 changed files with 24 additions and 3 deletions

View File

@@ -553,6 +553,10 @@ See <<controllers-schema-mapping-data-loader>>.
| For access to the context from the `DataFetchingEnvironment`.
See <<controllers-schema-mapping-graphql-context>>.
| `DataFetchingFieldSelectionSet`
| For access to the selection set for the query through the `DataFetchingEnvironment`.
See <<controllers-schema-mapping-selection-set>>.
| `DataFetchingEnvironment`
| For direct access to the underlying `DataFetchingEnvironment`.
See <<controllers-schema-mapping-environment>>.
@@ -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`

View File

@@ -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);
}
}
}