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
8 changes: 7 additions & 1 deletion src/main/java/graphql/TypeResolutionEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ public class TypeResolutionEnvironment {
private final Field field;
private final GraphQLType fieldType;
private final GraphQLSchema schema;
private final Object context;

public TypeResolutionEnvironment(Object object, Map<String, Object> arguments, Field field, GraphQLType fieldType, GraphQLSchema schema) {
public TypeResolutionEnvironment(Object object, Map<String, Object> arguments, Field field, GraphQLType fieldType, GraphQLSchema schema, final Object context) {
this.object = object;
this.arguments = arguments;
this.field = field;
this.fieldType = fieldType;
this.schema = schema;
this.context = context;
}

/**
Expand Down Expand Up @@ -67,4 +69,8 @@ public GraphQLType getFieldType() {
public GraphQLSchema getSchema() {
return schema;
}

public <T> T getContext() {
return (T) context;
}
}
6 changes: 4 additions & 2 deletions src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ protected CompletableFuture<ExecutionResult> completeValue(ExecutionContext exec
.field(parameters.field().get(0))
.value(parameters.source())
.argumentValues(parameters.arguments())
.context(executionContext.getContext())
.schema(executionContext.getGraphQLSchema()).build();
resolvedType = resolveTypeForInterface(resolutionParams);

Expand All @@ -358,6 +359,7 @@ protected CompletableFuture<ExecutionResult> completeValue(ExecutionContext exec
.field(parameters.field().get(0))
.value(parameters.source())
.argumentValues(parameters.arguments())
.context(executionContext.getContext())
.schema(executionContext.getGraphQLSchema()).build();
resolvedType = resolveTypeForUnion(resolutionParams);
} else {
Expand Down Expand Up @@ -435,7 +437,7 @@ protected Iterable<Object> toIterable(Object result) {
* @return a {@link GraphQLObjectType}
*/
protected GraphQLObjectType resolveTypeForInterface(TypeResolutionParameters params) {
TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLInterfaceType(), params.getSchema());
TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLInterfaceType(), params.getSchema(), params.getContext());
GraphQLObjectType result = params.getGraphQLInterfaceType().getTypeResolver().getType(env);
if (result == null) {
throw new UnresolvedTypeException(params.getGraphQLInterfaceType());
Expand All @@ -451,7 +453,7 @@ protected GraphQLObjectType resolveTypeForInterface(TypeResolutionParameters par
* @return a {@link GraphQLObjectType}
*/
protected GraphQLObjectType resolveTypeForUnion(TypeResolutionParameters params) {
TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLUnionType(), params.getSchema());
TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLUnionType(), params.getSchema(), params.getContext());
GraphQLObjectType result = params.getGraphQLUnionType().getTypeResolver().getType(env);
if (result == null) {
throw new UnresolvedTypeException(params.getGraphQLUnionType());
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/graphql/execution/TypeResolutionParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ public class TypeResolutionParameters {
private final Object value;
private final Map<String, Object> argumentValues;
private final GraphQLSchema schema;
private final Object context;

private TypeResolutionParameters(GraphQLInterfaceType graphQLInterfaceType, GraphQLUnionType graphQLUnionType,
Field field, Object value, Map<String, Object> argumentValues, GraphQLSchema schema) {
Field field, Object value, Map<String, Object> argumentValues, GraphQLSchema schema, final Object context) {
this.graphQLInterfaceType = graphQLInterfaceType;
this.graphQLUnionType = graphQLUnionType;
this.field = field;
this.value = value;
this.argumentValues = argumentValues;
this.schema = schema;
this.context = context;
}

public GraphQLInterfaceType getGraphQLInterfaceType() {
Expand Down Expand Up @@ -54,6 +56,10 @@ public static Builder newParameters() {
return new Builder();
}

public Object getContext() {
return context;
}

public static class Builder {

private Field field;
Expand All @@ -62,6 +68,7 @@ public static class Builder {
private Object value;
private Map<String, Object> argumentValues;
private GraphQLSchema schema;
private Object context;

public Builder field(Field field) {
this.field = field;
Expand Down Expand Up @@ -93,8 +100,13 @@ public Builder schema(GraphQLSchema schema) {
return this;
}

public Builder context(Object context) {
this.context = context;
return this;
}

public TypeResolutionParameters build() {
return new TypeResolutionParameters(graphQLInterfaceType, graphQLUnionType, field, value, argumentValues, schema);
return new TypeResolutionParameters(graphQLInterfaceType, graphQLUnionType, field, value, argumentValues, schema, context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ private GraphQLObjectType getGraphQLObjectType(ExecutionContext executionContext
.field(field)
.value(value)
.argumentValues(argumentValues)
.context(executionContext.getContext())
.schema(executionContext.getGraphQLSchema())
.build());
} else if (fieldType instanceof GraphQLUnionType) {
Expand All @@ -424,6 +425,7 @@ private GraphQLObjectType getGraphQLObjectType(ExecutionContext executionContext
.field(field)
.value(value)
.argumentValues(argumentValues)
.context(executionContext.getContext())
.schema(executionContext.getGraphQLSchema())
.build());
} else if (fieldType instanceof GraphQLObjectType) {
Expand Down
40 changes: 39 additions & 1 deletion src/test/groovy/graphql/TypeResolutionEnvironmentTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ class TypeResolutionEnvironmentTest extends Specification {
foo : String
bar : String
}

type FooImpl implements Foo {
foo : String
}
"""

def schema = TestUtil.schema(idl)

def "basic operations"() {
given:

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

when:

Expand Down Expand Up @@ -67,4 +71,38 @@ class TypeResolutionEnvironmentTest extends Specification {
thrown(GraphQLException)

}

def "using context"() {
given:

def resolverWithContext = new TypeResolver() {
@Override
GraphQLObjectType getType(TypeResolutionEnvironment env) {
String source = env.getObject()
assert source == "source"
if ("FooBar" == env.getContext()) {
return schema.getObjectType("FooBar")
}
if ("Foo" == env.getContext()) {
return schema.getObjectType("FooImpl")
}
return null
}
}

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

then:
objTypeFooBar.name == "FooBar"

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

then:
objTypeFooImpl.name == "FooImpl"

}
}