-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Adding support for making the exception handlers async #2371
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
271bf3a
Adding support for making the exception handlers async
bbakerman 86f1b21
Better function naming here
bbakerman cf3cb83
MAde it return completable future
bbakerman 9a71b50
Addressing contention on error list - moving away from synchronised list
bbakerman 0d54737
ImmutableList handles empty list efficiently
bbakerman e7f5a91
moved to AtomicRef + ImmutableList
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
27 changes: 25 additions & 2 deletions
27
src/main/java/graphql/execution/DataFetcherExceptionHandler.java
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 |
|---|---|---|
| @@ -1,22 +1,45 @@ | ||
| package graphql.execution; | ||
|
|
||
| import graphql.ExecutionResult; | ||
| import graphql.PublicSpi; | ||
| import graphql.schema.DataFetcher; | ||
| import graphql.schema.DataFetchingEnvironment; | ||
|
|
||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.CompletionStage; | ||
|
|
||
| /** | ||
| * This is called when an exception is thrown during {@link graphql.schema.DataFetcher#get(DataFetchingEnvironment)} execution | ||
| */ | ||
| @PublicSpi | ||
| public interface DataFetcherExceptionHandler { | ||
|
|
||
| /** | ||
| * When an exception during a call to a {@link DataFetcher} then this handler | ||
| * is called back to shape the error that should be placed in the list of errors | ||
| * When an exception occurs during a call to a {@link DataFetcher} then this handler | ||
| * is called to shape the errors that should be placed in the {@link ExecutionResult#getErrors()} | ||
| * list of errors. | ||
| * | ||
| * @param handlerParameters the parameters to this callback | ||
| * | ||
| * @return a result that can contain custom formatted {@link graphql.GraphQLError}s | ||
| * | ||
| * @deprecated use {@link #handleException(DataFetcherExceptionHandlerParameters)} instead which as an asynchronous | ||
| * version | ||
| */ | ||
| @Deprecated | ||
| DataFetcherExceptionHandlerResult onException(DataFetcherExceptionHandlerParameters handlerParameters); | ||
|
|
||
| /** | ||
| * When an exception occurs during a call to a {@link DataFetcher} then this handler | ||
| * is called to shape the errors that should be placed in the {@link ExecutionResult#getErrors()} | ||
| * list of errors. | ||
| * | ||
| * @param handlerParameters the parameters to this callback | ||
| * | ||
| * @return a result that can contain custom formatted {@link graphql.GraphQLError}s | ||
| */ | ||
| default CompletableFuture<DataFetcherExceptionHandlerResult> handleException(DataFetcherExceptionHandlerParameters handlerParameters) { | ||
| DataFetcherExceptionHandlerResult result = onException(handlerParameters); | ||
| return CompletableFuture.completedFuture(result); | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -48,6 +48,7 @@ | |
| import java.util.OptionalInt; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.CompletionException; | ||
| import java.util.function.Function; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import static graphql.execution.Async.exceptionallyCompletedFuture; | ||
|
|
@@ -290,12 +291,12 @@ protected CompletableFuture<FetchedValue> fetchField(ExecutionContext executionC | |
| .handle((result, exception) -> { | ||
| fetchCtx.onCompleted(result, exception); | ||
| if (exception != null) { | ||
| handleFetchingException(executionContext, environment, exception); | ||
| return null; | ||
| return handleFetchingException(executionContext, environment, exception); | ||
| } else { | ||
| return result; | ||
| return CompletableFuture.completedFuture(result); | ||
| } | ||
| }) | ||
| .thenCompose(Function.identity()) | ||
| .thenApply(result -> unboxPossibleDataFetcherResult(executionContext, parameters, result)); | ||
|
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. we need to flat map the above |
||
| } | ||
|
|
||
|
|
@@ -309,9 +310,8 @@ protected FetchedValue unboxPossibleDataFetcherResult(ExecutionContext execution | |
| Object result) { | ||
|
|
||
| if (result instanceof DataFetcherResult) { | ||
| //noinspection unchecked | ||
| DataFetcherResult<?> dataFetcherResult = (DataFetcherResult) result; | ||
| dataFetcherResult.getErrors().forEach(executionContext::addError); | ||
| DataFetcherResult<?> dataFetcherResult = (DataFetcherResult<?>) result; | ||
| executionContext.addErrors(dataFetcherResult.getErrors()); | ||
|
|
||
| Object localContext = dataFetcherResult.getLocalContext(); | ||
| if (localContext == null) { | ||
|
|
@@ -333,26 +333,35 @@ protected FetchedValue unboxPossibleDataFetcherResult(ExecutionContext execution | |
| } | ||
| } | ||
|
|
||
| protected void handleFetchingException(ExecutionContext executionContext, | ||
| DataFetchingEnvironment environment, | ||
| Throwable e) { | ||
| protected <T> CompletableFuture<T> handleFetchingException(ExecutionContext executionContext, | ||
| DataFetchingEnvironment environment, | ||
| Throwable e) { | ||
| DataFetcherExceptionHandlerParameters handlerParameters = DataFetcherExceptionHandlerParameters.newExceptionParameters() | ||
| .dataFetchingEnvironment(environment) | ||
| .exception(e) | ||
| .build(); | ||
|
|
||
| DataFetcherExceptionHandlerResult handlerResult; | ||
| try { | ||
| handlerResult = dataFetcherExceptionHandler.onException(handlerParameters); | ||
| return asyncHandleException(dataFetcherExceptionHandler, handlerParameters, executionContext); | ||
| } catch (Exception handlerException) { | ||
| handlerParameters = DataFetcherExceptionHandlerParameters.newExceptionParameters() | ||
| .dataFetchingEnvironment(environment) | ||
| .exception(handlerException) | ||
| .build(); | ||
| handlerResult = new SimpleDataFetcherExceptionHandler().onException(handlerParameters); | ||
| return asyncHandleException(new SimpleDataFetcherExceptionHandler(), handlerParameters, executionContext); | ||
| } | ||
| handlerResult.getErrors().forEach(executionContext::addError); | ||
| } | ||
|
|
||
| private <T> CompletableFuture<T> asyncHandleException(DataFetcherExceptionHandler handler, DataFetcherExceptionHandlerParameters handlerParameters, ExecutionContext executionContext) { | ||
| return handler.handleException(handlerParameters) | ||
| .thenApply(handlerResult -> { | ||
| // the side effect is that we added the returned errors to the execution context | ||
| // here | ||
| executionContext.addErrors(handlerResult.getErrors()); | ||
| // and we return null because there is no data for the executed field | ||
| return null; | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -706,7 +715,6 @@ private void handleTypeMismatchProblem(ExecutionContext context, ExecutionStrate | |
| TypeMismatchError error = new TypeMismatchError(parameters.getPath(), parameters.getExecutionStepInfo().getUnwrappedNonNullType()); | ||
| logNotSafe.warn("{} got {}", error.getMessage(), result.getClass()); | ||
| context.addError(error); | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
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.
The consuming code only calls this method. It delegates back to the old sync method. So any current implementations will continue to work.
Later at some point we can remove the deprecated version for a breaking change