-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Asynchronous execution strategy #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3af03bb
Async execution strategy
dminkovsky dd574c3
Should allow for specific CompletableFuture implementations
bmsantos cfc56c7
Test for null with isNull
dminkovsky 101a9d6
Static import Arrays.asList
dminkovsky 3c4f59f
Removed unnecessary `@SuppressWarnings`
dminkovsky 182845d
`executeInParallel` correctly handles null results
dminkovsky f3c5366
`executeInParallel` handles exceptions
dminkovsky 54d8c02
Removed unnecessary map copy
dminkovsky a585ef7
Capture propagating `NonNullException`
dminkovsky d551919
Cosmetic changes to async strategy spec
dminkovsky 5d88bc5
Fixup after rebase: DataFetchingEnvironment now an interface
dminkovsky 4b063a1
Fixup after rebase: ExecutionContext now has an ExecutionId
dminkovsky 3e4eee5
Should set execution ID
bmsantos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package graphql; | ||
|
|
||
| import graphql.execution.AsyncExecution; | ||
| import graphql.execution.Execution; | ||
| import graphql.execution.ExecutionStrategy; | ||
| 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.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CompletionStage; | ||
|
|
||
| import static graphql.Assert.assertNotNull; | ||
| import static java.util.concurrent.CompletableFuture.completedFuture; | ||
|
|
||
| public class GraphQLAsync extends GraphQL { | ||
|
|
||
| private static Logger log = LoggerFactory.getLogger(GraphQLAsync.class); | ||
|
|
||
| public GraphQLAsync(GraphQLSchema graphQLSchema) { | ||
| super(graphQLSchema); | ||
| } | ||
|
|
||
| public GraphQLAsync(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy) { | ||
| super(graphQLSchema, queryStrategy); | ||
| } | ||
|
|
||
| public GraphQLAsync(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy, ExecutionStrategy mutationStrategy) { | ||
| super(graphQLSchema, queryStrategy, mutationStrategy); | ||
| } | ||
|
|
||
| public CompletionStage<ExecutionResult> executeAsync(String requestString) { | ||
| return executeAsync(requestString, null); | ||
|
|
||
| } | ||
|
|
||
| public CompletionStage<ExecutionResult> executeAsync(String requestString, Object context) { | ||
| return executeAsync(requestString, context, Collections.emptyMap()); | ||
|
|
||
| } | ||
|
|
||
| public CompletionStage<ExecutionResult> executeAsync(String requestString, String operationName, Object context) { | ||
| return executeAsync(requestString, operationName, context, Collections.emptyMap()); | ||
|
|
||
| } | ||
|
|
||
| public CompletionStage<ExecutionResult> executeAsync(String requestString, Object context, Map<String, Object> arguments) { | ||
| return executeAsync(requestString, null, context, arguments); | ||
|
|
||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public CompletionStage<ExecutionResult> executeAsync(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 completedFuture(new ExecutionResultImpl(Arrays.asList(invalidSyntaxError))); | ||
| } | ||
|
|
||
| Validator validator = new Validator(); | ||
| List<ValidationError> validationErrors = validator.validateDocument(graphQLSchema, document); | ||
| if (validationErrors.size() > 0) { | ||
| return completedFuture(new ExecutionResultImpl(validationErrors)); | ||
| } | ||
| AsyncExecution execution = new AsyncExecution(queryStrategy, mutationStrategy); | ||
| return execution.executeAsync(graphQLSchema, context, document, operationName, arguments); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package graphql; | ||
|
|
||
| public class NonNullException extends GraphQLException { | ||
|
|
||
| public NonNullException(String s) { | ||
| super(s); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package graphql.execution; | ||
|
|
||
| import graphql.ExecutionResult; | ||
| import graphql.execution.async.AsyncExecutionStrategy; | ||
| 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.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CompletionStage; | ||
|
|
||
| public class AsyncExecution extends Execution { | ||
|
|
||
| public AsyncExecution(ExecutionStrategy queryStrategy, ExecutionStrategy mutationStrategy) { | ||
| super(queryStrategy, mutationStrategy); | ||
| } | ||
|
|
||
| public CompletionStage<ExecutionResult> executeAsync(GraphQLSchema graphQLSchema, Object root, Document document, String operationName, Map<String, Object> args) { | ||
| ExecutionContextBuilder executionContextBuilder = new ExecutionContextBuilder(new ValuesResolver()); | ||
| ExecutionContext executionContext = executionContextBuilder | ||
| .executionId(ExecutionId.generate()) | ||
| .build(graphQLSchema, queryStrategy, mutationStrategy, root, document, operationName, args); | ||
| return executeOperationAsync(executionContext, root, executionContext.getOperationDefinition()); | ||
| } | ||
|
|
||
| private CompletionStage<ExecutionResult> executeOperationAsync( | ||
| ExecutionContext executionContext, | ||
| Object root, | ||
| OperationDefinition operationDefinition) { | ||
| GraphQLObjectType operationRootType = getOperationRootType(executionContext.getGraphQLSchema(), operationDefinition); | ||
|
|
||
| Map<String, List<Field>> fields = new LinkedHashMap<String, List<Field>>(); | ||
| fieldCollector.collectFields(executionContext, operationRootType, operationDefinition.getSelectionSet(), new ArrayList<String>(), fields); | ||
|
|
||
| if (operationDefinition.getOperation() == OperationDefinition.Operation.MUTATION) { | ||
| return ((AsyncExecutionStrategy) mutationStrategy).executeAsync(executionContext, operationRootType, root, fields); | ||
| } else { | ||
| return ((AsyncExecutionStrategy) queryStrategy).executeAsync(executionContext, operationRootType, root, fields); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be more convenient if it returned
CompletableFuturehere.