Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/main/java/graphql/TypeResolutionEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import graphql.collect.ImmutableMapWithNullValues;
import graphql.execution.MergedField;
import graphql.schema.DataFetchingFieldSelectionSet;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLType;

Expand All @@ -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
*
Expand Down Expand Up @@ -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() {
Copy link
Member Author

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

return (T) context;
}

/**
* @return the {@link GraphQLContext} object set in via {@link ExecutionInput#getGraphQLContext()}
*/
public GraphQLContext getGraphQLContext() {
return graphQLContext;
}
Copy link
Member Author

Choose a reason for hiding this comment

The 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() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new capability

return fieldSelectionSet;
}
}
2 changes: 1 addition & 1 deletion src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ protected Iterable<Object> toIterable(Object result) {
}

protected GraphQLObjectType resolveType(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLType fieldType) {
return resolvedType.resolveType(executionContext, parameters.getField(), parameters.getSource(), parameters.getExecutionStepInfo().getArguments(), fieldType);
return resolvedType.resolveType(executionContext, parameters.getField(), parameters.getSource(), parameters.getExecutionStepInfo(), fieldType);
}


Expand Down
45 changes: 28 additions & 17 deletions src/main/java/graphql/execution/ResolveType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member Author

Choose a reason for hiding this comment

The 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);
}
Copy link
Member Author

Choose a reason for hiding this comment

The 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

Expand Down
17 changes: 15 additions & 2 deletions src/main/java/graphql/execution/TypeResolutionParameters.java
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
Copy link
Member Author

Choose a reason for hiding this comment

The 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 graphql.execution.ResolveType and that is an internal class

public class TypeResolutionParameters {

private final GraphQLInterfaceType graphQLInterfaceType;
Expand All @@ -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;
Expand All @@ -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() {
Expand All @@ -56,6 +59,10 @@ public GraphQLSchema getSchema() {
return schema;
}

public DataFetchingFieldSelectionSet getSelectionSet() {
return selectionSet;
}

public static Builder newParameters() {
return new Builder();
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public FieldSubSelection createFieldSubSelection(ExecutionContext executionConte
Object localContext = resolvedValue.getLocalContext();

GraphQLOutputType sourceType = executionInfo.getUnwrappedNonNullType();
GraphQLObjectType resolvedObjectType = resolveType.resolveType(executionContext, field, source, executionInfo.getArguments(), sourceType);
GraphQLObjectType resolvedObjectType = resolveType.resolveType(executionContext, field, source, executionInfo, sourceType);
FieldCollectorParameters collectorParameters = newParameters()
.schema(executionContext.getGraphQLSchema())
.objectType(resolvedObjectType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private FetchedValueAnalysis analyzeFetchedValueImpl(ExecutionContext executionC
.build();
}
try {
GraphQLObjectType resolvedObjectType = resolveType.resolveType(executionContext, field, toAnalyze, executionInfo.getArguments(), fieldType);
GraphQLObjectType resolvedObjectType = resolveType.resolveType(executionContext, field, toAnalyze, executionInfo, fieldType);
return newFetchedValueAnalysis(OBJECT)
.fetchedValue(fetchedValue)
.executionStepInfo(executionInfo)
Expand Down
9 changes: 6 additions & 3 deletions src/test/groovy/graphql/TypeResolutionEnvironmentTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ class TypeResolutionEnvironmentTest extends Specification {

def schema = TestUtil.schema(idl)

def graphqlContext = GraphQLContext.newContext().of("a", "b").build()

def "basic operations"() {
given:

def environment = new TypeResolutionEnvironment("source", [:], mergedField(new Field("field")), Scalars.GraphQLString, schema, "FooBar")
def environment = new TypeResolutionEnvironment("source", [:], mergedField(new Field("field")), Scalars.GraphQLString, schema, "FooBar", graphqlContext, null)

when:

Expand Down Expand Up @@ -82,6 +84,7 @@ class TypeResolutionEnvironmentTest extends Specification {
GraphQLObjectType getType(TypeResolutionEnvironment env) {
String source = env.getObject()
assert source == "source"
assert env.getGraphQLContext().get("a") == "b"
if ("FooBar" == env.getContext()) {
return schema.getObjectType("FooBar")
}
Expand All @@ -93,14 +96,14 @@ class TypeResolutionEnvironmentTest extends Specification {
}

when:
def environmentFooBar = new TypeResolutionEnvironment("source", [:], mergedField(new Field("field")), Scalars.GraphQLString, schema, "FooBar")
def environmentFooBar = new TypeResolutionEnvironment("source", [:], mergedField(new Field("field")), Scalars.GraphQLString, schema, "FooBar", graphqlContext, null)
def objTypeFooBar = resolverWithContext.getType(environmentFooBar)

then:
objTypeFooBar.name == "FooBar"

when:
def environmentFooImpl = new TypeResolutionEnvironment("source", [:], mergedField(new Field("field")), Scalars.GraphQLString, schema, "Foo")
def environmentFooImpl = new TypeResolutionEnvironment("source", [:], mergedField(new Field("field")), Scalars.GraphQLString, schema, "Foo", graphqlContext, null)
def objTypeFooImpl = resolverWithContext.getType(environmentFooImpl)

then:
Expand Down
Loading