-
Notifications
You must be signed in to change notification settings - Fork 1.2k
This adds the field selection set to the type resolver #2597
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import graphql.collect.ImmutableMapWithNullValues; | ||
| import graphql.execution.MergedField; | ||
| import graphql.schema.DataFetchingFieldSelectionSet; | ||
| import graphql.schema.GraphQLSchema; | ||
| import graphql.schema.GraphQLType; | ||
|
|
||
|
|
@@ -22,18 +23,30 @@ public class TypeResolutionEnvironment { | |
| private final GraphQLType fieldType; | ||
| private final GraphQLSchema schema; | ||
| private final Object context; | ||
| private final GraphQLContext graphQLContext; | ||
| private final DataFetchingFieldSelectionSet fieldSelectionSet; | ||
|
|
||
| public TypeResolutionEnvironment(Object object, Map<String, Object> arguments, MergedField field, GraphQLType fieldType, GraphQLSchema schema, final Object context) { | ||
| public TypeResolutionEnvironment(Object object, | ||
| Map<String, Object> arguments, | ||
| MergedField field, | ||
| GraphQLType fieldType, | ||
| GraphQLSchema schema, | ||
| Object context, | ||
| GraphQLContext graphQLContext, | ||
| DataFetchingFieldSelectionSet fieldSelectionSet) { | ||
| this.object = object; | ||
| this.arguments = ImmutableMapWithNullValues.copyOf(arguments); | ||
| this.field = field; | ||
| this.fieldType = fieldType; | ||
| this.schema = schema; | ||
| this.context = context; | ||
| this.graphQLContext = graphQLContext; | ||
| this.fieldSelectionSet = fieldSelectionSet; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * You will be passed the specific source object that needs to be resolve into a concrete graphql object type | ||
| * You will be passed the specific source object that needs to be resolved into a concrete graphql object type | ||
| * | ||
| * @param <T> you decide what type it is | ||
| * | ||
|
|
@@ -73,7 +86,31 @@ public GraphQLSchema getSchema() { | |
| return schema; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the context object set in via {@link ExecutionInput#getContext()} | ||
| * | ||
| * @param <T> to two | ||
| * | ||
| * @return the context object | ||
| * | ||
| * @deprecated use {@link #getGraphQLContext()} instead | ||
| */ | ||
| @Deprecated | ||
| public <T> T getContext() { | ||
| return (T) context; | ||
| } | ||
|
|
||
| /** | ||
| * @return the {@link GraphQLContext} object set in via {@link ExecutionInput#getGraphQLContext()} | ||
| */ | ||
| public GraphQLContext getGraphQLContext() { | ||
| return graphQLContext; | ||
| } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was missed as being a replacement for the above |
||
|
|
||
| /** | ||
| * @return the {@link DataFetchingFieldSelectionSet} for the current field fetch that needs type resolution | ||
| */ | ||
| public DataFetchingFieldSelectionSet getSelectionSet() { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the new capability |
||
| return fieldSelectionSet; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,69 +2,80 @@ | |
|
|
||
| import graphql.Internal; | ||
| import graphql.TypeResolutionEnvironment; | ||
| import graphql.normalized.ExecutableNormalizedField; | ||
| import graphql.normalized.ExecutableNormalizedOperation; | ||
| import graphql.schema.DataFetchingFieldSelectionSet; | ||
| import graphql.schema.DataFetchingFieldSelectionSetImpl; | ||
| import graphql.schema.GraphQLInterfaceType; | ||
| import graphql.schema.GraphQLNamedOutputType; | ||
| import graphql.schema.GraphQLObjectType; | ||
| import graphql.schema.GraphQLOutputType; | ||
| import graphql.schema.GraphQLType; | ||
| import graphql.schema.GraphQLUnionType; | ||
| import graphql.schema.TypeResolver; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.function.Supplier; | ||
|
|
||
| @Internal | ||
| public class ResolveType { | ||
|
|
||
|
|
||
| public GraphQLObjectType resolveType(ExecutionContext executionContext, MergedField field, Object source, Map<String, Object> arguments, GraphQLType fieldType) { | ||
| public GraphQLObjectType resolveType(ExecutionContext executionContext, MergedField field, Object source, ExecutionStepInfo executionStepInfo, GraphQLType fieldType) { | ||
| GraphQLObjectType resolvedType; | ||
| if (fieldType instanceof GraphQLInterfaceType) { | ||
| DataFetchingFieldSelectionSet fieldSelectionSet = buildSelectionSet(executionContext, field, (GraphQLOutputType) fieldType, executionStepInfo); | ||
| TypeResolutionParameters resolutionParams = TypeResolutionParameters.newParameters() | ||
| .graphQLInterfaceType((GraphQLInterfaceType) fieldType) | ||
| .field(field) | ||
| .value(source) | ||
| .argumentValues(arguments) | ||
| .argumentValues(executionStepInfo.getArguments()) | ||
| .selectionSet(fieldSelectionSet) | ||
| .context(executionContext.getContext()) | ||
| .graphQLContext(executionContext.getGraphQLContext()) | ||
| .schema(executionContext.getGraphQLSchema()).build(); | ||
| resolvedType = resolveTypeForInterface(resolutionParams); | ||
|
|
||
| } else if (fieldType instanceof GraphQLUnionType) { | ||
| DataFetchingFieldSelectionSet selectionSet = buildSelectionSet(executionContext, field, (GraphQLOutputType) fieldType, executionStepInfo); | ||
| TypeResolutionParameters resolutionParams = TypeResolutionParameters.newParameters() | ||
| .graphQLUnionType((GraphQLUnionType) fieldType) | ||
| .field(field) | ||
| .value(source) | ||
| .argumentValues(arguments) | ||
| .argumentValues(executionStepInfo.getArguments()) | ||
| .selectionSet(selectionSet) | ||
| .context(executionContext.getContext()) | ||
| .graphQLContext(executionContext.getGraphQLContext()) | ||
| .schema(executionContext.getGraphQLSchema()).build(); | ||
| resolvedType = resolveTypeForUnion(resolutionParams); | ||
| } else { | ||
| resolvedType = (GraphQLObjectType) fieldType; | ||
| } | ||
|
|
||
| return resolvedType; | ||
| } | ||
|
|
||
| private DataFetchingFieldSelectionSet buildSelectionSet(ExecutionContext executionContext, MergedField field, GraphQLOutputType fieldType, ExecutionStepInfo executionStepInfo) { | ||
| Supplier<ExecutableNormalizedOperation> normalizedQuery = executionContext.getNormalizedQueryTree(); | ||
| Supplier<ExecutableNormalizedField> normalizedFieldSupplier = () -> normalizedQuery.get().getNormalizedField(field, executionStepInfo.getObjectType(), executionStepInfo.getPath()); | ||
| return DataFetchingFieldSelectionSetImpl.newCollector(executionContext.getGraphQLSchema(), fieldType, normalizedFieldSupplier); | ||
| } | ||
|
|
||
| public GraphQLObjectType resolveTypeForInterface(TypeResolutionParameters params) { | ||
| TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLInterfaceType(), params.getSchema(), params.getContext()); | ||
| TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLInterfaceType(), params.getSchema(), params.getContext(), params.getGraphQLContext(), params.getSelectionSet()); | ||
| GraphQLInterfaceType abstractType = params.getGraphQLInterfaceType(); | ||
| TypeResolver typeResolver = params.getSchema().getCodeRegistry().getTypeResolver(abstractType); | ||
| GraphQLObjectType result = typeResolver.getType(env); | ||
| if (result == null) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I refactored this a little because the same code was repeated in each |
||
| throw new UnresolvedTypeException(abstractType); | ||
| } | ||
|
|
||
| if (!params.getSchema().isPossibleType(abstractType, result)) { | ||
| throw new UnresolvedTypeException(abstractType, result); | ||
| } | ||
|
|
||
| return result; | ||
| return resolveAbstractType(params, env, typeResolver, abstractType); | ||
| } | ||
|
|
||
| public GraphQLObjectType resolveTypeForUnion(TypeResolutionParameters params) { | ||
| TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLUnionType(), params.getSchema(), params.getContext()); | ||
| TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLUnionType(), params.getSchema(), params.getContext(), params.getGraphQLContext(), params.getSelectionSet()); | ||
| GraphQLUnionType abstractType = params.getGraphQLUnionType(); | ||
| TypeResolver typeResolver = params.getSchema().getCodeRegistry().getTypeResolver(abstractType); | ||
| return resolveAbstractType(params, env, typeResolver, abstractType); | ||
| } | ||
|
|
||
| private GraphQLObjectType resolveAbstractType(TypeResolutionParameters params, TypeResolutionEnvironment env, TypeResolver typeResolver, GraphQLNamedOutputType abstractType) { | ||
| GraphQLObjectType result = typeResolver.getType(env); | ||
|
|
||
| if (result == null) { | ||
| throw new UnresolvedTypeException(abstractType); | ||
| } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hard to see but the common red code above is now here in a common method |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,16 @@ | ||
| package graphql.execution; | ||
|
|
||
| import graphql.GraphQLContext; | ||
| import graphql.PublicApi; | ||
| import graphql.Internal; | ||
| import graphql.collect.ImmutableMapWithNullValues; | ||
| import graphql.schema.DataFetchingFieldSelectionSet; | ||
| import graphql.schema.GraphQLInterfaceType; | ||
| import graphql.schema.GraphQLSchema; | ||
| import graphql.schema.GraphQLUnionType; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| @PublicApi | ||
| @Internal | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should NEVER have been public API Is never visible to any one - there is no way for a consumer to get this class It was ONLY ever used in |
||
| public class TypeResolutionParameters { | ||
|
|
||
| private final GraphQLInterfaceType graphQLInterfaceType; | ||
|
|
@@ -20,6 +21,7 @@ public class TypeResolutionParameters { | |
| private final GraphQLSchema schema; | ||
| private final Object context; | ||
| private final GraphQLContext graphQLContext; | ||
| private final DataFetchingFieldSelectionSet selectionSet; | ||
|
|
||
| private TypeResolutionParameters(Builder builder) { | ||
| this.graphQLInterfaceType = builder.graphQLInterfaceType; | ||
|
|
@@ -30,6 +32,7 @@ private TypeResolutionParameters(Builder builder) { | |
| this.schema = builder.schema; | ||
| this.context = builder.context; | ||
| this.graphQLContext = builder.graphQLContext; | ||
| this.selectionSet = builder.selectionSet; | ||
| } | ||
|
|
||
| public GraphQLInterfaceType getGraphQLInterfaceType() { | ||
|
|
@@ -56,6 +59,10 @@ public GraphQLSchema getSchema() { | |
| return schema; | ||
| } | ||
|
|
||
| public DataFetchingFieldSelectionSet getSelectionSet() { | ||
| return selectionSet; | ||
| } | ||
|
|
||
| public static Builder newParameters() { | ||
| return new Builder(); | ||
| } | ||
|
|
@@ -84,6 +91,7 @@ public static class Builder { | |
| private GraphQLSchema schema; | ||
| private Object context; | ||
| private GraphQLContext graphQLContext; | ||
| private DataFetchingFieldSelectionSet selectionSet; | ||
|
|
||
| public Builder field(MergedField field) { | ||
| this.field = field; | ||
|
|
@@ -126,6 +134,11 @@ public Builder graphQLContext(GraphQLContext context) { | |
| return this; | ||
| } | ||
|
|
||
| public Builder selectionSet(DataFetchingFieldSelectionSet selectionSet) { | ||
| this.selectionSet = selectionSet; | ||
| return this; | ||
| } | ||
|
|
||
| public TypeResolutionParameters build() { | ||
| return new TypeResolutionParameters(this); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have been deprecated when we did the context work. Its is now like all the others