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
60 changes: 31 additions & 29 deletions src/main/java/graphql/execution/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.ExecutionResultImpl;
import graphql.GraphQLException;
import graphql.Internal;
import graphql.MutationNotSupportedError;
import graphql.execution.instrumentation.Instrumentation;
Expand All @@ -20,6 +19,7 @@
import java.util.List;
import java.util.Map;

import static graphql.Assert.assertShouldNeverHappen;
import static graphql.execution.ExecutionStrategyParameters.newParameters;
import static graphql.execution.TypeInfo.newTypeInfo;
import static graphql.language.OperationDefinition.Operation.MUTATION;
Expand All @@ -43,37 +43,25 @@ public Execution(ExecutionStrategy queryStrategy, ExecutionStrategy mutationStra
}

public ExecutionResult execute(Document document, GraphQLSchema graphQLSchema, ExecutionId executionId, ExecutionInput executionInput) {
final Object context = executionInput.getContext();
final Object root = executionInput.getRoot();
final String operationName = executionInput.getOperationName();
final Map<String, Object> variables = executionInput.getVariables();

ExecutionContextBuilder executionContextBuilder = new ExecutionContextBuilder(new ValuesResolver(), instrumentation);
ExecutionContext executionContext = executionContextBuilder
ExecutionContext executionContext = new ExecutionContextBuilder()
.valuesResolver(new ValuesResolver())
.instrumentation(instrumentation)
.executionId(executionId)
.build(graphQLSchema, queryStrategy, mutationStrategy, subscriptionStrategy, context, root, document, operationName, variables);
return executeOperation(executionContext, root, executionContext.getOperationDefinition());
}

private GraphQLObjectType getOperationRootType(GraphQLSchema graphQLSchema, OperationDefinition.Operation operation) {
if (operation == MUTATION) {
return graphQLSchema.getMutationType();

} else if (operation == QUERY) {
return graphQLSchema.getQueryType();

} else if (operation == SUBSCRIPTION) {
return graphQLSchema.getSubscriptionType();

} else {
throw new GraphQLException("Unhandled case. An extra operation enum has been added without code support");
}
.graphQLSchema(graphQLSchema)
.queryStrategy(queryStrategy)
.mutationStrategy(mutationStrategy)
.subscriptionStrategy(subscriptionStrategy)
.context(executionInput.getContext())
.root(executionInput.getRoot())
.document(document)
.operationName(executionInput.getOperationName())
.variables(executionInput.getVariables())
.build();
return executeOperation(executionContext, executionInput.getRoot(), executionContext.getOperationDefinition());
}

