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
7 changes: 4 additions & 3 deletions src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
public abstract class ExecutionStrategy {
private static final Logger log = LoggerFactory.getLogger(ExecutionStrategy.class);

protected ValuesResolver valuesResolver = new ValuesResolver();
protected FieldCollector fieldCollector = new FieldCollector();
protected final ValuesResolver valuesResolver = new ValuesResolver();
protected final FieldCollector fieldCollector = new FieldCollector();

public abstract ExecutionResult execute(ExecutionContext executionContext, GraphQLObjectType parentType, Object source, Map<String, List<Field>> fields);

protected ExecutionResult resolveField(ExecutionContext executionContext, GraphQLObjectType parentType, Object source, List<Field> fields) {
GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, fields.get(0));

Map<String, Object> argumentValues = valuesResolver.getArgumentValues(fieldDef.getArguments(), fields.get(0).getArguments(), executionContext.getVariables());
DataFetchingEnvironment environment = new DataFetchingEnvironment(
DataFetchingEnvironment environment = new DataFetchingEnvironmentImpl(
source,
argumentValues,
executionContext.getRoot(),
Expand Down Expand Up @@ -92,6 +92,7 @@ private ExecutionResult completeValueForList(ExecutionContext executionContext,
result = Arrays.asList((Object[]) result);
}

//noinspection unchecked
return completeValueForList(executionContext, fieldType, fields, (Iterable<Object>) result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ private List<GraphQLExecutionNode> handleList(ExecutionContext executionContext,

}

@SuppressWarnings("UnnecessaryLocalVariable")
private List<GraphQLExecutionNode> handleObject(ExecutionContext executionContext,
List<GraphQLExecutionNodeValue> values, String fieldName, List<Field> fields, GraphQLType fieldType) {

Expand Down Expand Up @@ -250,7 +251,7 @@ private List<GraphQLExecutionNodeValue> fetchData(ExecutionContext executionCont
for (GraphQLExecutionNodeDatum n : nodeData) {
sources.add(n.getSource());
}
DataFetchingEnvironment environment = new DataFetchingEnvironment(
DataFetchingEnvironment environment = new DataFetchingEnvironmentImpl(
sources,
argumentValues,
executionContext.getRoot(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.DataFetchingEnvironmentImpl;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -24,11 +25,10 @@ public UnbatchedDataFetcher(DataFetcher delegate) {

@Override
public Object get(DataFetchingEnvironment environment) {
@SuppressWarnings("unchecked")
List<Object> sources = (List<Object>) environment.getSource();
List<Object> sources = environment.getSource();
List<Object> results = new ArrayList<Object>();
for (Object source : sources) {
DataFetchingEnvironment singleEnv = new DataFetchingEnvironment(
DataFetchingEnvironment singleEnv = new DataFetchingEnvironmentImpl(
source,
environment.getArguments(),
environment.getContext(),
Expand Down
104 changes: 56 additions & 48 deletions src/main/java/graphql/schema/DataFetchingEnvironment.java
Original file line number Diff line number Diff line change
@@ -1,64 +1,72 @@
package graphql.schema;


import graphql.language.Field;

import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
public class DataFetchingEnvironment {
private final Object source;
private final Map<String, Object> arguments;
private final Object context;
private final List<Field> fields;
private final GraphQLOutputType fieldType;
private final GraphQLType parentType;
private final GraphQLSchema graphQLSchema;

public DataFetchingEnvironment(Object source, Map<String, Object> arguments, Object context, List<Field> fields, GraphQLOutputType fieldType, GraphQLType parentType, GraphQLSchema graphQLSchema) {
this.source = source;
this.arguments = arguments;
this.context = context;
this.fields = fields;
this.fieldType = fieldType;
this.parentType = parentType;
this.graphQLSchema = graphQLSchema;
}

public <T> T getSource() {
return (T) source;
}
/**
* A DataFetchingEnvironment instance of passed to a {@link DataFetcher} as an execution context parameter
*/
public interface DataFetchingEnvironment {
/**
* @param <T> you decide what type it is
*
* @return the current object being queried
*/
<T> T getSource();

public Map<String, Object> getArguments() {
return arguments;
}
/**
* @return the arguments that have been passed in via the graphql query
*/
Map<String, Object> getArguments();

public boolean containsArgument(String name) {
return arguments.containsKey(name);
}
/**
* Returns true of the named argument is present
*
* @param name the name of the argument
*
* @return true of the named argument is present
*/
boolean containsArgument(String name);

public <T> T getArgument(String name) {
return (T) arguments.get(name);
}
/**
* Returns the named argument
*
* @param name the name of the argument
* @param <T> you decide what type it is
*
* @return the named argument or null if its not [present
*/
<T> T getArgument(String name);

public <T> T getContext() {
return (T) context;
}
/**
* Returns a context argument that is set up when the {@link graphql.GraphQL#execute(String, Object)} method
* is invoked
*
* @param <T> you decide what type it is
*
* @return a context object
*/
<T> T getContext();

public List<Field> getFields() {
return fields;
}
/**
* @return the list of fields currently in query context
*/
List<Field> getFields();

public GraphQLOutputType getFieldType() {
return fieldType;
}
/**
* @return graphql type of the current field
*/
GraphQLOutputType getFieldType();

public GraphQLType getParentType() {
return parentType;
}
/**
* @return the type of the parent of the current field
*/
GraphQLType getParentType();

public GraphQLSchema getGraphQLSchema() {
return graphQLSchema;
}
/**
* @return the underlying graphql schema
*/
GraphQLSchema getGraphQLSchema();
}
73 changes: 73 additions & 0 deletions src/main/java/graphql/schema/DataFetchingEnvironmentImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package graphql.schema;


import graphql.language.Field;

import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
public class DataFetchingEnvironmentImpl implements DataFetchingEnvironment {
private final Object source;
private final Map<String, Object> arguments;
private final Object context;
private final List<Field> fields;
private final GraphQLOutputType fieldType;
private final GraphQLType parentType;
private final GraphQLSchema graphQLSchema;

public DataFetchingEnvironmentImpl(Object source, Map<String, Object> arguments, Object context, List<Field> fields, GraphQLOutputType fieldType, GraphQLType parentType, GraphQLSchema graphQLSchema) {
this.source = source;
this.arguments = arguments;
this.context = context;
this.fields = fields;
this.fieldType = fieldType;
this.parentType = parentType;
this.graphQLSchema = graphQLSchema;
}

@Override
public <T> T getSource() {
return (T) source;
}

@Override
public Map<String, Object> getArguments() {
return arguments;
}

@Override
public boolean containsArgument(String name) {
return arguments.containsKey(name);
}

@Override
public <T> T getArgument(String name) {
return (T) arguments.get(name);
}

@Override
public <T> T getContext() {
return (T) context;
}

@Override
public List<Field> getFields() {
return fields;
}

@Override
public GraphQLOutputType getFieldType() {
return fieldType;
}

@Override
public GraphQLType getParentType() {
return parentType;
}

@Override
public GraphQLSchema getGraphQLSchema() {
return graphQLSchema;
}
}
32 changes: 17 additions & 15 deletions src/test/groovy/graphql/DataFetcherTest.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package graphql

import graphql.schema.DataFetchingEnvironment
import graphql.schema.DataFetchingEnvironmentImpl
import graphql.schema.FieldDataFetcher
import graphql.schema.PropertyDataFetcher
import spock.lang.Specification
Expand All @@ -10,41 +10,43 @@ import static graphql.Scalars.GraphQLString

class DataFetcherTest extends Specification {

@SuppressWarnings("GroovyUnusedDeclaration")
class DataHolder {

private String privateField
public String publicField
private Boolean booleanField
private Boolean booleanFieldWithGet

public String getProperty() {
String getProperty() {
return privateField
}

public void setProperty(String value) {
void setProperty(String value) {
privateField = value
}

public Boolean isBooleanField() {
return booleanField;
Boolean isBooleanField() {
return booleanField
}

public void setBooleanField(Boolean value) {
void setBooleanField(Boolean value) {
booleanField = value
}

public Boolean getBooleanFieldWithGet() {
Boolean getBooleanFieldWithGet() {
return booleanFieldWithGet
}

public Boolean setBooleanFieldWithGet(Boolean value) {
Boolean setBooleanFieldWithGet(Boolean value) {
booleanFieldWithGet = value
}
}
def DataHolder dataHolder

DataHolder dataHolder

def setup() {
dataHolder = new DataHolder();
dataHolder = new DataHolder()
dataHolder.publicField = "publicValue"
dataHolder.setProperty("propertyValue")
dataHolder.setBooleanField(true)
Expand All @@ -53,7 +55,7 @@ class DataFetcherTest extends Specification {

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

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

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

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

def "get field value as property"() {
given:
def environment = new DataFetchingEnvironment(dataHolder, null, null, null, GraphQLString, null, null)
def environment = new DataFetchingEnvironmentImpl(dataHolder, null, null, null, GraphQLString, null, null)
when:
def result = new PropertyDataFetcher("publicField").get(environment)
then:
Expand Down