Skip to content

Commit 3bbc155

Browse files
committed
A start to graphql-java#324 that provides a promise interface to executing querie
1 parent 7906b13 commit 3bbc155

13 files changed

Lines changed: 278 additions & 213 deletions

src/main/java/graphql/GraphQL.java

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.util.List;
2828
import java.util.Map;
29+
import java.util.concurrent.CompletableFuture;
2930

3031
import static graphql.Assert.assertNotNull;
3132

@@ -49,7 +50,6 @@ public class GraphQL {
4950
* A GraphQL object ready to execute queries
5051
*
5152
* @param graphQLSchema the schema to use
52-
*
5353
* @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
5454
*/
5555
@Internal
@@ -63,7 +63,6 @@ public GraphQL(GraphQLSchema graphQLSchema) {
6363
*
6464
* @param graphQLSchema the schema to use
6565
* @param queryStrategy the query execution strategy to use
66-
*
6766
* @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
6867
*/
6968
@Internal
@@ -78,7 +77,6 @@ public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy) {
7877
* @param graphQLSchema the schema to use
7978
* @param queryStrategy the query execution strategy to use
8079
* @param mutationStrategy the mutation execution strategy to use
81-
*
8280
* @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
8381
*/
8482
@Internal
@@ -93,7 +91,6 @@ public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy, Exe
9391
* @param queryStrategy the query execution strategy to use
9492
* @param mutationStrategy the mutation execution strategy to use
9593
* @param subscriptionStrategy the subscription execution strategy to use
96-
*
9794
* @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
9895
*/
9996
@Internal
@@ -115,7 +112,6 @@ private GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy, Ex
115112
* Helps you build a GraphQL object ready to execute queries
116113
*
117114
* @param graphQLSchema the schema to use
118-
*
119115
* @return a builder of GraphQL objects
120116
*/
121117
public static Builder newGraphQL(GraphQLSchema graphQLSchema) {
@@ -183,9 +179,7 @@ public GraphQL build() {
183179

184180
/**
185181
* @param query the query/mutation/subscription
186-
*
187182
* @return result including errors
188-
*
189183
* @deprecated Use {@link #execute(ExecutionInput)}
190184
*/
191185
@Deprecated
@@ -201,9 +195,7 @@ public ExecutionResult execute(String query) {
201195
*
202196
* @param query the query/mutation/subscription
203197
* @param context custom object provided to each {@link graphql.schema.DataFetcher}
204-
*
205198
* @return result including errors
206-
*
207199
* @deprecated Use {@link #execute(ExecutionInput)}
208200
*/
209201
@Deprecated
@@ -222,9 +214,7 @@ public ExecutionResult execute(String query, Object context) {
222214
* @param query the query/mutation/subscription
223215
* @param operationName the name of the operation to execute
224216
* @param context custom object provided to each {@link graphql.schema.DataFetcher}
225-
*
226217
* @return result including errors
227-
*
228218
* @deprecated Use {@link #execute(ExecutionInput)}
229219
*/
230220
@Deprecated
@@ -244,9 +234,7 @@ public ExecutionResult execute(String query, String operationName, Object contex
244234
* @param query the query/mutation/subscription
245235
* @param context custom object provided to each {@link graphql.schema.DataFetcher}
246236
* @param variables variable values uses as argument
247-
*
248237
* @return result including errors
249-
*
250238
* @deprecated Use {@link #execute(ExecutionInput)}
251239
*/
252240
@Deprecated
@@ -267,9 +255,7 @@ public ExecutionResult execute(String query, Object context, Map<String, Object>
267255
* @param operationName name of the operation to execute
268256
* @param context custom object provided to each {@link graphql.schema.DataFetcher}
269257
* @param variables variable values uses as argument
270-
*
271258
* @return result including errors
272-
*
273259
* @deprecated Use {@link #execute(ExecutionInput)}
274260
*/
275261
@Deprecated
@@ -285,25 +271,34 @@ public ExecutionResult execute(String query, String operationName, Object contex
285271
}
286272

287273
/**
288-
* @param executionInput {@link ExecutionInput}
274+
* This will return a promise (aka {@link CompletableFuture}) to provide a {@link ExecutionResult}
275+
* which is the result of executing the provided query.
289276
*
290-
* @return result including errors
277+
* @param executionInput {@link ExecutionInput}
278+
* @return a promise to an execution result
291279
*/
292-
public ExecutionResult execute(ExecutionInput executionInput) {
280+
public CompletableFuture<ExecutionResult> executeAsync(ExecutionInput executionInput) {
293281
log.debug("Executing request. operation name: {}. query: {}. variables {} ", executionInput.getOperationName(), executionInput.getQuery(), executionInput.getVariables());
294282

295283
InstrumentationContext<ExecutionResult> executionInstrumentation = instrumentation.beginExecution(new InstrumentationExecutionParameters(executionInput));
296-
final ExecutionResult executionResult = parseValidateAndExecute(executionInput);
297-
executionInstrumentation.onEnd(executionResult);
298-
284+
final CompletableFuture<ExecutionResult> executionResult = parseValidateAndExecute(executionInput);
285+
executionResult.thenAccept(executionInstrumentation::onEnd);
299286
return executionResult;
300287
}
301288

302-
private ExecutionResult parseValidateAndExecute(ExecutionInput executionInput) {
289+
/**
290+
* @param executionInput {@link ExecutionInput}
291+
* @return result including errors
292+
*/
293+
public ExecutionResult execute(ExecutionInput executionInput) {
294+
return executeAsync(executionInput).join();
295+
}
296+
297+
private CompletableFuture<ExecutionResult> parseValidateAndExecute(ExecutionInput executionInput) {
303298
PreparsedDocumentEntry preparsedDoc = preparsedDocumentProvider.get(executionInput.getQuery(), query -> parseAndValidate(executionInput));
304299

305300
if (preparsedDoc.hasErrors()) {
306-
return new ExecutionResultImpl(preparsedDoc.getErrors());
301+
return CompletableFuture.completedFuture(new ExecutionResultImpl(preparsedDoc.getErrors()));
307302
}
308303

309304
return execute(executionInput, preparsedDoc.getDocument());
@@ -351,7 +346,7 @@ private List<ValidationError> validate(ExecutionInput executionInput, Document d
351346
return validationErrors;
352347
}
353348

354-
private ExecutionResult execute(ExecutionInput executionInput, Document document) {
349+
private CompletableFuture<ExecutionResult> execute(ExecutionInput executionInput, Document document) {
355350
String query = executionInput.getQuery();
356351
String operationName = executionInput.getOperationName();
357352
Object context = executionInput.getContext();

src/main/java/graphql/execution/Execution.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
import java.util.Collections;
1919
import java.util.List;
2020
import java.util.Map;
21+
import java.util.concurrent.CompletableFuture;
2122

2223
import static graphql.Assert.assertShouldNeverHappen;
2324
import static graphql.execution.ExecutionStrategyParameters.newParameters;
2425
import static graphql.execution.TypeInfo.newTypeInfo;
2526
import static graphql.language.OperationDefinition.Operation.MUTATION;
2627
import static graphql.language.OperationDefinition.Operation.QUERY;
2728
import static graphql.language.OperationDefinition.Operation.SUBSCRIPTION;
29+
import static java.util.concurrent.CompletableFuture.completedFuture;
2830

2931
@Internal
3032
public class Execution {
@@ -42,7 +44,7 @@ public Execution(ExecutionStrategy queryStrategy, ExecutionStrategy mutationStra
4244
this.instrumentation = instrumentation;
4345
}
4446

45-
public ExecutionResult execute(Document document, GraphQLSchema graphQLSchema, ExecutionId executionId, ExecutionInput executionInput) {
47+
public CompletableFuture<ExecutionResult> execute(Document document, GraphQLSchema graphQLSchema, ExecutionId executionId, ExecutionInput executionInput) {
4648

4749
ExecutionContext executionContext = new ExecutionContextBuilder()
4850
.valuesResolver(new ValuesResolver())
@@ -61,7 +63,7 @@ public ExecutionResult execute(Document document, GraphQLSchema graphQLSchema, E
6163
return executeOperation(executionContext, executionInput.getRoot(), executionContext.getOperationDefinition());
6264
}
6365

64-
private ExecutionResult executeOperation(ExecutionContext executionContext, Object root, OperationDefinition operationDefinition) {
66+
private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext executionContext, Object root, OperationDefinition operationDefinition) {
6567

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

@@ -74,7 +76,7 @@ private ExecutionResult executeOperation(ExecutionContext executionContext, Obje
7476
// for the record earlier code has asserted that we have a query type in the schema since the spec says this is
7577
// ALWAYS required
7678
if (operation == MUTATION && operationRootType == null) {
77-
return new ExecutionResultImpl(Collections.singletonList(new MutationNotSupportedError()));
79+
return completedFuture(new ExecutionResultImpl(Collections.singletonList(new MutationNotSupportedError())));
7880
}
7981

8082
FieldCollectorParameters collectorParameters = FieldCollectorParameters.newParameters()
@@ -97,7 +99,7 @@ private ExecutionResult executeOperation(ExecutionContext executionContext, Obje
9799
.path(ExecutionPath.rootPath())
98100
.build();
99101

100-
ExecutionResult result;
102+
CompletableFuture<ExecutionResult> result;
101103
try {
102104
if (operation == OperationDefinition.Operation.MUTATION) {
103105
result = mutationStrategy.execute(executionContext, parameters);
@@ -114,10 +116,11 @@ private ExecutionResult executeOperation(ExecutionContext executionContext, Obje
114116
//
115117
// http://facebook.github.io/graphql/#sec-Errors-and-Non-Nullability
116118
//
117-
result = new ExecutionResultImpl(null, executionContext.getErrors());
119+
result = completedFuture(new ExecutionResultImpl(null, executionContext.getErrors()));
118120
}
119121

120-
dataFetchCtx.onEnd(result);
122+
result.thenAccept(dataFetchCtx::onEnd);
123+
121124
return result;
122125
}
123126

0 commit comments

Comments
 (0)