Repository navigation
Now handles CompletableFuture coming back from an un batched DF #696
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
Merged
bbakerman
merged 4 commits into
graphql-java:master
from
bbakerman:672-683-batched-es-complete-futures
Sep 6, 2017
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a18bd9b
Now handles CompletableFuture coming back from an un batched DF
bbakerman d1b7b4a
Merge remote-tracking branch 'upstream/master' into 672-683-batched-e…
bbakerman 26ec14a
Made it return the signature that was there before
bbakerman fb4bfb0
Now uses Async.each
bbakerman 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 |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| import graphql.ExecutionResultImpl; | ||
| import graphql.GraphQLException; | ||
| import graphql.PublicApi; | ||
| import graphql.execution.Async; | ||
| import graphql.execution.DataFetcherExceptionHandler; | ||
| import graphql.execution.DataFetcherExceptionHandlerParameters; | ||
| import graphql.execution.ExecutionContext; | ||
|
|
@@ -47,7 +48,6 @@ | |
| import java.util.Queue; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.CompletionException; | ||
| import java.util.concurrent.CompletionStage; | ||
| import java.util.function.BiFunction; | ||
| import java.util.stream.IntStream; | ||
|
|
||
|
|
@@ -160,6 +160,7 @@ private void executeImpl(ExecutionContext executionContext, | |
|
|
||
| ExecutionNode finalCurNode = curNode; | ||
| Iterator<String> finalCurFieldNames = curFieldNames; | ||
|
|
||
| resolveField(executionContext, newParameters, fieldName, curNode) | ||
| .whenComplete((childNodes, exception) -> { | ||
| if (exception != null) { | ||
|
|
@@ -201,6 +202,52 @@ private CompletableFuture<List<ExecutionNode>> resolveField(ExecutionContext exe | |
|
|
||
| } | ||
|
|
||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mostly moved this up in the file so its near where it gets called and tweaked the DF fetching to use the Async code |
||
| private CompletableFuture<List<FetchedValue>> fetchData(ExecutionContext executionContext, | ||
| ExecutionStrategyParameters parameters, | ||
| String fieldName, | ||
| ExecutionNode node, | ||
| GraphQLFieldDefinition fieldDef) { | ||
| GraphQLObjectType parentType = node.getType(); | ||
| List<Field> fields = node.getFields().get(fieldName); | ||
| List<MapOrList> parentResults = node.getParentResults(); | ||
|
|
||
| Map<String, Object> argumentValues = valuesResolver.getArgumentValues( | ||
| fieldDef.getArguments(), fields.get(0).getArguments(), executionContext.getVariables()); | ||
|
|
||
| GraphQLOutputType fieldType = fieldDef.getType(); | ||
| DataFetchingFieldSelectionSet fieldCollector = DataFetchingFieldSelectionSetImpl.newCollector(executionContext, fieldType, fields); | ||
|
|
||
| DataFetchingEnvironment environment = newDataFetchingEnvironment(executionContext) | ||
| .source(node.getSources()) | ||
| .arguments(argumentValues) | ||
| .fieldDefinition(fieldDef) | ||
| .fields(fields) | ||
| .fieldType(fieldDef.getType()) | ||
| .fieldTypeInfo(parameters.typeInfo()) | ||
| .parentType(parentType) | ||
| .selectionSet(fieldCollector) | ||
| .build(); | ||
|
|
||
| Instrumentation instrumentation = executionContext.getInstrumentation(); | ||
| InstrumentationContext<Object> fetchCtx = instrumentation.beginFieldFetch( | ||
| new InstrumentationFieldFetchParameters(executionContext, fieldDef, environment) | ||
| ); | ||
|
|
||
| CompletableFuture<Object> fetchedValue; | ||
| try { | ||
| BatchedDataFetcher dataFetcher = getDataFetcher(fieldDef); | ||
| Object fetchedValueRaw = dataFetcher.get(environment); | ||
| fetchedValue = Async.toCompletableFuture(fetchedValueRaw); | ||
| } catch (Exception e) { | ||
| fetchedValue = new CompletableFuture<>(); | ||
| fetchedValue.completeExceptionally(e); | ||
| } | ||
| return fetchedValue | ||
| .thenApply((result) -> assertResult(parentResults, result)) | ||
| .whenComplete(fetchCtx::onEnd) | ||
| .handle(handleResult(executionContext, parameters, parentResults, fields, fieldDef, argumentValues, environment)); | ||
| } | ||
|
|
||
| private List<ExecutionNode> completeValues(ExecutionContext executionContext, GraphQLObjectType parentType, | ||
| List<FetchedValue> fetchedValues, String fieldName, List<Field> fields, | ||
| GraphQLOutputType fieldType, ExecutionTypeInfo typeInfo, Map<String, Object> argumentValues) { | ||
|
|
@@ -373,56 +420,6 @@ private boolean isObject(GraphQLType type) { | |
| type instanceof GraphQLUnionType; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private CompletableFuture<List<FetchedValue>> fetchData(ExecutionContext executionContext, | ||
| ExecutionStrategyParameters parameters, | ||
| String fieldName, | ||
| ExecutionNode node, | ||
| GraphQLFieldDefinition fieldDef) { | ||
| GraphQLObjectType parentType = node.getType(); | ||
| List<Field> fields = node.getFields().get(fieldName); | ||
| List<MapOrList> parentResults = node.getParentResults(); | ||
|
|
||
| Map<String, Object> argumentValues = valuesResolver.getArgumentValues( | ||
| fieldDef.getArguments(), fields.get(0).getArguments(), executionContext.getVariables()); | ||
|
|
||
| GraphQLOutputType fieldType = fieldDef.getType(); | ||
| DataFetchingFieldSelectionSet fieldCollector = DataFetchingFieldSelectionSetImpl.newCollector(executionContext, fieldType, fields); | ||
|
|
||
| DataFetchingEnvironment environment = newDataFetchingEnvironment(executionContext) | ||
| .source(node.getSources()) | ||
| .arguments(argumentValues) | ||
| .fieldDefinition(fieldDef) | ||
| .fields(fields) | ||
| .fieldType(fieldDef.getType()) | ||
| .fieldTypeInfo(parameters.typeInfo()) | ||
| .parentType(parentType) | ||
| .selectionSet(fieldCollector) | ||
| .build(); | ||
|
|
||
| Instrumentation instrumentation = executionContext.getInstrumentation(); | ||
| InstrumentationContext<Object> fetchCtx = instrumentation.beginFieldFetch( | ||
| new InstrumentationFieldFetchParameters(executionContext, fieldDef, environment) | ||
| ); | ||
|
|
||
| CompletableFuture<Object> valuesFuture; | ||
| try { | ||
| Object rawValue = getDataFetcher(fieldDef).get(environment); | ||
| if (rawValue instanceof CompletionStage) { | ||
| valuesFuture = ((CompletionStage) rawValue).toCompletableFuture(); | ||
| } else { | ||
| valuesFuture = CompletableFuture.completedFuture(rawValue); | ||
| } | ||
| } catch (Exception e) { | ||
| valuesFuture = new CompletableFuture<>(); | ||
| valuesFuture.completeExceptionally(e); | ||
| } | ||
| return valuesFuture | ||
| .thenApply((result) -> assertResult(parentResults, result)) | ||
| .whenComplete(fetchCtx::onEnd) | ||
| .handle(handleResult(executionContext, parameters, parentResults, fields, fieldDef, argumentValues, environment)); | ||
| } | ||
|
|
||
| private BiFunction<List<Object>, Throwable, List<FetchedValue>> handleResult(ExecutionContext executionContext, ExecutionStrategyParameters parameters, List<MapOrList> parentResults, List<Field> fields, GraphQLFieldDefinition fieldDef, Map<String, Object> argumentValues, DataFetchingEnvironment environment) { | ||
| return (result, exception) -> { | ||
| if (exception != null) { | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -141,5 +141,4 @@ class AsyncTest extends Specification { | |
| result.isDone() | ||
| result.get() == ['x', 'y', 'z'] | ||
| } | ||
|
|
||
| } | ||
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.
good idea :-)