Repository navigation
Expand file tree
/
Copy pathGraphQL.java
More file actions
173 lines (144 loc) · 6.65 KB
/
Copy pathGraphQL.java
File metadata and controls
173 lines (144 loc) · 6.65 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package graphql;
import graphql.execution.Execution;
import graphql.execution.ExecutionId;
import graphql.execution.ExecutionIdProvider;
import graphql.execution.ExecutionStrategy;
import graphql.execution.SimpleExecutionStrategy;
import graphql.language.Document;
import graphql.language.SourceLocation;
import graphql.parser.Parser;
import graphql.schema.GraphQLSchema;
import graphql.validation.ValidationError;
import graphql.validation.Validator;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.misc.ParseCancellationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static graphql.Assert.assertNotNull;
public class GraphQL {
private final GraphQLSchema graphQLSchema;
private final ExecutionStrategy queryStrategy;
private final ExecutionStrategy mutationStrategy;
//
// later PR changes will allow api consumers to provide their own id provider
//
// see https://github.com/graphql-java/graphql-java/pull/276 for the builder pattern
// needed to make this sustainable. But for now we will use a hard coded approach.
//
private final ExecutionIdProvider idProvider = new ExecutionIdProvider() {
@Override
public ExecutionId provide(String query, String operationName, Object context) {
return ExecutionId.generate();
}
};
private static final Logger log = LoggerFactory.getLogger(GraphQL.class);
/**
* A GraphQL object ready to execute queries
*
* @param graphQLSchema the schema to use
*
* @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
*/
public GraphQL(GraphQLSchema graphQLSchema) {
//noinspection deprecation
this(graphQLSchema, null, null);
}
/**
* A GraphQL object ready to execute queries
*
* @param graphQLSchema the schema to use
* @param queryStrategy the query execution strategy to use
*
* @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
*/
public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy) {
//noinspection deprecation
this(graphQLSchema, queryStrategy, null);
}
/**
* A GraphQL object ready to execute queries
*
* @param graphQLSchema the schema to use
* @param queryStrategy the query execution strategy to use
* @param mutationStrategy the mutation execution strategy to use
*
* @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
*/
@SuppressWarnings("DeprecatedIsStillUsed")
public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy, ExecutionStrategy mutationStrategy) {
this.graphQLSchema = graphQLSchema;
this.queryStrategy = queryStrategy;
this.mutationStrategy = mutationStrategy;
}
/**
* Helps you build a GraphQL object ready to execute queries
*
* @param graphQLSchema the schema to use
*
* @return a builder of GraphQL objects
*/
public static Builder newGraphQL(GraphQLSchema graphQLSchema) {
return new Builder(graphQLSchema);
}
public static class Builder {
private GraphQLSchema graphQLSchema;
private ExecutionStrategy queryExecutionStrategy = new SimpleExecutionStrategy();
private ExecutionStrategy mutationExecutionStrategy = new SimpleExecutionStrategy();
public Builder(GraphQLSchema graphQLSchema) {
this.graphQLSchema = graphQLSchema;
}
public Builder schema(GraphQLSchema graphQLSchema) {
this.graphQLSchema = assertNotNull(graphQLSchema, "GraphQLSchema must be non null");
return this;
}
public Builder queryExecutionStrategy(ExecutionStrategy executionStrategy) {
this.queryExecutionStrategy = assertNotNull(executionStrategy, "Query ExecutionStrategy must be non null");
return this;
}
public Builder mutationExecutionStrategy(ExecutionStrategy executionStrategy) {
this.mutationExecutionStrategy = assertNotNull(executionStrategy, "Mutation ExecutionStrategy must be non null");
return this;
}
public GraphQL build() {
//noinspection deprecation
return new GraphQL(graphQLSchema, queryExecutionStrategy, mutationExecutionStrategy);
}
}
public ExecutionResult execute(String requestString) {
return execute(requestString, null);
}
public ExecutionResult execute(String requestString, Object context) {
return execute(requestString, context, Collections.<String, Object>emptyMap());
}
public ExecutionResult execute(String requestString, String operationName, Object context) {
return execute(requestString, operationName, context, Collections.<String, Object>emptyMap());
}
public ExecutionResult execute(String requestString, Object context, Map<String, Object> arguments) {
return execute(requestString, null, context, arguments);
}
public ExecutionResult execute(String requestString, String operationName, Object context, Map<String, Object> arguments) {
assertNotNull(arguments, "arguments can't be null");
log.debug("Executing request. operation name: {}. Request: {} ", operationName, requestString);
Parser parser = new Parser();
Document document;
try {
document = parser.parseDocument(requestString);
} catch (ParseCancellationException e) {
RecognitionException recognitionException = (RecognitionException) e.getCause();
SourceLocation sourceLocation = new SourceLocation(recognitionException.getOffendingToken().getLine(), recognitionException.getOffendingToken().getCharPositionInLine());
InvalidSyntaxError invalidSyntaxError = new InvalidSyntaxError(sourceLocation);
return new ExecutionResultImpl(Collections.singletonList(invalidSyntaxError));
}
Validator validator = new Validator();
List<ValidationError> validationErrors = validator.validateDocument(graphQLSchema, document);
if (validationErrors.size() > 0) {
return new ExecutionResultImpl(validationErrors);
}
ExecutionId executionId = idProvider.provide(requestString, operationName, context);
Execution execution = new Execution(queryStrategy, mutationStrategy);
return execution.execute(executionId, graphQLSchema, context, document, operationName, arguments);
}
}