javagraphqlgraphql-java

How to override DataFetcherExceptionHandler in GraphQL Java?


I am using the following starter for Graphql integration

<dependency>
    <groupId>com.graphql-java-kickstart</groupId>
    <artifactId>graphql-spring-boot-starter</artifactId>
    <version>6.0.1</version>
</dependency>
  1. I am not sure how to override the SimpleDataFetcherExceptionHandler with my own CustomExceptionHandler. The library already autowires a lot of stuff. Do I need to create a separate configuration for graphQl object? The documentation is not much helpful.

  2. I also tried to integrate @ControllerAdvice to my Graphql java spring boot application but the errors are not matched by the exception handlers inside it. They are handled by GraphQl error handler. How does error propagates inside Graphql?

How can I change this behaviour?


Solution

  • I have this and it's working for me. Pretty much regular spring stuff for exception handling, but there is no @ControllerAdvice (it's kotlin btw, if you can't understand - let me know, but I think it's pretty clear even without kotlin knowledge).

    import org.springframework.web.bind.annotation.ExceptionHandler
    import com.oembedler.moon.graphql.boot.error.ThrowableGraphQLError
    
    internal class GraphQLExceptionHandler {
        @ExceptionHandler(AppException::class)
        fun handleGenericException(ex: AppException): ThrowableGraphQLError {
            return ThrowableGraphQLError(ex)
        }
    
        @ExceptionHandler(Exception::class)
        fun handleGenericException(ex: Exception): ThrowableGraphQLError {
            return ThrowableGraphQLError(TechnicalException())
        }
    }
    
    @Configuration
    internal open class GraphQLConfiguration {
        @Bean
        open fun graphQLExceptionHandler(): GraphQLExceptionHandler {
            return GraphQLExceptionHandler()
        }
    }
    

    I have quite a few dependencies tho, in case they matter here:

    <graphql.version>5.4.1</graphql.version>
    <graphql-datetime-spring-boot-starter.version>1.4.0</graphql-datetime-spring-boot-starter.version>
    
    <dependency>
      <groupId>com.graphql-java-kickstart</groupId>
      <artifactId>graphql-spring-boot-starter</artifactId>
      <version>${graphql.version}</version>
    </dependency>
    <dependency>
      <groupId>com.graphql-java-kickstart</groupId>
      <artifactId>graphiql-spring-boot-starter</artifactId>
      <version>${graphql.version}</version>
    </dependency>
    <dependency>
      <groupId>com.graphql-java-kickstart</groupId>
      <artifactId>voyager-spring-boot-starter</artifactId>
      <version>${graphql.version}</version>
    </dependency>
    <dependency>
      <groupId>com.graphql-java-kickstart</groupId>
      <artifactId>graphql-java-tools</artifactId>
      <version>${graphql.version}</version>
    </dependency>
    <dependency>
      <groupId>com.zhokhov.graphql</groupId>
      <artifactId>graphql-datetime-spring-boot-starter</artifactId>
      <version>${graphql-datetime-spring-boot-starter.version}</version>
    </dependency>