Repository navigation
Expand file tree
/
Copy pathExecution.java
More file actions
91 lines (73 loc) · 4.04 KB
/
Copy pathExecution.java
File metadata and controls
91 lines (73 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package graphql.execution;
import graphql.ExecutionResult;
import graphql.ExecutionResultImpl;
import graphql.GraphQLException;
import graphql.MutationNotSupportedError;
import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.parameters.DataFetchParameters;
import graphql.language.Document;
import graphql.language.Field;
import graphql.language.OperationDefinition;
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;
import static graphql.language.OperationDefinition.Operation.MUTATION;
import static graphql.language.OperationDefinition.Operation.QUERY;
public class Execution {
private final FieldCollector fieldCollector = new FieldCollector();
private final ExecutionStrategy queryStrategy;
private final ExecutionStrategy mutationStrategy;
private final Instrumentation instrumentation;
public Execution(ExecutionStrategy queryStrategy, ExecutionStrategy mutationStrategy, Instrumentation instrumentation) {
this.queryStrategy = queryStrategy != null ? queryStrategy : new SimpleExecutionStrategy();
this.mutationStrategy = mutationStrategy != null ? mutationStrategy : new SimpleExecutionStrategy();
this.instrumentation = instrumentation;
}
public ExecutionResult execute(ExecutionId executionId, GraphQLSchema graphQLSchema, Object root, Document document, String operationName, Map<String, Object> args) {
ExecutionContextBuilder executionContextBuilder = new ExecutionContextBuilder(new ValuesResolver(), instrumentation);
ExecutionContext executionContext = executionContextBuilder
.executionId(executionId)
.build(graphQLSchema, queryStrategy, mutationStrategy, root, document, operationName, args);
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 {
throw new GraphQLException("Unhandled case. An extra operation enum has been added without code support");
}
}
private ExecutionResult executeOperation(
ExecutionContext executionContext,
Object root,
OperationDefinition operationDefinition) {
InstrumentationContext<ExecutionResult> dataFetchCtx = instrumentation.beginDataFetch(new DataFetchParameters(executionContext));
OperationDefinition.Operation operation = operationDefinition.getOperation();
GraphQLObjectType operationRootType = getOperationRootType(executionContext.getGraphQLSchema(), operation);
//
// do we have a mutation operation root type. The spec says if we don't then mutations are not allowed to be executed
//
// for the record earlier code has asserted that we have a query type in the schema since the spec says this is
// ALWAYS required
if (operation == MUTATION && operationRootType == null) {
return new ExecutionResultImpl(Collections.singletonList(new MutationNotSupportedError()));
}
Map<String, List<Field>> fields = new LinkedHashMap<String, List<Field>>();
fieldCollector.collectFields(executionContext, operationRootType, operationDefinition.getSelectionSet(), new ArrayList<String>(), fields);
ExecutionResult result;
if (operation == MUTATION) {
result = mutationStrategy.execute(executionContext, operationRootType, root, fields);
} else {
result = queryStrategy.execute(executionContext, operationRootType, root, fields);
}
dataFetchCtx.onEnd(result);
return result;
}
}