简体   繁体   中英

Checking the return of Multiple mutations requests using Apollo Client

I'm making a simple shopping cart using Apollo Client and React, but the total price I'm showing thats retrieved from the server its not making reference to the last request sent but to the last request received. Its there a way to make the requests synchronous or something like that? Or even how to know whats the return from the last mutation sent?

My code:

  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)
  })

I am not 100% I am following you, but I don't think this isn't support. The mutation query does not wait for the promise to complete.

There is an issue against the apollo-client

One way you could go about trying to solve this is to use setTimeout so youor price is only updated once the store has been updated.

 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)
  })

It's a hack, but it works for me. Still waiting on the github issue for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM