A.S*_*dor 6 java spring graphql graphql-java-tools
如何处理 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)
当 GraphQL 服务器可以接受请求时,它会返回 HTTP 200(语法有效,服务器已启动...)。
如果发生错误,它将返回 200 并填充响应中的错误列表。
因此,在客户端,您将拥有:
| 归档时间: |
|
| 查看次数: |
7560 次 |
| 最近记录: |