我正在使用GraphQL Apollo客户端。我试图从有错误的查询中检索结果。
如果在同一响应中返回错误,我该如何检索数据?
这会自动捕获错误。相反,我希望它访问数据。
this.client.query({
query: gql(query),
variables: variables
}).then(function ({ data }) {
return data;
}).catch(error => console.log(error));

无法在Apollo docs中找到任何相关内容。有什么想法吗?
答案 0 :(得分:1)
在Apollo客户端中,各种错误类型如下: 1. GraphQL错误, 2.服务器错误, 3.交易错误 4. UI错误 5. Apollo客户端错误。 作为@ Alexander Schoonderwaldt,您可以了解此错误和错误政策here
您可以使用以下代码来处理/捕获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: {} }; });