Skip to content

Commit c38596a

Browse files
authored
Merge pull request graphql-java#392 from apottere/expose-fragments
Add fragment definitions and execution id to DataFetchingEnvironment
2 parents 205557e + e64f128 commit c38596a

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
@@ -70,11 +70,10 @@ protected ExecutionResult resolveField(ExecutionContext executionContext, Execut
7070
DataFetchingEnvironmentenvironment = newDataFetchingEnvironmentImpl(
7171
parameters.source(),
7272
argumentValues,
73-
executionContext.getRoot(),
7473
fields,
7574
fieldDef.getType(),
7675
type,
77-
executionContext.getGraphQLSchema()
76+
executionContext
7877
);
7978

8079
Instrumentationinstrumentation = 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
DataFetchingEnvironmentenvironment = newDataFetchingEnvironmentImpl(
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
returnresults;

‎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
packagegraphql.schema;
22

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

57
importjava.util.List;
68
importjava.util.Map;
@@ -69,4 +71,14 @@ public interface DataFetchingEnvironment{
6971
* @return the underlying graphql schema
7072
*/
7173
GraphQLSchemagetGraphQLSchema();
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+
ExecutionIdgetExecutionId();
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
packagegraphql.schema;
22

33

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

69
importjava.util.List;
710
importjava.util.Map;
@@ -15,15 +18,23 @@ public class DataFetchingEnvironmentImpl implements DataFetchingEnvironment{
1518
privatefinalGraphQLOutputTypefieldType;
1619
privatefinalGraphQLTypeparentType;
1720
privatefinalGraphQLSchemagraphQLSchema;
21+
privateMap<String, FragmentDefinition> fragmentsByName;
22+
privateExecutionIdexecutionId;
1823

19-
publicDataFetchingEnvironmentImpl(Objectsource, Map<String, Object> arguments, Objectcontext, List<Field> fields, GraphQLOutputTypefieldType, GraphQLTypeparentType, GraphQLSchemagraphQLSchema){
24+
publicDataFetchingEnvironmentImpl(Objectsource, Map<String, Object> arguments, List<Field> fields, GraphQLOutputTypefieldType, GraphQLTypeparentType, ExecutionContextexecutionContext){
25+
this(source, arguments, executionContext.getRoot(), fields, fieldType, parentType, executionContext.getGraphQLSchema(), executionContext.getFragmentsByName(), executionContext.getExecutionId());
26+
}
27+
28+
publicDataFetchingEnvironmentImpl(Objectsource, Map<String, Object> arguments, Objectcontext, List<Field> fields, GraphQLOutputTypefieldType, GraphQLTypeparentType, GraphQLSchemagraphQLSchema, Map<String, FragmentDefinition> fragmentsByName, ExecutionIdexecutionId){
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
publicGraphQLSchemagetGraphQLSchema(){
7182
returngraphQLSchema;
7283
}
84+
85+
@Override
86+
publicMap<String, FragmentDefinition> getFragmentsByName(){
87+
returnfragmentsByName;
88+
}
89+
90+
@Override
91+
publicExecutionIdgetExecutionId(){
92+
returnexecutionId;
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
importgraphql.schema.DataFetchingEnvironmentImpl
44
importgraphql.schema.FieldDataFetcher
5+
importgraphql.schema.GraphQLOutputType
6+
importgraphql.schema.GraphQLScalarType
57
importgraphql.schema.PropertyDataFetcher
68
importspock.lang.Specification
79

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

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

6571
def"get property value"(){
6672
given:
67-
def environment =newDataFetchingEnvironmentImpl(dataHolder, null, null, null, GraphQLString, null, null)
73+
def environment =env(GraphQLString)
6874
when:
6975
def result =newPropertyDataFetcher("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 =newDataFetchingEnvironmentImpl(dataHolder, null, null, null, GraphQLBoolean, null, null)
82+
def environment =env(GraphQLBoolean)
7783
when:
7884
def result =newPropertyDataFetcher("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 =newDataFetchingEnvironmentImpl(dataHolder, null, null, null, GraphQLBoolean, null, null)
91+
def environment =env(GraphQLBoolean)
8692
when:
8793
def result =newPropertyDataFetcher("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 =newDataFetchingEnvironmentImpl(dataHolder, null, null, null, GraphQLString, null, null)
100+
def environment =env(GraphQLString)
95101
when:
96102
def result =newPropertyDataFetcher("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
classPropertyDataFetcherTestextendsSpecification{
88

99
defenv(obj){
10-
returnnewDataFetchingEnvironmentImpl(obj,
10+
returnnewDataFetchingEnvironmentImpl(
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)