Skip to content

Commit ecc3b26

Browse files
gregryorkbbakerman
authored andcommitted
Add context to the TypeResolutionEnvironment (graphql-java#795)
1 parent 8d5ff1f commit ecc3b26

File tree

5 files changed

+66
-6
lines changed

5 files changed

+66
-6
lines changed

src/main/java/graphql/TypeResolutionEnvironment.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ public class TypeResolutionEnvironment {
1818
private final Field field;
1919
private final GraphQLType fieldType;
2020
private final GraphQLSchema schema;
21+
private final Object context;
2122

22-
public TypeResolutionEnvironment(Object object, Map<String, Object> arguments, Field field, GraphQLType fieldType, GraphQLSchema schema) {
23+
public TypeResolutionEnvironment(Object object, Map<String, Object> arguments, Field field, GraphQLType fieldType, GraphQLSchema schema, final Object context) {
2324
this.object = object;
2425
this.arguments = arguments;
2526
this.field = field;
2627
this.fieldType = fieldType;
2728
this.schema = schema;
29+
this.context = context;
2830
}
2931

3032
/**
@@ -67,4 +69,8 @@ public GraphQLType getFieldType() {
6769
public GraphQLSchema getSchema() {
6870
return schema;
6971
}
72+
73+
public <T> T getContext() {
74+
return (T) context;
75+
}
7076
}

src/main/java/graphql/execution/ExecutionStrategy.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ protected CompletableFuture<ExecutionResult> completeValue(ExecutionContext exec
349349
.field(parameters.field().get(0))
350350
.value(parameters.source())
351351
.argumentValues(parameters.arguments())
352+
.context(executionContext.getContext())
352353
.schema(executionContext.getGraphQLSchema()).build();
353354
resolvedType = resolveTypeForInterface(resolutionParams);
354355

@@ -358,6 +359,7 @@ protected CompletableFuture<ExecutionResult> completeValue(ExecutionContext exec
358359
.field(parameters.field().get(0))
359360
.value(parameters.source())
360361
.argumentValues(parameters.arguments())
362+
.context(executionContext.getContext())
361363
.schema(executionContext.getGraphQLSchema()).build();
362364
resolvedType = resolveTypeForUnion(resolutionParams);
363365
} else {
@@ -435,7 +437,7 @@ protected Iterable<Object> toIterable(Object result) {
435437
* @return a {@link GraphQLObjectType}
436438
*/
437439
protected GraphQLObjectType resolveTypeForInterface(TypeResolutionParameters params) {
438-
TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLInterfaceType(), params.getSchema());
440+
TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLInterfaceType(), params.getSchema(), params.getContext());
439441
GraphQLObjectType result = params.getGraphQLInterfaceType().getTypeResolver().getType(env);
440442
if (result == null) {
441443
throw new UnresolvedTypeException(params.getGraphQLInterfaceType());
@@ -451,7 +453,7 @@ protected GraphQLObjectType resolveTypeForInterface(TypeResolutionParameters par
451453
* @return a {@link GraphQLObjectType}
452454
*/
453455
protected GraphQLObjectType resolveTypeForUnion(TypeResolutionParameters params) {
454-
TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLUnionType(), params.getSchema());
456+
TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLUnionType(), params.getSchema(), params.getContext());
455457
GraphQLObjectType result = params.getGraphQLUnionType().getTypeResolver().getType(env);
456458
if (result == null) {
457459
throw new UnresolvedTypeException(params.getGraphQLUnionType());

src/main/java/graphql/execution/TypeResolutionParameters.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ public class TypeResolutionParameters {
1515
private final Object value;
1616
private final Map<String, Object> argumentValues;
1717
private final GraphQLSchema schema;
18+
private final Object context;
1819

1920
private TypeResolutionParameters(GraphQLInterfaceType graphQLInterfaceType, GraphQLUnionType graphQLUnionType,
20-
Field field, Object value, Map<String, Object> argumentValues, GraphQLSchema schema) {
21+
Field field, Object value, Map<String, Object> argumentValues, GraphQLSchema schema, final Object context) {
2122
this.graphQLInterfaceType = graphQLInterfaceType;
2223
this.graphQLUnionType = graphQLUnionType;
2324
this.field = field;
2425
this.value = value;
2526
this.argumentValues = argumentValues;
2627
this.schema = schema;
28+
this.context = context;
2729
}
2830

2931
public GraphQLInterfaceType getGraphQLInterfaceType() {
@@ -54,6 +56,10 @@ public static Builder newParameters() {
5456
return new Builder();
5557
}
5658

59+
public Object getContext() {
60+
return context;
61+
}
62+
5763
public static class Builder {
5864

5965
private Field field;
@@ -62,6 +68,7 @@ public static class Builder {
6268
private Object value;
6369
private Map<String, Object> argumentValues;
6470
private GraphQLSchema schema;
71+
private Object context;
6572

6673
public Builder field(Field field) {
6774
this.field = field;
@@ -93,8 +100,13 @@ public Builder schema(GraphQLSchema schema) {
93100
return this;
94101
}
95102

103+
public Builder context(Object context) {
104+
this.context = context;
105+
return this;
106+
}
107+
96108
public TypeResolutionParameters build() {
97-
return new TypeResolutionParameters(graphQLInterfaceType, graphQLUnionType, field, value, argumentValues, schema);
109+
return new TypeResolutionParameters(graphQLInterfaceType, graphQLUnionType, field, value, argumentValues, schema, context);
98110
}
99111
}
100112
}

src/main/java/graphql/execution/batched/BatchedExecutionStrategy.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ private GraphQLObjectType getGraphQLObjectType(ExecutionContext executionContext
416416
.field(field)
417417
.value(value)
418418
.argumentValues(argumentValues)
419+
.context(executionContext.getContext())
419420
.schema(executionContext.getGraphQLSchema())
420421
.build());
421422
} else if (fieldType instanceof GraphQLUnionType) {
@@ -424,6 +425,7 @@ private GraphQLObjectType getGraphQLObjectType(ExecutionContext executionContext
424425
.field(field)
425426
.value(value)
426427
.argumentValues(argumentValues)
428+
.context(executionContext.getContext())
427429
.schema(executionContext.getGraphQLSchema())
428430
.build());
429431
} else if (fieldType instanceof GraphQLObjectType) {

src/test/groovy/graphql/TypeResolutionEnvironmentTest.groovy

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@ class TypeResolutionEnvironmentTest extends Specification {
2626
foo : String
2727
bar : String
2828
}
29+
30+
type FooImpl implements Foo {
31+
foo : String
32+
}
2933
"""
3034

3135
def schema = TestUtil.schema(idl)
3236

3337
def "basic operations"() {
3438
given:
3539

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

3842
when:
3943

@@ -67,4 +71,38 @@ class TypeResolutionEnvironmentTest extends Specification {
6771
thrown(GraphQLException)
6872

6973
}
74+
75+
def "using context"() {
76+
given:
77+
78+
def resolverWithContext = new TypeResolver() {
79+
@Override
80+
GraphQLObjectType getType(TypeResolutionEnvironment env) {
81+
String source = env.getObject()
82+
assert source == "source"
83+
if ("FooBar" == env.getContext()) {
84+
return schema.getObjectType("FooBar")
85+
}
86+
if ("Foo" == env.getContext()) {
87+
return schema.getObjectType("FooImpl")
88+
}
89+
return null
90+
}
91+
}
92+
93+
when:
94+
def environmentFooBar = new TypeResolutionEnvironment("source", [:], new Field("field"), Scalars.GraphQLString, schema, "FooBar")
95+
def objTypeFooBar = resolverWithContext.getType(environmentFooBar)
96+
97+
then:
98+
objTypeFooBar.name == "FooBar"
99+
100+
when:
101+
def environmentFooImpl = new TypeResolutionEnvironment("source", [:], new Field("field"), Scalars.GraphQLString, schema, "Foo")
102+
def objTypeFooImpl = resolverWithContext.getType(environmentFooImpl)
103+
104+
then:
105+
objTypeFooImpl.name == "FooImpl"
106+
107+
}
70108
}

0 commit comments

Comments
 (0)