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
16 changes: 11 additions & 5 deletions src/main/java/graphql/GraphQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,24 @@ public class GraphQL {


private final GraphQLSchema graphQLSchema;
private final ExecutionStrategy executionStrategy;
private final ExecutionStrategy queryStrategy;
private final ExecutionStrategy mutationStrategy;

private static final Logger log = LoggerFactory.getLogger(GraphQL.class);

public GraphQL(GraphQLSchema graphQLSchema) {
this(graphQLSchema, null);
this(graphQLSchema, null, null);
}


public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy executionStrategy) {
public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: I wonder if this should be deprecated with the new change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the cod should have a Builder of GraphQL objects so it can be extended more easily over time.

GraphQL.newGraph()
  .schema(mySchema)
  .executionStategy(s1)
  .mutationStrategy(ms)
  .build();

I have a PR in that adds an "Instrumentation SPI" and this is YET another example adding extra aspects to the GrapQL object

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out @IamCornholio. Going to leave this for now, and consider @bbakerman's builder PR. Thanks

this(graphQLSchema, queryStrategy, null);
}

public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy, ExecutionStrategy mutationStrategy) {
this.graphQLSchema = graphQLSchema;
this.executionStrategy = executionStrategy;
this.queryStrategy = queryStrategy;
this.mutationStrategy = mutationStrategy;
}

