Interface DataFetcherExceptionResolver

All Known Subinterfaces:
HandlerDataFetcherExceptionResolver
All Known Implementing Classes:
DataFetcherExceptionResolverAdapter, ReactiveSecurityDataFetcherExceptionResolver, SecurityDataFetcherExceptionResolver

public interface DataFetcherExceptionResolver
Contract to resolve exceptions from DataFetchers. Resolves are typically declared as Spring beans and invoked in turn until one resolves the exception by emitting a (possibly empty) GraphQLError list. Use the static factory method createExceptionHandler(java.util.List<org.springframework.graphql.execution.DataFetcherExceptionResolver>) to create a DataFetcherExceptionHandler from a list of resolvers.

Resolver implementations can extend DataFetcherExceptionResolverAdapter and override one of its resolveToSingleError or resolveToMultipleErrors methods that resolve the exception synchronously.

Resolver implementations can use ErrorType to classify errors using one of several common categories.

Since:
1.0.0
Author:
Rossen Stoyanchev
See Also:
  • Method Details

    • resolveException

      reactor.core.publisher.Mono<List<graphql.GraphQLError>> resolveException(Throwable exception, graphql.schema.DataFetchingEnvironment environment)
      Resolve the given exception and return the error(s) to add to the response.

      Implementations can use GraphqlErrorBuilder.newError(DataFetchingEnvironment) to create an error with the coordinates of the target field, and use ErrorType to specify a category for the error.

      Parameters:
      exception - the exception to resolve
      environment - the environment for the invoked DataFetcher
      Returns:
      a Mono with errors to add to the GraphQL response; if the Mono completes with an empty List, the exception is resolved without any errors added to the response; if the Mono completes empty, without emitting a List, the exception remains unresolved and that allows other resolvers to resolve it.
    • forSingleError

      static DataFetcherExceptionResolverAdapter forSingleError(BiFunction<Throwable, graphql.schema.DataFetchingEnvironment, graphql.GraphQLError> resolver)
      Factory method to create a DataFetcherExceptionResolver to resolve an exception to a single GraphQL error. Effectively, a shortcut for creating DataFetcherExceptionResolverAdapter and overriding its resolveToSingleError method.
      Parameters:
      resolver - the resolver function to use
      Returns:
      the created instance
      Since:
      1.0.1
    • createExceptionHandler

      static graphql.execution.DataFetcherExceptionHandler createExceptionHandler(List<DataFetcherExceptionResolver> resolvers)
      Factory method to create a DataFetcherExceptionHandler from a list of DataFetcherExceptionResolver's. This is used internally in AbstractGraphQlSourceBuilder to set the exception handler on GraphQL.Builder, which in turn is used to create ExecutionStrategy's. Applications may also use this method to create an exception handler when they to need to initialize a custom ExecutionStrategy.

      Resolvers are invoked in turn until one resolves the exception by emitting a (possibly empty) GraphQLError list. If the exception remains unresolved, the handler creates a GraphQLError with ErrorType.INTERNAL_ERROR and a short message with the execution id.

      Parameters:
      resolvers - the list of resolvers to use
      Returns:
      the created DataFetcherExceptionHandler instance
      Since:
      1.1.1