【问题标题】:Get response in catch block在 catch 块中获取响应
【发布时间】:2017-12-10 20:47:51
【问题描述】:

我正在从我的 graphql 端点发送 dataerrors(架构是在 Graphene 中制作的)。我查询的方式是使用client.query 方法(在我使用withApollo HOC 时作为道具提供)。

我的做法是这样的:

    let result = this.props.client.query({
        query: query,
        variables: {},
        fetchPolicy: this.state.fetchPolicy
        })
        .then(result => this.handle_data(result.data))
        .catch((e) => {
            alert("Hello"+e);
        });

当服务器发生错误时(由于一些错误的查询),我发送data 作为nullerrors 设置实际错误消息。然后在这里执行catch 块。 e 的值不是我从服务器发送的errors。如何获取我在catch 块中发送的服务器响应(就像我们在then 块中一样)?

【问题讨论】:

  • 我认为错误的查询会导致业务异常,但不会导致服务器异常。业务异常可能不会在 catch 块中捕获
  • @brk result = schema.execute(query) 将自动填充 result.dataresult.errors。我也检查过了。所以这意味着获取的响应将包含dataerrors。在客户端,catch 块被执行。我能以某种方式在 catch 块中获取我的响应对象吗?
  • 错误在哪里抛出?在 client.query() 中?如果是这样,那么这是Apollo客户端抛出的错误,根据API(dev.apollodata.com/core/apollo-client-api.html#ApolloClient\.query),这只包含错误,没有数据。
  • @FinbarrO'B 是的!你是对的。感谢您提出这个问题。

标签: javascript reactjs graphql react-apollo apollo-client


【解决方案1】:

使用 withApollo 然后在您的配置中添加 errorPolicy: 'ignore',如下所示:

const result = await client.query({
      query: getQuery(fieldArgs),
      variables: { filters: [...filters] },
      errorPolicy: 'ignore',
    });

【讨论】:

    猜你喜欢
    • 2021-11-16
    • 2011-05-16
    • 2023-03-03
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 2021-08-28
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多