Apollo客户端突变错误处理 [英] Apollo client mutation error handling
问题描述
我在服务器上使用GraphQL和猫鼬.
发生验证错误时,GraphQL突变会发送状态码为200的响应.在客户端,响应如下所示:
{
"data": null,
"errors": [{
"message": "error for id...",
"path": "_id"
}]
}
我想使用apollo-client突变承诺的catch
功能来访问验证错误.像这样:
this.props.deleteProduct(this.state.selectedProductId).then(response => {
// handle successful mutation
}).catch(response => {
const errors = response.errors; // does not work
this.setState({ errorMessages: errors.map(error => error.message) });
});
这怎么办?
注意:由于最新版本的Apollo Client中的catch
中显示了突变错误,因此该答案(可能是整个问题)现在已过时./p>
当前,来自
GraphQL错误的错误显示在then
内响应的errors
字段中.我认为肯定有人宣称它们应该出现在catch
中,但这是
{
"data": null,
"errors": [{
"message": "error for id...",
"path": "_id"
}]
}
I would like to get access to the validation error using the catch
functionality of the apollo-client mutation promise. Something like:
this.props.deleteProduct(this.state.selectedProductId).then(response => {
// handle successful mutation
}).catch(response => {
const errors = response.errors; // does not work
this.setState({ errorMessages: errors.map(error => error.message) });
});
How can this be done?
Note: This answer (and arguably the whole question) is now outdated, since mutation errors show up in catch
in more recent versions of Apollo Client.
GraphQL errors from the mutation currently show up in the errors
field on the response inside then
. I think there's definitely a claim to be made that they should show up in the catch
instead, but here's a snippet of a mutation from GitHunt:
// The container
const withData = graphql(SUBMIT_REPOSITORY_MUTATION, {
props: ({ mutate }) => ({
submit: repoFullName => mutate({
variables: { repoFullName },
}),
}),
});
// Where it's called
return submit(repoFullName).then((res) => {
if (!res.errors) {
browserHistory.push('/feed/new');
} else {
this.setState({ errors: res.errors });
}
});
这篇关于Apollo客户端突变错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!