Skip to content

Commit 0bd3058

Browse files
committed
Add factory method for DataFetcherExceptionHandler
Closes gh-552
1 parent 75d5d4b commit 0bd3058

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ public GraphQlSource build() {
105105
schema = applyTypeVisitors(schema);
106106

107107
GraphQL.Builder builder = GraphQL.newGraphQL(schema);
108-
builder.defaultDataFetcherExceptionHandler(new ExceptionResolversExceptionHandler(this.exceptionResolvers));
108+
builder.defaultDataFetcherExceptionHandler(
109+
DataFetcherExceptionResolver.createExceptionHandler(this.exceptionResolvers));
109110

110111
if (!this.instrumentations.isEmpty()) {
111112
builder = builder.instrumentation(new ChainedInstrumentation(this.instrumentations));

spring-graphql/src/main/java/org/springframework/graphql/execution/DataFetcherExceptionResolver.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.function.BiFunction;
2121

2222
import graphql.GraphQLError;
23+
import graphql.execution.DataFetcherExceptionHandler;
2324
import graphql.schema.DataFetchingEnvironment;
2425
import reactor.core.publisher.Mono;
2526

@@ -83,4 +84,24 @@ protected GraphQLError resolveToSingleError(Throwable ex, DataFetchingEnvironmen
8384
};
8485
}
8586

87+
/**
88+
* Factory method to create a {@link DataFetcherExceptionResolver} from a
89+
* list of resolvers. Spring for GraphQL uses this method to set
90+
* {@link graphql.GraphQL.Builder#defaultDataFetcherExceptionHandler(DataFetcherExceptionHandler)}
91+
* from resolvers found in Spring configuration, and that default handler
92+
* is used in turn to create each {@code ExecutionStrategy}. Applications
93+
* may also find this factory method useful when creating a custom
94+
* {@code ExecutionStrategy}.
95+
* <p>Resolvers are invoked in turn until one resolves the exception by
96+
* emitting a (possibly empty) {@code GraphQLError} list. If the exception
97+
* remains unresolved, the handler creates a {@code GraphQLError} with
98+
* {@link ErrorType#INTERNAL_ERROR} and a short message with the execution id.
99+
* @param resolvers the list of resolvers to use
100+
* @return the created {@link DataFetcherExceptionHandler} instance
101+
* @since 1.1.1
102+
*/
103+
static DataFetcherExceptionHandler createExceptionHandler(List<DataFetcherExceptionResolver> resolvers) {
104+
return new ExceptionResolversExceptionHandler(resolvers);
105+
}
106+
86107
}

0 commit comments

Comments
 (0)