Spring boot graphql supports Spring web's @ExceptionHandler annotations.
To enable this we first need to set the graphql property: graphql.servlet.exception-handlers-enabled: true. We can then proceed to create a component with @ExceptionHandler methods, that will transform an exception into a GraphQLError implementation. This is the hook point where you can place your exception handling logic. In this example, I transform the exceptions into a simple ThrowableGraphQLError, but many are available in the library or you have the option to customize your own.
With this approach I recommend you create an "catch all" exception handler. This will ensure any spring, feign, jooq, hibernate etc runtime exception messages are not propagated to the client, but instead are shown a internal server error message. To sum up, if its not defined handled exception type, then show internal server error. Lets not leak info.
By default, graphql will create a DefaultGraphQLErrorHandler bean. This bean will be invoked with a list of all the graphql exceptions/errors that occurred during the query. The bean will then try to find a matching ExceptionHandler for the error's exception. If it finds a matching handler, it will then swap its wrapped error (containing the real exception) with your ErrorHandlers response object.
See you in the next video. Spring Boot GraphQL Exception Handling with a custom GraphQLErrorHandler implementation.
Cheers!
Philip
GraphQL Spring Boot: https://github.com/graphql-java-kicks...
Spring Boot ExceptionHandler: https://docs.spring.io/spring-framewo...
Thumbnail image from: https://proandroiddev.com/android-err...
Nesta página do site você pode assistir ao vídeo on-line Spring Boot GraphQL Tutorial #8 - Exception Handling with ExceptionHandler duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Philip Starritt 16 Setembro 2020, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 17,571 vezes e gostou 102 espectadores. Boa visualização!