javaspring-bootgraphqlgraphql-spqr

Handling Exceptions not working with SPQR (graphql)


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 **

query{
    books{
        id,
        title
    }
}
{
    "books": null
}

Thanks.

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


Solution

  • I found the answer of the above question,

    @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; // it should return executionResult as it is
        }
    

    Earlier, I was returning executionResult.getData() instead of executionResult - that is why it was not working.