简体   繁体   English

如何使用 React Apollo Link Rest 获取错误正文

[英]How to get error body using React Apollo Link Rest

I am creating an React app using Apollo Graphql and I am having some trouble with the Apollo Link Rest.我正在使用 Apollo Graphql 创建一个 React 应用程序,但我在使用 Apollo Link Rest 时遇到了一些问题。 The problem is that in the middle of my app I have to make a REST request, however this request can be sucessful and return a status code = 200 or it can fail, returning a status code = 400.问题是,在我的应用程序中间,我必须发出 REST 请求,但是此请求可能成功并返回状态码 = 200,也可能失败,返回状态码 = 400。

It is important for my app to show the end user an error message that is returned in this request's body in case of a 400 error.如果出现 400 错误,我的应用程序必须向最终用户显示在此请求正文中返回的错误消息。 However when an error happens the Apollo Link Rest just throws an exception, but it doesn't return response body back to me.但是,当发生错误时,Apollo Link Rest 只会引发异常,但不会将响应正文返回给我。

Is there a way for me to get the response body, when there is an error with the Apollo Link Rest?当 Apollo Link Rest 出现错误时,我有办法获取响应正文吗? I thought that I could get it from the result variable, but since the Apollo throws an exception, this variable never changes.我以为我可以从result变量中得到它,但是由于 Apollo 抛出了异常,所以这个变量永远不会改变。

Here is my code:这是我的代码:

    const result = await context.client.query<MyQuery>({
      query: MyQuery,
      variables: {
        input: {
          companyId: variables.companyId,
        },
      },
    });
query MyQuery($input: MyQueryInput!) {
  myQuery(input: $input) @rest(
    type: "QueryResponse",
    method: "POST"
    path: "v1/my-query"
  ) {
    id
    minimumPreparationTime
    maximumPreparationTime
    extras {
      id
      optional
      key
    }

    warning
    error {
      id
      type
      message
    }
  }
}

Extending Greg comment and apollo-link-error , you can retrieve the error body from networkError.result.message .扩展 Greg 注释和apollo-link-error ,您可以从networkError.result.message检索错误正文。

You can use apollo-link-error to catch and handle server errors, network errors, and GraphQL errors.您可以使用apollo-link-error来捕获和处理服务器错误、网络错误和 GraphQL 错误。 You should be able to conditionally set response.errors if needed.如果需要,您应该能够有条件地设置response.errors

    import { onError } from "apollo-link-error";

    const link = onError(({ graphQLErrors, networkError, response, operation }) => 
    {
       if (graphQLErrors)
       graphQLErrors.map(({ message, locations, path }) =>
           console.log(
            `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: 
            ${path}`,
          ),
        );

     if (networkError) console.log(`[Network error]: ${networkError}`);

    response.error = // set error property here 
});

It may also be worth noting Apollo exposes an errorPolicy option, for which the default is none .值得注意的是,Apollo 公开了一个errorPolicy选项,默认值为none The policy can be modified to all which, according to the docs,根据文档,可以将策略修改为all这些,

is the best way to notify your users of potential issues while still showing as much data as possible from your server.是通知用户潜在问题的最佳方式,同时仍显示来自服务器的尽可能多的数据。 It saves both data and errors into the Apollo Cache so your UI can use them.它将数据和错误都保存到 Apollo 缓存中,以便您的 UI 可以使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM