Skip to content

Commit e64f128

Browse files
committed
Add fragment definitions and execution id to DataFetchingEnvironment (fixes graphql-java#303).
1 parent 7bd01a5 commit e64f128

File tree

7 files changed

+54
-12
lines changed

7 files changed

+54
-12
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,10 @@ protected ExecutionResult resolveField(ExecutionContext executionContext, Execut
6969
DataFetchingEnvironment environment = new DataFetchingEnvironmentImpl(
7070
parameters.source(),
7171
argumentValues,
72-
executionContext.getRoot(),
7372
fields,
7473
fieldDef.getType(),
7574
type,
76-
executionContext.getGraphQLSchema()
75+
executionContext
7776
);
7877

7978
Instrumentation instrumentation = executionContext.getInstrumentation();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,10 @@ private List<GraphQLExecutionNodeValue> fetchData(ExecutionContext executionCont
255255
DataFetchingEnvironment environment = new DataFetchingEnvironmentImpl(
256256
sources,
257257
argumentValues,
258-
executionContext.getRoot(),
259258
fields,
260259
fieldDef.getType(),
261260
parentType,
262-
executionContext.getGraphQLSchema()
261+
executionContext
263262
);
264263

265264
List<Object> values;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public Object get(DataFetchingEnvironment environment) {
3535
environment.getFields(),
3636
environment.getFieldType(),
3737
environment.getParentType(),
38-
environment.getGraphQLSchema());
38+
environment.getGraphQLSchema(),
39+
environment.getFragmentsByName(),
40+
environment.getExecutionId());
3941
results.add(delegate.get(singleEnv));
4042
}
4143
return results;

src/main/java/graphql/schema/DataFetchingEnvironment.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package graphql.schema;
22

3+
import graphql.execution.ExecutionId;
34
import graphql.language.Field;
5+
import graphql.language.FragmentDefinition;
46

57
import java.util.List;
68
import java.util.Map;
@@ -69,4 +71,14 @@ public interface DataFetchingEnvironment {
6971
* @return the underlying graphql schema
7072
*/
7173
GraphQLSchema getGraphQLSchema();
74+
75+
/**
76+
* @return the {@link FragmentDefinition} map for the current operation
77+
*/
78+
Map<String, FragmentDefinition> getFragmentsByName();
79+
80+
/**
81+
* @return the {@link ExecutionId} for the current operation
82+
*/
83+
ExecutionId getExecutionId();
7284
}

src/main/java/graphql/schema/DataFetchingEnvironmentImpl.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package graphql.schema;
22

33

4+
import graphql.execution.ExecutionContext;
5+
import graphql.execution.ExecutionId;
46
import graphql.language.Field;
7+
import graphql.language.FragmentDefinition;
58

69
import java.util.List;
710
import java.util.Map;
@@ -15,15 +18,23 @@ public class DataFetchingEnvironmentImpl implements DataFetchingEnvironment {
1518
private final GraphQLOutputType fieldType;
1619
private final GraphQLType parentType;
1720
private final GraphQLSchema graphQLSchema;
21+
private Map<String, FragmentDefinition> fragmentsByName;
22+
private ExecutionId executionId;
1823

19-
public DataFetchingEnvironmentImpl(Object source, Map<String, Object> arguments, Object context, List<Field> fields, GraphQLOutputType fieldType, GraphQLType parentType, GraphQLSchema graphQLSchema) {
24+
public DataFetchingEnvironmentImpl(Object source, Map<String, Object> arguments, List<Field> fields, GraphQLOutputType fieldType, GraphQLType parentType, ExecutionContext executionContext) {
25+
this(source, arguments, executionContext.getRoot(), fields, fieldType, parentType, executionContext.getGraphQLSchema(), executionContext.getFragmentsByName(), executionContext.getExecutionId());
26+
}
27+
28+
public DataFetchingEnvironmentImpl(Object source, Map<String, Object> arguments, Object context, List<Field> fields, GraphQLOutputType fieldType, GraphQLType parentType, GraphQLSchema graphQLSchema, Map<String, FragmentDefinition> fragmentsByName, ExecutionId executionId) {
2029
this.source = source;
2130
this.arguments = arguments;
2231
this.context = context;
2332
this.fields = fields;
2433
this.fieldType = fieldType;
2534
this.parentType = parentType;
2635
this.graphQLSchema = graphQLSchema;
36+
this.fragmentsByName = fragmentsByName;
37+
this.executionId = executionId;
2738
}
2839

2940
@Override
@@ -70,4 +81,14 @@ public GraphQLType getParentType() {
7081
public GraphQLSchema getGraphQLSchema() {
7182
return graphQLSchema;
7283
}
84+
85+
@Override
86+
public Map<String, FragmentDefinition> getFragmentsByName() {
87+
return fragmentsByName;
88+
}
89+
90+
@Override
91+
public ExecutionId getExecutionId() {
92+
return executionId;
93+
}
7394
}

src/test/groovy/graphql/DataFetcherTest.groovy

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package graphql
22

33
import graphql.schema.DataFetchingEnvironmentImpl
44
import graphql.schema.FieldDataFetcher
5+
import graphql.schema.GraphQLOutputType
6+
import graphql.schema.GraphQLScalarType
57
import graphql.schema.PropertyDataFetcher
68
import spock.lang.Specification
79

@@ -53,9 +55,13 @@ class DataFetcherTest extends Specification {
5355
dataHolder.setBooleanFieldWithGet(false)
5456
}
5557

58+
private def env(GraphQLOutputType type) {
59+
new DataFetchingEnvironmentImpl(dataHolder, null, null, null, type, null, null, null, null)
60+
}
61+
5662
def "get field value"() {
5763
given:
58-
def environment = new DataFetchingEnvironmentImpl(dataHolder, null, null, null, GraphQLString, null, null)
64+
def environment = env(GraphQLString)
5965
when:
6066
def result = new FieldDataFetcher("publicField").get(environment)
6167
then:
@@ -64,7 +70,7 @@ class DataFetcherTest extends Specification {
6470

6571
def "get property value"() {
6672
given:
67-
def environment = new DataFetchingEnvironmentImpl(dataHolder, null, null, null, GraphQLString, null, null)
73+
def environment = env(GraphQLString)
6874
when:
6975
def result = new PropertyDataFetcher("property").get(environment)
7076
then:
@@ -73,7 +79,7 @@ class DataFetcherTest extends Specification {
7379

7480
def "get Boolean property value"() {
7581
given:
76-
def environment = new DataFetchingEnvironmentImpl(dataHolder, null, null, null, GraphQLBoolean, null, null)
82+
def environment = env(GraphQLBoolean)
7783
when:
7884
def result = new PropertyDataFetcher("booleanField").get(environment)
7985
then:
@@ -82,7 +88,7 @@ class DataFetcherTest extends Specification {
8288

8389
def "get Boolean property value with get"() {
8490
given:
85-
def environment = new DataFetchingEnvironmentImpl(dataHolder, null, null, null, GraphQLBoolean, null, null)
91+
def environment = env(GraphQLBoolean)
8692
when:
8793
def result = new PropertyDataFetcher("booleanFieldWithGet").get(environment)
8894
then:
@@ -91,7 +97,7 @@ class DataFetcherTest extends Specification {
9197

9298
def "get field value as property"() {
9399
given:
94-
def environment = new DataFetchingEnvironmentImpl(dataHolder, null, null, null, GraphQLString, null, null)
100+
def environment = env(GraphQLString)
95101
when:
96102
def result = new PropertyDataFetcher("publicField").get(environment)
97103
then:

src/test/groovy/graphql/schema/PropertyDataFetcherTest.groovy

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import spock.lang.Specification
77
class PropertyDataFetcherTest extends Specification {
88

99
def env(obj) {
10-
return new DataFetchingEnvironmentImpl(obj,
10+
return new DataFetchingEnvironmentImpl(
11+
obj,
1112
Collections.emptyMap(),
1213
null,
1314
Collections.emptyList(),
1415
null,
1516
null,
17+
null,
18+
null,
1619
null
1720
)
1821
}

0 commit comments

Comments
 (0)