public ExecutionResult execute(String requestString) {
Expand Down Expand Up @@ -74,7 +80,7 @@ public ExecutionResult execute(String requestString, String operationName, Objec
if (validationErrors.size() > 0) {
return new ExecutionResultImpl(validationErrors);
}
Execution execution = new Execution(executionStrategy);
Execution execution = new Execution(queryStrategy, mutationStrategy);
return execution.execute(graphQLSchema, context, document, operationName, arguments);
}

Expand Down
18 changes: 8 additions & 10 deletions src/main/java/graphql/execution/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@
public class Execution {

private FieldCollector fieldCollector = new FieldCollector();
private ExecutionStrategy strategy;
private ExecutionStrategy queryStrategy;
private ExecutionStrategy mutationStrategy;

public Execution(ExecutionStrategy strategy) {
this.strategy = strategy;

if (this.strategy == null) {
this.strategy = new SimpleExecutionStrategy();
}
public Execution(ExecutionStrategy queryStrategy, ExecutionStrategy mutationStrategy) {
this.queryStrategy = queryStrategy != null ? queryStrategy : new SimpleExecutionStrategy();
this.mutationStrategy = mutationStrategy != null ? mutationStrategy : new SimpleExecutionStrategy();
}

public ExecutionResult execute(GraphQLSchema graphQLSchema, Object root, Document document, String operationName, Map<String, Object> args) {
ExecutionContextBuilder executionContextBuilder = new ExecutionContextBuilder(new ValuesResolver());
ExecutionContext executionContext = executionContextBuilder.build(graphQLSchema, strategy, root, document, operationName, args);
ExecutionContext executionContext = executionContextBuilder.build(graphQLSchema, queryStrategy, mutationStrategy, root, document, operationName, args);
return executeOperation(executionContext, root, executionContext.getOperationDefinition());
}

Expand All @@ -55,9 +53,9 @@ private ExecutionResult executeOperation(
fieldCollector.collectFields(executionContext, operationRootType, operationDefinition.getSelectionSet(), new ArrayList<String>(), fields);

if (operationDefinition.getOperation() == OperationDefinition.Operation.MUTATION) {
return new SimpleExecutionStrategy().execute(executionContext, operationRootType, root, fields);
return mutationStrategy.execute(executionContext, operationRootType, root, fields);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

} else {
return strategy.execute(executionContext, operationRootType, root, fields);
return queryStrategy.execute(executionContext, operationRootType, root, fields);
}
}
}
16 changes: 10 additions & 6 deletions src/main/java/graphql/execution/ExecutionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@
import graphql.language.OperationDefinition;
import graphql.schema.GraphQLSchema;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

public class ExecutionContext {

private final GraphQLSchema graphQLSchema;
private final ExecutionStrategy executionStrategy;
private final ExecutionStrategy queryStrategy;
private final ExecutionStrategy mutationStrategy;
private final Map<String, FragmentDefinition> fragmentsByName;
private final OperationDefinition operationDefinition;
private final Map<String, Object> variables;
private final Object root;
private final List<GraphQLError> errors = new CopyOnWriteArrayList<GraphQLError>();

public ExecutionContext(GraphQLSchema graphQLSchema, ExecutionStrategy executionStrategy, Map<String, FragmentDefinition> fragmentsByName, OperationDefinition operationDefinition, Map<String, Object> variables, Object root) {
public ExecutionContext(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy, ExecutionStrategy mutationStrategy, Map<String, FragmentDefinition> fragmentsByName, OperationDefinition operationDefinition, Map<String, Object> variables, Object root) {
this.graphQLSchema = graphQLSchema;
this.executionStrategy = executionStrategy;
this.queryStrategy = queryStrategy;
this.mutationStrategy = mutationStrategy;
this.fragmentsByName = fragmentsByName;
this.operationDefinition = operationDefinition;
this.variables = variables;
Expand Down Expand Up @@ -62,8 +63,11 @@ public List<GraphQLError> getErrors() {
return errors;
}

public ExecutionStrategy getExecutionStrategy() {
return executionStrategy;
public ExecutionStrategy getQueryStrategy() {
return queryStrategy;
}

public ExecutionStrategy getMutationStrategy() {
return mutationStrategy;
}
}
6 changes: 3 additions & 3 deletions src/main/java/graphql/execution/ExecutionContextBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ExecutionContextBuilder(ValuesResolver valuesResolver) {
this.valuesResolver = valuesResolver;
}

public ExecutionContext build(GraphQLSchema graphQLSchema, ExecutionStrategy executionStrategy, Object root, Document document, String operationName, Map<String, Object> args) {
public ExecutionContext build(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy, ExecutionStrategy mutationStrategy, Object root, Document document, String operationName, Map<String, Object> args) {
Map<String, FragmentDefinition> fragmentsByName = new LinkedHashMap<String, FragmentDefinition>();
Map<String, OperationDefinition> operationsByName = new LinkedHashMap<String, OperationDefinition>();

Expand All @@ -45,12 +45,12 @@ public ExecutionContext build(GraphQLSchema graphQLSchema, ExecutionStrategy exe
if (operation == null) {
throw new GraphQLException();
}

Map<String, Object> variableValues = valuesResolver.getVariableValues(graphQLSchema, operation.getVariableDefinitions(), args);

return new ExecutionContext(
graphQLSchema,
executionStrategy,
queryStrategy,
mutationStrategy,
fragmentsByName,
operation,
variableValues,
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ protected ExecutionResult completeValue(ExecutionContext executionContext, Graph
fieldCollector.collectFields(executionContext, resolvedType, field.getSelectionSet(), visitedFragments, subFields);
}

// Calling this from the executionContext so that you can shift from the simple execution strategy for mutations
// back to the desired strategy.
// Calling this from the executionContext to ensure we shift back from mutation strategy to the query strategy.

return executionContext.getExecutionStrategy().execute(executionContext, resolvedType, result, subFields);
return executionContext.getQueryStrategy().execute(executionContext, resolvedType, result, subFields);
}

private ExecutionResult completeValueForList(ExecutionContext executionContext, GraphQLList fieldType, List<Field> fields, Object result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ExecutionStrategySpec extends Specification {
}

def buildContext() {
new ExecutionContext(null,executionStrategy,null,null,null,null)
new ExecutionContext(null,executionStrategy,executionStrategy,null,null,null,null)
}

def "completes value for a java.util.List"() {
Expand Down
56 changes: 56 additions & 0 deletions src/test/groovy/graphql/execution/ExecutionTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package graphql.execution

import graphql.MutationSchema
import graphql.parser.Parser
import spock.lang.Specification

class ExecutionTest extends Specification {

def parser = new Parser()
def mutationStrategy = Mock(ExecutionStrategy)
def queryStrategy = Mock(ExecutionStrategy)
def execution = new Execution(queryStrategy, mutationStrategy)

def "query strategy is used for query requests"() {
given:
def mutationStrategy = Mock(ExecutionStrategy)

def queryStrategy = Mock(ExecutionStrategy)
def execution = new Execution(queryStrategy, mutationStrategy)

def query = '''
query {
numberHolder {
theNumber
}
}
'''
def document = parser.parseDocument(query)

when:
execution.execute(MutationSchema.schema, null, document, null, null)

then:
1 * queryStrategy.execute(*_)
0 * mutationStrategy.execute(*_)
}

def "mutation strategy is used for mutation requests"() {
given:
def query = '''
mutation {
changeTheNumber(newNumber: 1) {
theNumber
}
}
'''
def document = parser.parseDocument(query)

when:
execution.execute(MutationSchema.schema, null, document, null, null)

then:
0 * queryStrategy.execute(*_)
1 * mutationStrategy.execute(*_)
}
}