如何在 GraphQL apollo-android 客户端中获取 HTTP 状态码

问题描述

我正在使用 apollo 客户端 https://github.com/apollographql/apollo-android 查询 Graphql API。在 Graphql Server 中,我们以 HTTPS 状态返回

return Response.status( Status.BAD_REQUEST ).entity( exception.getMessage() ).build();

如何在客户端获取 HTTPS 状态代码作为响应?

客户端代码

ApolloCall<T> apolloCall = apolloClient.query( query );
Response<T> response = Rx2Apollo.from( apolloCall ).onErrorReturn( throwable -> null ).blockingFirst();

我主要想知道错误是客户端异常还是服务器异常。

解决方法

您可以删除 .onErrorReturn( throwable -> null ) 部分,HTTP 错误将在 onError 中作为 ApolloHttpException 返回,您可以在其上调用 .code() 以获取 HTTP 代码。