我正在使用 GraphQL Apollo 客户端。我正在尝试从有错误的查询中检索结果。
图形QL响应
如何检索数据,即使在同一响应中返回错误?
这会自动捕获错误。相反,我希望它访问数据。
this.client.query({
query: gql(query),
variables: variables
}).then(function ({ data }) {
return data;
}).catch(error => console.log(error));
在阿波罗文档中找不到有关此的任何内容。有什么想法吗?
如果你将来在这里绊倒了
在Apollo-client中,有以下各种错误类型:1. 图形QL错误,2. 服务器错误,3. 交易错误,4. 用户界面错误,5. 阿波罗客户端错误。作为@Alexander Schoonderwaldt,您可以在此处了解此错误和错误策略
您可以使用下面的代码处理/捕获 graphql 错误。如果值返回 undefined
,则不再是 graphql 错误。你需要调查。您可能有返回 Error CONNREFUSED
或其他错误的网络错误。
.query({ 查询:我的查询 }) .then(({ data }) => { console.log(data.user); 返回 { loggedInUser: data }; }) .catch(errors => { 优雅地失败 console.log(errors.message);日志抓取 返回 { loggedInUser: {} }; });