private ExecutionResult executeOperation(
ExecutionContext executionContext,
Object root,
OperationDefinition operationDefinition) {
private ExecutionResult executeOperation(ExecutionContext executionContext, Object root, OperationDefinition operationDefinition) {

InstrumentationContext<ExecutionResult> dataFetchCtx = instrumentation.beginDataFetch(new InstrumentationDataFetchParameters(executionContext));

Expand All @@ -89,7 +77,9 @@ private ExecutionResult executeOperation(
return new ExecutionResultImpl(Collections.singletonList(new MutationNotSupportedError()));
}

FieldCollectorParameters collectorParameters = FieldCollectorParameters.newParameters(executionContext.getGraphQLSchema(), operationRootType)
FieldCollectorParameters collectorParameters = FieldCollectorParameters.newParameters()
.schema(executionContext.getGraphQLSchema())
.objectType(operationRootType)
.fragments(executionContext.getFragmentsByName())
.variables(executionContext.getVariables())
.build();
Expand Down Expand Up @@ -130,4 +120,16 @@ private ExecutionResult executeOperation(
dataFetchCtx.onEnd(result);
return result;
}

private GraphQLObjectType getOperationRootType(GraphQLSchema graphQLSchema, OperationDefinition.Operation operation) {
if (operation == MUTATION) {
return graphQLSchema.getMutationType();
} else if (operation == QUERY) {
return graphQLSchema.getQueryType();
} else if (operation == SUBSCRIPTION) {
return graphQLSchema.getSubscriptionType();
} else {
return assertShouldNeverHappen("Unhandled case. An extra operation enum has been added without code support");
}
}
}
64 changes: 61 additions & 3 deletions src/main/java/graphql/execution/ExecutionContextBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,77 @@ public class ExecutionContextBuilder {
private ValuesResolver valuesResolver;
private Instrumentation instrumentation;
private ExecutionId executionId;
private GraphQLSchema graphQLSchema;
private ExecutionStrategy queryStrategy;
private ExecutionStrategy mutationStrategy;
private ExecutionStrategy subscriptionStrategy;
private Object context;
private Object root;
private Document document;
private String operationName;
private Map<String, Object> variables;

public ExecutionContextBuilder(ValuesResolver valuesResolver, Instrumentation instrumentation) {
public ExecutionContextBuilder valuesResolver(ValuesResolver valuesResolver) {
this.valuesResolver = valuesResolver;
this.instrumentation = instrumentation;
return this;
}

public ExecutionContextBuilder instrumentation(Instrumentation instrumentation) {
this.instrumentation = instrumentation;
return this;
}

public ExecutionContextBuilder executionId(ExecutionId executionId) {
this.executionId = executionId;
return this;
}

public ExecutionContext build(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy, ExecutionStrategy mutationStrategy, ExecutionStrategy subscriptionStrategy, Object context, Object root, Document document, String operationName, Map<String, Object> variables) {
public ExecutionContextBuilder graphQLSchema(GraphQLSchema graphQLSchema) {
this.graphQLSchema = graphQLSchema;
return this;
}

public ExecutionContextBuilder queryStrategy(ExecutionStrategy queryStrategy) {
this.queryStrategy = queryStrategy;
return this;
}

public ExecutionContextBuilder mutationStrategy(ExecutionStrategy mutationStrategy) {
this.mutationStrategy = mutationStrategy;
return this;
}

public ExecutionContextBuilder subscriptionStrategy(ExecutionStrategy subscriptionStrategy) {
this.subscriptionStrategy = subscriptionStrategy;
return this;
}

public ExecutionContextBuilder context(Object context) {
this.context = context;
return this;
}

public ExecutionContextBuilder root(Object root) {
this.root = root;
return this;
}

public ExecutionContextBuilder document(Document document) {
this.document = document;
return this;
}

public ExecutionContextBuilder operationName(String operationName) {
this.operationName = operationName;
return this;
}

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

public ExecutionContext build() {
// preconditions
assertNotNull(executionId, "You must provide a query identifier");

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ protected ExecutionResult completeValue(ExecutionContext executionContext, Execu
resolvedType = (GraphQLObjectType) fieldType;
}

FieldCollectorParameters collectorParameters = newParameters(executionContext.getGraphQLSchema(), resolvedType)
FieldCollectorParameters collectorParameters = newParameters()
.schema(executionContext.getGraphQLSchema())
.objectType(resolvedType)
.fragments(executionContext.getFragmentsByName())
.variables(executionContext.getVariables())
.build();
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/graphql/execution/ExecutionStrategyParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public ExecutionStrategyParameters transform(Consumer<Builder> builderConsumer)
return builder.build();
}

@Override
public String toString() {
return String.format("ExecutionStrategyParameters { path=%s, typeInfo=%s, source=%s, fields=%s }",
path, typeInfo, source, fields);
}

public static Builder newParameters() {
return new Builder();
Expand All @@ -70,12 +75,6 @@ public static Builder newParameters(ExecutionStrategyParameters oldParameters) {
return new Builder(oldParameters);
}

@Override
public String toString() {
return String.format("ExecutionStrategyParameters { path=%s, typeInfo=%s, source=%s, fields=%s }",
path, typeInfo, source, fields);
}

public static class Builder {
TypeInfo typeInfo;
Object source;
Expand All @@ -84,9 +83,15 @@ public static class Builder {
NonNullableFieldValidator nonNullableFieldValidator;
ExecutionPath path = ExecutionPath.rootPath();

/**
* @see ExecutionStrategyParameters#newParameters()
*/
private Builder() {
}

/**
* @see ExecutionStrategyParameters#newParameters(ExecutionStrategyParameters)
*/
private Builder(ExecutionStrategyParameters oldParameters) {
this.typeInfo = oldParameters.typeInfo;
this.source = oldParameters.source;
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/graphql/execution/FieldCollectorParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ private FieldCollectorParameters(GraphQLSchema graphQLSchema, Map<String, Object
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 Builder newParameters() {
return new Builder();
}

public static class Builder {
Expand All @@ -49,6 +47,13 @@ public static class Builder {
private Map<String, Object> variables = new LinkedHashMap<>();
private GraphQLObjectType objectType;

/**
* @see FieldCollectorParameters#newParameters()
*/
private Builder() {

}

public Builder schema(GraphQLSchema graphQLSchema) {
this.graphQLSchema = graphQLSchema;
return this;
Expand All @@ -70,6 +75,8 @@ public Builder variables(Map<String, Object> variables) {
}

public FieldCollectorParameters build() {
Assert.assertNotNull(graphQLSchema, "You must provide a schema");
Assert.assertNotNull(objectType, "You must provide an object type");
return new FieldCollectorParameters(graphQLSchema, variables, fragmentsByName, objectType);
}

Expand Down
15 changes: 10 additions & 5 deletions src/main/java/graphql/execution/TypeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ public TypeInfo asType(GraphQLType type) {
return new TypeInfo(unwrap(type), this.parentType, this.typeIsNonNull);
}

public static Builder newTypeInfo() {
return new Builder();
}

private static GraphQLType unwrap(GraphQLType type) {
// its possible to have non nulls wrapping non nulls of things but it must end at some point
while (type instanceof GraphQLNonNull) {
Expand All @@ -67,17 +63,26 @@ private static GraphQLType unwrap(GraphQLType type) {
return type;
}


@Override
public String toString() {
return String.format("TypeInfo { nonnull=%s, type=%s, parentType=%s }",
typeIsNonNull, type, parentType);
}

public static TypeInfo.Builder newTypeInfo() {
return new Builder();
}

public static class Builder {
GraphQLType type;
TypeInfo parentType;

/**
* @see TypeInfo#newTypeInfo()
*/
private Builder() {
}

public Builder type(GraphQLType type) {
this.type = type;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ private boolean isNonNull(GraphQLType fieldType) {
private Map<String, List<Field>> getChildFields(ExecutionContext executionContext, GraphQLObjectType resolvedType,
List<Field> fields) {

FieldCollectorParameters collectorParameters = newParameters(executionContext.getGraphQLSchema(), resolvedType)
FieldCollectorParameters collectorParameters = newParameters()
.schema(executionContext.getGraphQLSchema())
.objectType(resolvedType)
.fragments(executionContext.getFragmentsByName())
.variables(executionContext.getVariables())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public static DataFetchingFieldSelectionSet newCollector(ExecutionContext execut
private DataFetchingFieldSelectionSetImpl(ExecutionContext executionContext, GraphQLObjectType fieldType, List<Field> fields) {
this.fields = fields;
this.fieldCollector = new FieldCollector();
this.parameters = FieldCollectorParameters.
newParameters(executionContext.getGraphQLSchema(), fieldType)
this.parameters = FieldCollectorParameters.newParameters()
.schema(executionContext.getGraphQLSchema())
.objectType(fieldType)
.fragments(executionContext.getFragmentsByName())
.variables(executionContext.getVariables())
.build();
Expand Down