【问题标题】:Checking the return of Multiple mutations requests using Apollo Client使用 Apollo 客户端检查多个突变请求的返回
【发布时间】:2017-10-17 06:23:31
【问题描述】:

我正在使用 Apollo Client 和 React 制作一个简单的购物车,但我显示的从服务器检索到的总价格不是参考发送的最后一个请求,而是参考接收的最后一个请求。有没有办法使请求同步或类似的东西?甚至如何知道上次发送的突变的返回值是多少?

我的代码:

  this.props.mutate({ variables: { input: {
    lineItems: this.productCart
  }}})
  .then(({ data }) => {
    this.setState({
      subtotalPrice : data.manageCheckout.checkout.subtotalPrice
    })
  }).catch((error) => {
    console.warn('there was an error sending the query', error)
  })

【问题讨论】:

    标签: reactjs graphql apollo react-apollo apollo-client


    【解决方案1】:

    我不是 100% 关注你,但我不认为这是不支持。变异查询不等待承诺完成。

    apollo-client 存在问题

    您可以尝试解决此问题的一种方法是使用setTimeout,这样您的价格只会在商店更新后更新。

     this.props.mutate({ variables: { input: {
        lineItems: this.productCart
      }}})
      .then(({ data }) => {
        setTimeout(() => {
          this.setState({ 
          subtotalPrice : data.manageCheckout.checkout.subtotalPrice
        })
       )}
      }).catch((error) => {
        console.warn('there was an error sending the query', error)
      })
    

    这是一个 hack,但它对我有用。仍在等待 github 问题以获取更多信息。

    【讨论】:

    • 那没用:/ 需要为每次点击在屏幕上添加加载以使所有内容同步...但感谢您的回答:)
    猜你喜欢
    • 2021-09-03
    • 2019-06-25
    • 2020-02-01
    • 2019-02-14
    • 2017-09-06
    • 2018-12-19
    • 2020-01-23
    • 2020-02-03
    • 2021-12-14
    相关资源
    最近更新 更多