简体   繁体   English

Apollo Client:从有错误的查询中检索结果

[英]Apollo Client: retrieving the results from a query with errors

I'm using the GraphQL Apollo Client.我正在使用 GraphQL Apollo 客户端。 And I'm trying to retrieve the results from a query with errors.我正在尝试从有错误的查询中检索结果。

GraphQL response GraphQL 响应

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.在 Apollo 文档中找不到关于此的任何内容。 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.在 Apollo 客户端中,有以下各种错误类型:1. GraphQL 错误,2. 服务器错误,3. 事务错误,4. UI 错误,5. Apollo 客户端错误。 As @ Alexander Schoonderwaldt you can learn about this errors and error policy here作为@ Alexander Schoonderwaldt,您可以在此处了解此错误和错误政策

You can handle/catch graphql errors using code below.您可以使用下面的代码处理/捕获 graphql 错误。 If the values returns undefined , its no longer a graphql error.如果值返回undefined ,则不再是 graphql 错误。 You need to investigate.你需要调查。 You may have network error that returns Error CONNREFUSED or others.您可能有网络错误,返回Error CONNREFUSED或其他。

.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: {} };
    });

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

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