Add factory method for DataFetcherExceptionHandler

Closes gh-552
This commit is contained in:
rstoyanchev
2022-12-08 14:43:10 +00:00
parent 75d5d4b33a
commit 0bd30582e5
2 changed files with 23 additions and 1 deletions

View File

@@ -105,7 +105,8 @@ abstract class AbstractGraphQlSourceBuilder<B extends GraphQlSource.Builder<B>>
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));

View File

@@ -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}.
* <p>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<DataFetcherExceptionResolver> resolvers) {
return new ExceptionResolversExceptionHandler(resolvers);
}
}