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
6 changes: 3 additions & 3 deletions src/main/java/graphql/execution/ConditionalNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ public ConditionalNodes() {
valuesResolver = new ValuesResolver();
}

public boolean shouldInclude(ExecutionContext executionContext, List<Directive> directives) {
public boolean shouldInclude(Map<String, Object> variables, List<Directive> directives) {

Directive skipDirective = findDirective(directives, SkipDirective.getName());
if (skipDirective != null) {
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(SkipDirective.getArguments(), skipDirective.getArguments(), executionContext.getVariables());
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(SkipDirective.getArguments(), skipDirective.getArguments(), variables);
return !(Boolean) argumentValues.get("if");
}


Directive includeDirective = findDirective(directives, IncludeDirective.getName());
if (includeDirective != null) {
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(IncludeDirective.getArguments(), includeDirective.getArguments(), executionContext.getVariables());
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(IncludeDirective.getArguments(), includeDirective.getArguments(), variables);
return (Boolean) argumentValues.get("if");
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/graphql/execution/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -85,9 +83,12 @@ private ExecutionResult executeOperation(
return new ExecutionResultImpl(Collections.singletonList(new MutationNotSupportedError()));
}

Map<String, List<Field>> fields = new LinkedHashMap<>();
fieldCollector.collectFields(executionContext, operationRootType, operationDefinition.getSelectionSet(), new ArrayList<>(), fields);
FieldCollectorParameters collectorParameters = FieldCollectorParameters.newParameters(executionContext.getGraphQLSchema(), operationRootType)
.fragments(executionContext.getFragmentsByName())
.variables(executionContext.getVariables())
.build();

Map<String, List<Field>> fields = fieldCollector.collectFields(collectorParameters, operationDefinition.getSelectionSet());

ExecutionParameters parameters = newParameters()
.typeInfo(newTypeInfo().type(operationRootType))
Expand Down
36 changes: 23 additions & 13 deletions src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
import graphql.language.Field;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.DataFetchingEnvironmentImpl;
import graphql.schema.DataFetchingFieldSelectionSet;
import graphql.schema.DataFetchingFieldSelectionSetImpl;
import graphql.schema.GraphQLEnumType;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLInterfaceType;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLOutputType;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLType;
Expand All @@ -27,10 +30,10 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static graphql.execution.FieldCollectorParameters.newParameters;
import static graphql.execution.TypeInfo.newTypeInfo;
import static graphql.introspection.Introspection.SchemaMetaFieldDef;
import static graphql.introspection.Introspection.TypeMetaFieldDef;
Expand Down Expand Up @@ -70,14 +73,21 @@ protected ExecutionResult resolveField(ExecutionContext executionContext, Execut
GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), type, fields.get(0));

Map<String, Object> argumentValues = valuesResolver.getArgumentValues(fieldDef.getArguments(), fields.get(0).getArguments(), executionContext.getVariables());

GraphQLOutputType fieldType = fieldDef.getType();
DataFetchingFieldSelectionSet fieldCollector = DataFetchingFieldSelectionSetImpl.newCollector(executionContext, fieldType, fields);

DataFetchingEnvironment environment = new DataFetchingEnvironmentImpl(
parameters.source(),
argumentValues,
executionContext.getRoot(),
fields,
fieldDef.getType(),
fieldType,
type,
executionContext
);
executionContext.getGraphQLSchema(),
executionContext.getFragmentsByName(),
executionContext.getExecutionId(),
fieldCollector);

Instrumentation instrumentation = executionContext.getInstrumentation();

Expand All @@ -95,14 +105,14 @@ protected ExecutionResult resolveField(ExecutionContext executionContext, Execut
fetchCtx.onEnd(e);
}

TypeInfo fieldType = newTypeInfo()
.type(fieldDef.getType())
TypeInfo fieldTypeInfo = newTypeInfo()
.type(fieldType)
.parentInfo(parameters.typeInfo())
.build();


ExecutionParameters newParameters = ExecutionParameters.newParameters()
.typeInfo(fieldType)
.typeInfo(fieldTypeInfo)
.fields(parameters.fields())
.arguments(argumentValues)
.source(resolvedValue).build();
Expand Down Expand Up @@ -157,12 +167,12 @@ protected ExecutionResult completeValue(ExecutionContext executionContext, Execu
resolvedType = (GraphQLObjectType) fieldType;
}

Map<String, List<Field>> subFields = new LinkedHashMap<>();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice how visitedFragments is NEVER used. Thats why I took it out of the public method signature

List<String> visitedFragments = new ArrayList<>();
for (Field field : fields) {
if (field.getSelectionSet() == null) continue;
fieldCollector.collectFields(executionContext, resolvedType, field.getSelectionSet(), visitedFragments, subFields);
}
FieldCollectorParameters collectorParameters = newParameters(executionContext.getGraphQLSchema(), resolvedType)
.fragments(executionContext.getFragmentsByName())
.variables(executionContext.getVariables())
.build();

Map<String, List<Field>> subFields = fieldCollector.collectFields(collectorParameters, fields);

ExecutionParameters newParameters = ExecutionParameters.newParameters()
.typeInfo(typeInfo.asType(resolvedType))
Expand Down
90 changes: 66 additions & 24 deletions src/main/java/graphql/execution/FieldCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
import graphql.schema.SchemaUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static graphql.execution.TypeFromAST.getTypeFromAST;

/**
* A field collector can iterate over field selection sets and build out the sub fields that have been selected,
* expanding named and inline fragments as it goes.s
*/
@Internal
public class FieldCollector {

Expand All @@ -32,48 +37,84 @@ public FieldCollector() {
}


public void collectFields(ExecutionContext executionContext, GraphQLObjectType type, SelectionSet selectionSet, List<String> visitedFragments, Map<String, List<Field>> fields) {
/**
* Given a list of fields this will collect the sub-field selections and return it as a map
*
* @param parameters the parameters to this method
* @param fields the list of fields to collect for
*
* @return a map of the sub field selections
*/
public Map<String, List<Field>> collectFields(FieldCollectorParameters parameters, List<Field> fields) {
Map<String, List<Field>> subFields = new LinkedHashMap<>();
List<String> visitedFragments = new ArrayList<>();
for (Field field : fields) {
if (field.getSelectionSet() == null) {
continue;
}
this.collectFields(parameters, field.getSelectionSet(), visitedFragments, subFields);
}
return subFields;
}

/**
* Given a selection set this will collect the sub-field selections and return it as a map
*
* @param parameters the parameters to this method
* @param selectionSet the selection set to collect on
*
* @return a map of the sub field selections
*/
public Map<String, List<Field>> collectFields(FieldCollectorParameters parameters, SelectionSet selectionSet) {
Map<String, List<Field>> subFields = new LinkedHashMap<>();
List<String> visitedFragments = new ArrayList<>();
this.collectFields(parameters, selectionSet, visitedFragments, subFields);
return subFields;
}


private void collectFields(FieldCollectorParameters parameters, SelectionSet selectionSet, List<String> visitedFragments, Map<String, List<Field>> fields) {

for (Selection selection : selectionSet.getSelections()) {
if (selection instanceof Field) {
collectField(executionContext, fields, (Field) selection);
collectField(parameters, fields, (Field) selection);
} else if (selection instanceof InlineFragment) {
collectInlineFragment(executionContext, type, visitedFragments, fields, (InlineFragment) selection);
collectInlineFragment(parameters, visitedFragments, fields, (InlineFragment) selection);
} else if (selection instanceof FragmentSpread) {
collectFragmentSpread(executionContext, type, visitedFragments, fields, (FragmentSpread) selection);
collectFragmentSpread(parameters, visitedFragments, fields, (FragmentSpread) selection);
}
}
}

private void collectFragmentSpread(ExecutionContext executionContext, GraphQLObjectType type, List<String> visitedFragments, Map<String, List<Field>> fields, FragmentSpread fragmentSpread) {
private void collectFragmentSpread(FieldCollectorParameters parameters, List<String> visitedFragments, Map<String, List<Field>> fields, FragmentSpread fragmentSpread) {
if (visitedFragments.contains(fragmentSpread.getName())) {
return;
}
if (!conditionalNodes.shouldInclude(executionContext, fragmentSpread.getDirectives())) {
if (!conditionalNodes.shouldInclude(parameters.getVariables(), fragmentSpread.getDirectives())) {
return;
}
visitedFragments.add(fragmentSpread.getName());
FragmentDefinition fragmentDefinition = executionContext.getFragment(fragmentSpread.getName());
FragmentDefinition fragmentDefinition = parameters.getFragmentsByName().get(fragmentSpread.getName());

if (!conditionalNodes.shouldInclude(executionContext, fragmentDefinition.getDirectives())) {
if (!conditionalNodes.shouldInclude(parameters.getVariables(), fragmentDefinition.getDirectives())) {
return;
}
if (!doesFragmentConditionMatch(executionContext, fragmentDefinition, type)) {
if (!doesFragmentConditionMatch(parameters, fragmentDefinition)) {
return;
}
collectFields(executionContext, type, fragmentDefinition.getSelectionSet(), visitedFragments, fields);
collectFields(parameters, fragmentDefinition.getSelectionSet(), visitedFragments, fields);
}

private void collectInlineFragment(ExecutionContext executionContext, GraphQLObjectType type, List<String> visitedFragments, Map<String, List<Field>> fields, InlineFragment inlineFragment) {
if (!conditionalNodes.shouldInclude(executionContext, inlineFragment.getDirectives()) ||
!doesFragmentConditionMatch(executionContext, inlineFragment, type)) {
private void collectInlineFragment(FieldCollectorParameters parameters, List<String> visitedFragments, Map<String, List<Field>> fields, InlineFragment inlineFragment) {
if (!conditionalNodes.shouldInclude(parameters.getVariables(), inlineFragment.getDirectives()) ||
!doesFragmentConditionMatch(parameters, inlineFragment)) {
return;
}
collectFields(executionContext, type, inlineFragment.getSelectionSet(), visitedFragments, fields);
collectFields(parameters, inlineFragment.getSelectionSet(), visitedFragments, fields);
}

private void collectField(ExecutionContext executionContext, Map<String, List<Field>> fields, Field field) {
if (!conditionalNodes.shouldInclude(executionContext, field.getDirectives())) {
private void collectField(FieldCollectorParameters parameters, Map<String, List<Field>> fields, Field field) {
if (!conditionalNodes.shouldInclude(parameters.getVariables(), field.getDirectives())) {
return;
}
String name = getFieldEntryKey(field);
Expand All @@ -89,28 +130,29 @@ private String getFieldEntryKey(Field field) {
}


private boolean doesFragmentConditionMatch(ExecutionContext executionContext, InlineFragment inlineFragment, GraphQLObjectType type) {
private boolean doesFragmentConditionMatch(FieldCollectorParameters parameters, InlineFragment inlineFragment) {
if (inlineFragment.getTypeCondition() == null) {
return true;
}
GraphQLType conditionType;
conditionType = getTypeFromAST(executionContext.getGraphQLSchema(), inlineFragment.getTypeCondition());
return checkTypeCondition(executionContext, type, conditionType);
conditionType = getTypeFromAST(parameters.getGraphQLSchema(), inlineFragment.getTypeCondition());
return checkTypeCondition(parameters, conditionType);
}

private boolean doesFragmentConditionMatch(ExecutionContext executionContext, FragmentDefinition fragmentDefinition, GraphQLObjectType type) {
private boolean doesFragmentConditionMatch(FieldCollectorParameters parameters, FragmentDefinition fragmentDefinition) {
GraphQLType conditionType;
conditionType = getTypeFromAST(executionContext.getGraphQLSchema(), fragmentDefinition.getTypeCondition());
return checkTypeCondition(executionContext, type, conditionType);
conditionType = getTypeFromAST(parameters.getGraphQLSchema(), fragmentDefinition.getTypeCondition());
return checkTypeCondition(parameters, conditionType);
}

private boolean checkTypeCondition(ExecutionContext executionContext, GraphQLObjectType type, GraphQLType conditionType) {
private boolean checkTypeCondition(FieldCollectorParameters parameters, GraphQLType conditionType) {
GraphQLObjectType type = parameters.getObjectType();
if (conditionType.equals(type)) {
return true;
}

if (conditionType instanceof GraphQLInterfaceType) {
List<GraphQLObjectType> implementations = schemaUtil.findImplementations(executionContext.getGraphQLSchema(), (GraphQLInterfaceType) conditionType);
List<GraphQLObjectType> implementations = schemaUtil.findImplementations(parameters.getGraphQLSchema(), (GraphQLInterfaceType) conditionType);
return implementations.contains(type);
} else if (conditionType instanceof GraphQLUnionType) {
return ((GraphQLUnionType) conditionType).getTypes().contains(type);
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/graphql/execution/FieldCollectorParameters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package graphql.execution;

import graphql.Assert;
import graphql.language.FragmentDefinition;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;

import java.util.LinkedHashMap;
import java.util.Map;

public class FieldCollectorParameters {
private final GraphQLSchema graphQLSchema;
private final Map<String, FragmentDefinition> fragmentsByName;
private final Map<String, Object> variables;
private final GraphQLObjectType objectType;

public GraphQLSchema getGraphQLSchema() {
return graphQLSchema;
}

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

public Map<String, Object> getVariables() {
return variables;
}

public GraphQLObjectType getObjectType() {
return objectType;
}

private FieldCollectorParameters(GraphQLSchema graphQLSchema, Map<String, Object> variables, Map<String, FragmentDefinition> fragmentsByName, GraphQLObjectType objectType) {
this.fragmentsByName = fragmentsByName;
this.graphQLSchema = graphQLSchema;
this.variables = variables;
this.objectType = objectType;
}

public static Builder newParameters(GraphQLSchema graphQLSchema, GraphQLObjectType objectType) {
Assert.assertNotNull(graphQLSchema, "You must provide a schema");
Assert.assertNotNull(objectType, "You must provide an object type");
return new Builder().schema(graphQLSchema).objectType(objectType);
}

public static class Builder {
private GraphQLSchema graphQLSchema;
private Map<String, FragmentDefinition> fragmentsByName = new LinkedHashMap<>();
private Map<String, Object> variables = new LinkedHashMap<>();
private GraphQLObjectType objectType;

public Builder schema(GraphQLSchema graphQLSchema) {
this.graphQLSchema = graphQLSchema;
return this;
}

public Builder objectType(GraphQLObjectType objectType) {
this.objectType = objectType;
return this;
}

public Builder fragments(Map<String, FragmentDefinition> fragmentsByName) {
this.fragmentsByName.putAll(fragmentsByName);
return this;
}

public Builder variables(Map<String, Object> variables) {
this.variables.putAll(variables);
return this;
}

public FieldCollectorParameters build() {
return new FieldCollectorParameters(graphQLSchema, variables, fragmentsByName, objectType);
}

}
}
Loading