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
43 changes: 19 additions & 24 deletions src/main/java/graphql/GraphQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import static graphql.Assert.assertNotNull;

Expand All @@ -49,7 +50,6 @@ public class GraphQL {
* 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.
*/
@Internal
Expand All @@ -63,7 +63,6 @@ public GraphQL(GraphQLSchema graphQLSchema) {
*
* @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.
*/
@Internal
Expand All @@ -78,7 +77,6 @@ public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy) {
* @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.
*/
@Internal
Expand All @@ -93,7 +91,6 @@ public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy, Exe
* @param queryStrategy the query execution strategy to use
* @param mutationStrategy the mutation execution strategy to use
* @param subscriptionStrategy the subscription execution strategy to use
*
* @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
*/
@Internal
Expand All @@ -115,7 +112,6 @@ private GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy, Ex
* 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) {
Expand Down Expand Up @@ -183,9 +179,7 @@ public GraphQL build() {

/**
* @param query the query/mutation/subscription
*
* @return result including errors
*
* @deprecated Use {@link #execute(ExecutionInput)}
*/
@Deprecated
Expand All @@ -201,9 +195,7 @@ public ExecutionResult execute(String query) {
*
* @param query the query/mutation/subscription
* @param context custom object provided to each {@link graphql.schema.DataFetcher}
*
* @return result including errors
*
* @deprecated Use {@link #execute(ExecutionInput)}
*/
@Deprecated
Expand All @@ -222,9 +214,7 @@ public ExecutionResult execute(String query, Object context) {
* @param query the query/mutation/subscription
* @param operationName the name of the operation to execute
* @param context custom object provided to each {@link graphql.schema.DataFetcher}
*
* @return result including errors
*
* @deprecated Use {@link #execute(ExecutionInput)}
*/
@Deprecated
Expand All @@ -244,9 +234,7 @@ public ExecutionResult execute(String query, String operationName, Object contex
* @param query the query/mutation/subscription
* @param context custom object provided to each {@link graphql.schema.DataFetcher}
* @param variables variable values uses as argument
*
* @return result including errors
*
* @deprecated Use {@link #execute(ExecutionInput)}
*/
@Deprecated
Expand All @@ -267,9 +255,7 @@ public ExecutionResult execute(String query, Object context, Map<String, Object>
* @param operationName name of the operation to execute
* @param context custom object provided to each {@link graphql.schema.DataFetcher}
* @param variables variable values uses as argument
*
* @return result including errors
*
* @deprecated Use {@link #execute(ExecutionInput)}
*/
@Deprecated
Expand All @@ -285,25 +271,34 @@ public ExecutionResult execute(String query, String operationName, Object contex
}

/**
* @param executionInput {@link ExecutionInput}
* This will return a promise (aka {@link CompletableFuture}) to provide a {@link ExecutionResult}
* which is the result of executing the provided query.
*
* @return result including errors
* @param executionInput {@link ExecutionInput}
* @return a promise to an execution result
*/
public ExecutionResult execute(ExecutionInput executionInput) {
public CompletableFuture<ExecutionResult> executeAsync(ExecutionInput executionInput) {
log.debug("Executing request. operation name: {}. query: {}. variables {} ", executionInput.getOperationName(), executionInput.getQuery(), executionInput.getVariables());

InstrumentationContext<ExecutionResult> executionInstrumentation = instrumentation.beginExecution(new InstrumentationExecutionParameters(executionInput));
final ExecutionResult executionResult = parseValidateAndExecute(executionInput);
executionInstrumentation.onEnd(executionResult);

final CompletableFuture<ExecutionResult> executionResult = parseValidateAndExecute(executionInput);
executionResult.thenAccept(executionInstrumentation::onEnd);
return executionResult;
}

private ExecutionResult parseValidateAndExecute(ExecutionInput executionInput) {
/**
* @param executionInput {@link ExecutionInput}
* @return result including errors
*/
public ExecutionResult execute(ExecutionInput executionInput) {
return executeAsync(executionInput).join();
}

private CompletableFuture<ExecutionResult> parseValidateAndExecute(ExecutionInput executionInput) {
PreparsedDocumentEntry preparsedDoc = preparsedDocumentProvider.get(executionInput.getQuery(), query -> parseAndValidate(executionInput));

if (preparsedDoc.hasErrors()) {
return new ExecutionResultImpl(preparsedDoc.getErrors());
return CompletableFuture.completedFuture(new ExecutionResultImpl(preparsedDoc.getErrors()));
}

return execute(executionInput, preparsedDoc.getDocument());
Expand Down Expand Up @@ -351,7 +346,7 @@ private List<ValidationError> validate(ExecutionInput executionInput, Document d
return validationErrors;
}

private ExecutionResult execute(ExecutionInput executionInput, Document document) {
private CompletableFuture<ExecutionResult> execute(ExecutionInput executionInput, Document document) {
String query = executionInput.getQuery();
String operationName = executionInput.getOperationName();
Object context = executionInput.getContext();
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/graphql/execution/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import static graphql.Assert.assertShouldNeverHappen;
import static graphql.execution.ExecutionStrategyParameters.newParameters;
import static graphql.execution.TypeInfo.newTypeInfo;
import static graphql.language.OperationDefinition.Operation.MUTATION;
import static graphql.language.OperationDefinition.Operation.QUERY;
import static graphql.language.OperationDefinition.Operation.SUBSCRIPTION;
import static java.util.concurrent.CompletableFuture.completedFuture;

@Internal
public class Execution {
Expand All @@ -42,7 +44,7 @@ public Execution(ExecutionStrategy queryStrategy, ExecutionStrategy mutationStra
this.instrumentation = instrumentation;
}

public ExecutionResult execute(Document document, GraphQLSchema graphQLSchema, ExecutionId executionId, ExecutionInput executionInput) {
public CompletableFuture<ExecutionResult> execute(Document document, GraphQLSchema graphQLSchema, ExecutionId executionId, ExecutionInput executionInput) {

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

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

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

Expand All @@ -74,7 +76,7 @@ private ExecutionResult executeOperation(ExecutionContext executionContext, Obje
// 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()));
return completedFuture(new ExecutionResultImpl(Collections.singletonList(new MutationNotSupportedError())));
}

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

ExecutionResult result;
CompletableFuture<ExecutionResult> result;
try {
if (operation == OperationDefinition.Operation.MUTATION) {
result = mutationStrategy.execute(executionContext, parameters);
Expand All @@ -114,10 +116,11 @@ private ExecutionResult executeOperation(ExecutionContext executionContext, Obje
//
// http://facebook.github.io/graphql/#sec-Errors-and-Non-Nullability
//
result = new ExecutionResultImpl(null, executionContext.getErrors());
result = completedFuture(new ExecutionResultImpl(null, executionContext.getErrors()));
}

dataFetchCtx.onEnd(result);
result.thenAccept(dataFetchCtx::onEnd);

return result;
}

Expand Down
Loading