Apollo客户端:从有错误的查询中检索结果 [英] Apollo Client: retrieving the results from a query with errors

查看:58
本文介绍了Apollo客户端:从有错误的查询中检索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GraphQL Apollo客户端.我正在尝试从有错误的查询中检索结果.

GraphQL响应

即使同一响应中返回了错误,我该如何检索数据?

这将自动捕获错误.相反,我希望它访问数据.

  this.client.query({查询:gql(query),变量:变量}).then(函数({数据}){返回数据;}).catch(错误=> console.log(错误));  

在Apollo文档中找不到与此相关的任何内容.有什么想法吗?

解决方案

如果您将来在这里跌跌撞撞

在Apollo客户端中,有多种错误类型,如下所示:1. GraphQL错误,2.服务器错误,3.交易错误4. UI错误5. Apollo客户端错误.作为@ Alexander Schoonderwaldt,您可以在此处

您可以使用下面的代码来处理/捕获graphql错误.如果这些值返回 undefined ,则不再是graphql错误.您需要调查.您可能会遇到返回 Error CONNREFUSED 或其他错误的网络错误.

.询问({查询:myquery}).then(({{data})=> {console.log(data.user);返回{loggingInUser:data};}).catch(错误=> {//正常失败console.log(errors.message);//记录grapgql错误返回{loggingInUser:{}};}); 

I'm using the GraphQL Apollo Client. And I'm trying to retrieve the results from a query with errors.

GraphQL response

How do I retrieve the data, even when an error is returned in the same response?

This automatically catches the error. Instead, I want it to access the data.

this.client.query({
  query: gql(query),
  variables: variables
}).then(function ({ data }) {
  return data;
}).catch(error => console.log(error));

Couldn't find anything in Apollo docs about this. Any ideas?

解决方案

if you stumble here in future

In Apollo-client there are various error types as follow: 1. GraphQL Errors, 2. Server Errors, 3. Transaction Errors, 4. UI Errors, 5. Apollo Client Errors. As @ Alexander Schoonderwaldt you can learn about this errors and error policy here

You can handle/catch graphql errors using code below. If the values returns undefined, its no longer a graphql error. You need to investigate. You may have network error that returns Error CONNREFUSED or others.

    .query({
      query: myquery
    })
    .then(({ data }) => {
      console.log(data.user);
      return { loggedInUser: data };
    })
    .catch(errors => {
      // Fail gracefully
      console.log(errors.message); // log grapgql error
      return { loggedInUser: {} };
    });

这篇关于Apollo客户端:从有错误的查询中检索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