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
}
- Please show me how can I configure in a way so that I get proper exception message in response.
- To get the access of whole implementation you can refer to this branch -> https://github.com/Hardikraja/graphql-learning/tree/with-spqr
Thanks.
I tried using DataFetcherExceptionHandler
and ResolverInterceptor
too. But, Still getting the same issue.