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
3 changes: 1 addition & 2 deletions src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,10 @@ protected ExecutionResult resolveField(ExecutionContext executionContext, Execut
DataFetchingEnvironment environment = new DataFetchingEnvironmentImpl(
parameters.source(),
argumentValues,
executionContext.getRoot(),
fields,
fieldDef.getType(),
type,
executionContext.getGraphQLSchema()
executionContext
);

Instrumentation instrumentation = executionContext.getInstrumentation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,10 @@ private List<GraphQLExecutionNodeValue> fetchData(ExecutionContext executionCont
DataFetchingEnvironment environment = new DataFetchingEnvironmentImpl(
sources,
argumentValues,
executionContext.getRoot(),
fields,
fieldDef.getType(),
parentType,
executionContext.getGraphQLSchema()
executionContext
);

List<Object> values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public Object get(DataFetchingEnvironment environment) {
environment.getFields(),
environment.getFieldType(),
environment.getParentType(),
environment.getGraphQLSchema());
environment.getGraphQLSchema(),
environment.getFragmentsByName(),
environment.getExecutionId());
results.add(delegate.get(singleEnv));
}
return results;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/graphql/schema/DataFetchingEnvironment.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package graphql.schema;

import graphql.execution.ExecutionId;
import graphql.language.Field;
import graphql.language.FragmentDefinition;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -69,4 +71,14 @@ public interface DataFetchingEnvironment {
* @return the underlying graphql schema
*/
GraphQLSchema getGraphQLSchema();

/**
* @return the {@link FragmentDefinition} map for the current operation
*/
Map<String, FragmentDefinition> getFragmentsByName();

/**
* @return the {@link ExecutionId} for the current operation
*/
ExecutionId getExecutionId();
}
23 changes: 22 additions & 1 deletion src/main/java/graphql/schema/DataFetchingEnvironmentImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package graphql.schema;


import graphql.execution.ExecutionContext;
import graphql.execution.ExecutionId;
import graphql.language.Field;
import graphql.language.FragmentDefinition;

import java.util.List;
import java.util.Map;
Expand All @@ -15,15 +18,23 @@ public class DataFetchingEnvironmentImpl implements DataFetchingEnvironment {
private final GraphQLOutputType fieldType;
private final GraphQLType parentType;
private final GraphQLSchema graphQLSchema;
private Map<String, FragmentDefinition> fragmentsByName;
private ExecutionId executionId;

public DataFetchingEnvironmentImpl(Object source, Map<String, Object> arguments, Object context, List<Field> fields, GraphQLOutputType fieldType, GraphQLType parentType, GraphQLSchema graphQLSchema) {
public DataFetchingEnvironmentImpl(Object source, Map<String, Object> arguments, List<Field> fields, GraphQLOutputType fieldType, GraphQLType parentType, ExecutionContext executionContext) {
this(source, arguments, executionContext.getRoot(), fields, fieldType, parentType, executionContext.getGraphQLSchema(), executionContext.getFragmentsByName(), executionContext.getExecutionId());
}

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) {
this.source = source;
this.arguments = arguments;
this.context = context;
this.fields = fields;
this.fieldType = fieldType;
this.parentType = parentType;
this.graphQLSchema = graphQLSchema;
this.fragmentsByName = fragmentsByName;
this.executionId = executionId;
}

@Override
Expand Down Expand Up @@ -70,4 +81,14 @@ public GraphQLType getParentType() {
public GraphQLSchema getGraphQLSchema() {
return graphQLSchema;
}

@Override
public Map<String, FragmentDefinition> getFragmentsByName() {
return fragmentsByName;
}

@Override
public ExecutionId getExecutionId() {
return executionId;
}
}
16 changes: 11 additions & 5 deletions src/test/groovy/graphql/DataFetcherTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package graphql

import graphql.schema.DataFetchingEnvironmentImpl
import graphql.schema.FieldDataFetcher
import graphql.schema.GraphQLOutputType
import graphql.schema.GraphQLScalarType
import graphql.schema.PropertyDataFetcher
import spock.lang.Specification

Expand Down Expand Up @@ -53,9 +55,13 @@ class DataFetcherTest extends Specification {
dataHolder.setBooleanFieldWithGet(false)
}

private def env(GraphQLOutputType type) {
new DataFetchingEnvironmentImpl(dataHolder, null, null, null, type, null, null, null, null)
}

def "get field value"() {
given:
def environment = new DataFetchingEnvironmentImpl(dataHolder, null, null, null, GraphQLString, null, null)
def environment = env(GraphQLString)
when:
def result = new FieldDataFetcher("publicField").get(environment)
then:
Expand All @@ -64,7 +70,7 @@ class DataFetcherTest extends Specification {

def "get property value"() {
given:
def environment = new DataFetchingEnvironmentImpl(dataHolder, null, null, null, GraphQLString, null, null)
def environment = env(GraphQLString)
when:
def result = new PropertyDataFetcher("property").get(environment)
then:
Expand All @@ -73,7 +79,7 @@ class DataFetcherTest extends Specification {

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

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

def "get field value as property"() {
given:
def environment = new DataFetchingEnvironmentImpl(dataHolder, null, null, null, GraphQLString, null, null)
def environment = env(GraphQLString)
when:
def result = new PropertyDataFetcher("publicField").get(environment)
then:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import spock.lang.Specification
class PropertyDataFetcherTest extends Specification {

def env(obj) {
return new DataFetchingEnvironmentImpl(obj,
return new DataFetchingEnvironmentImpl(
obj,
Collections.emptyMap(),
null,
Collections.emptyList(),
null,
null,
null,
null,
null
)
}
Expand Down