leangen / graphql-spqr

Build a GraphQL service in seconds
Apache License 2.0
1.09k stars 179 forks source link

Help in Handling Exceptions with SPQR #478

Closed Hardikraja closed 8 months ago

Hardikraja commented 8 months ago

Hello @kaqqao / SPQR team,

I am trying to implement SPQR in my project, it is a great tool that allows running graphql without schema.

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

Thanks.

Hardikraja commented 8 months ago

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. Thanks.