From 0bd30582e5cbb60682b80ce08d536f83e4a4577e Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Thu, 8 Dec 2022 14:43:10 +0000 Subject: [PATCH] Add factory method for DataFetcherExceptionHandler Closes gh-552 --- .../AbstractGraphQlSourceBuilder.java | 3 ++- .../DataFetcherExceptionResolver.java | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java index 9bd94ea1..130c689f 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java @@ -105,7 +105,8 @@ abstract class AbstractGraphQlSourceBuilder> schema = applyTypeVisitors(schema); GraphQL.Builder builder = GraphQL.newGraphQL(schema); - builder.defaultDataFetcherExceptionHandler(new ExceptionResolversExceptionHandler(this.exceptionResolvers)); + builder.defaultDataFetcherExceptionHandler( + DataFetcherExceptionResolver.createExceptionHandler(this.exceptionResolvers)); if (!this.instrumentations.isEmpty()) { builder = builder.instrumentation(new ChainedInstrumentation(this.instrumentations)); diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/DataFetcherExceptionResolver.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/DataFetcherExceptionResolver.java index a34ad133..724e08ad 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/execution/DataFetcherExceptionResolver.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/DataFetcherExceptionResolver.java @@ -20,6 +20,7 @@ import java.util.List; import java.util.function.BiFunction; import graphql.GraphQLError; +import graphql.execution.DataFetcherExceptionHandler; import graphql.schema.DataFetchingEnvironment; import reactor.core.publisher.Mono; @@ -83,4 +84,24 @@ public interface DataFetcherExceptionResolver { }; } + /** + * Factory method to create a {@link DataFetcherExceptionResolver} from a + * list of resolvers. Spring for GraphQL uses this method to set + * {@link graphql.GraphQL.Builder#defaultDataFetcherExceptionHandler(DataFetcherExceptionHandler)} + * from resolvers found in Spring configuration, and that default handler + * is used in turn to create each {@code ExecutionStrategy}. Applications + * may also find this factory method useful when creating a custom + * {@code ExecutionStrategy}. + *

Resolvers are invoked in turn until one resolves the exception by + * emitting a (possibly empty) {@code GraphQLError} list. If the exception + * remains unresolved, the handler creates a {@code GraphQLError} with + * {@link ErrorType#INTERNAL_ERROR} and a short message with the execution id. + * @param resolvers the list of resolvers to use + * @return the created {@link DataFetcherExceptionHandler} instance + * @since 1.1.1 + */ + static DataFetcherExceptionHandler createExceptionHandler(List resolvers) { + return new ExceptionResolversExceptionHandler(resolvers); + } + }