如何 get/log/capture 来自 graphql apollo 客户端查询的错误

How to get/log/capture the errors from a graphql apollo client query

我正在使用 @apollo/client.

与 graphql 后端进行交互

我发出的请求 returns 是一个 400 错误请求,在网络选项卡中我可以看到 json 错误。

这是我想登录我的代码但我无法做到的。

        try {
            const response = await GraphQLClient.query({
                query: GET_PERSON,
                variables: {
                    personId: id,
                },
                errorPolicy: "all",
            });
            console.log("response", response);
        } catch (err) {
            console.log("err", err);
        }

当我执行上面的代码时,它进入了 catch 块,我无法访问错误对象。

err Error: Response not successful: Received status code 400 at new ApolloError (index.ts:54) at QueryManager.ts:1073 at both (asyncMap.ts:30) at asyncMap.ts:19 at new Promise () at Object.then (asyncMap.ts:19) at Object.error (asyncMap.ts:31) at notifySubscription (module.js:137) at onNotify (module.js:176) at SubscriptionObserver.error (module.js:229) at iteration.ts:13 at Array.forEach () at iterateObserversSafely (iteration.ts:13) at Object.error (Concast.ts:185) at notifySubscription (module.js:137) at onNotify (module.js:176) at SubscriptionObserver.error (module.js:229) at createHttpLink.ts:203

graphqlservice

import { ApolloClient, InMemoryCache } from "@apollo/client";
import { Config } from "./../config";

const FRONTEND_API = `${Config.frontend_api}/graphql` || "";

export const GraphQLClient = new ApolloClient({
    uri: FRONTEND_API,
    cache: new InMemoryCache(),
}

在 catch 方法中将错误作为 json 响应获取。

console.log(err.networkError.result.errors);

仍然非常不确定为什么响应对象有 errorerrors 属性,我不知道这些什么时候可以访问,也许其他人可以对此有所了解.

export declare type ApolloQueryResult<T> = {
    data: T;
    errors?: ReadonlyArray<GraphQLError>;
    error?: ApolloError;
    loading: boolean;
    networkStatus: NetworkStatus;
    partial?: boolean;
};