簡體   English   中英

Apollo Client:從有錯誤的查詢中檢索結果

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

我正在使用 GraphQL Apollo 客戶端。 我正在嘗試從有錯誤的查詢中檢索結果。

GraphQL 響應

即使在同一響應中返回錯誤,我如何檢索數據?

這會自動捕獲錯誤。 相反,我希望它訪問數據。

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

在 Apollo 文檔中找不到關於此的任何內容。 有任何想法嗎?

如果你將來在這里絆倒

在 Apollo 客戶端中,有以下各種錯誤類型:1. GraphQL 錯誤,2. 服務器錯誤,3. 事務錯誤,4. UI 錯誤,5. Apollo 客戶端錯誤。 作為@ Alexander Schoonderwaldt,您可以在此處了解此錯誤和錯誤政策

您可以使用下面的代碼處理/捕獲 graphql 錯誤。 如果值返回undefined ,則不再是 graphql 錯誤。 你需要調查。 您可能有網絡錯誤,返回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