Apollo客户端突变错误处理 [英] Apollo client mutation error handling

查看:65
本文介绍了Apollo客户端突变错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器上使用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中,但这是

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