Handling Exceptions not working with SPQR (graphql)

Handling Exceptions not working with SPQR (graphql)


0

I am having an issue in configuring exception handling with this. I went through all the issues/examples and found some solutions. But don’t know that is not working.

See, here is how I configured my graphQL

@Autowired
    public GraphqlController(BookResolver bookResolver) {
        GraphQLSchema schema = new GraphQLSchemaGenerator()
                .withBasePackages("com.graphql.learn")
                .withOperationsFromSingleton(bookResolver)
                .generate();
        this.graphQL = new GraphQL.Builder(schema)
                .queryExecutionStrategy(new AsyncExecutionStrategy(new CustomExceptionHandler()))
                .mutationExecutionStrategy(new AsyncExecutionStrategy(new CustomExceptionHandler()))
                .build();
    }
    @PostMapping(value = "/graphql")
    public Map<String, Object> execute(@RequestBody Map<String, String> request, HttpServletRequest raw)
            throws GraphQLException {
        ExecutionResult result = graphQL.execute(request.get("query"));
        return result.getData();
    }

This is the resolver I made (Here, I am intentionally throwing an exception)

@GraphQLQuery
    public List<Book> books(){
        if(true){
            throw new NullPointerException("Null pointer exception");
        }
        return bookService.getAll();
    }

And Here is how I created CustomExceptionHandler

 @Component
public class CustomExceptionHandler implements DataFetcherExceptionHandler {

    private DataFetcherExceptionHandlerResult onExceptionHandler(DataFetcherExceptionHandlerParameters handlerParameters) {

        Throwable exception = handlerParameters.getException();

        // do something with exception

        GraphQLError error = GraphqlErrorBuilder
                .newError()
                .message(exception.getMessage())
                .build();

        return DataFetcherExceptionHandlerResult
                .newResult()
                .error(error)
                .build();
    }

    @Override
    public CompletableFuture<DataFetcherExceptionHandlerResult> handleException(DataFetcherExceptionHandlerParameters handlerParameters) {
        DataFetcherExceptionHandlerResult result = onExceptionHandler(handlerParameters);
        return CompletableFuture.completedFuture(result);
    }
}

**And Here What I get for the query result **

  • Request
query{
    books{
        id,
        title
    }
}
  • Response
{
    "books": null
}

Thanks.

I tried using DataFetcherExceptionHandler and ResolverInterceptor too. But, Still getting the same issue.


Load 4 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *