如何处理 graphQL API 中的错误?我正在使用 graphql-java-tools 和 graphql-spring-boot-starter。我创建了错误处理程序,但每次我都会收到响应 200,即使抛出异常也是如此。你能告诉我应该如何设置错误代码(例如 400)作为响应吗?
@Component
public class CustomGraphQLErrorHandler implements GraphQLErrorHandler {
@Override
public List<GraphQLError> processErrors(List<GraphQLError> list) {
return list.stream().map(this::getNested).collect(Collectors.toList());
}
private GraphQLError getNested(GraphQLError error) {
if (error instanceof ExceptionWhileDataFetching) {
ExceptionWhileDataFetching exceptionError = (ExceptionWhileDataFetching) error;
if (exceptionError.getException() instanceof GraphQLError) {
return (GraphQLError) exceptionError.getException();
}
}
return error;
}
}
Run Code Online (Sandbox Code Playgroud)