question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mutate Promise - Wait for query to be done

See original GitHub issue

So let’s say that I have this react app and there is a form with a submit button, which has the following click handler. What I would expect to happen is that, when the promise resolves the mutate command, the refetchQueries portion would already have occurred. But, that is not the case at all. What actually happens is that the refetchQueries and the router redirect happen at the same time. The .then(…) happens directly after I call mutate.

So how do I handle a situation like this?

import { graphql } from 'react-apollo';

...

onSubmit({ email, password }) {
    this.props.mutate({
      variables: { email, password },
      refetchQueries: [{ query }],
    })
    .then(() => { router.push('/dashboard') })
    .catch((res) => {
      const errors = res.graphQLErrors.map(error => error.message);
      this.setState({
        errors,
      });
    });
  }

...

export default graphql(mutation)(LoginForm);

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:41
  • Comments:39 (10 by maintainers)

github_iconTop GitHub Comments

32reactions
dbenchicommented, Nov 3, 2017

IMO, this ticket should be reopen. I do not need to hack arround componentWillUpdate and leverage the nextProps so as to guarantee that the mutation along with the refetchQueries are all setteled. I really want to know what the Apollo team thinks about this? Thanks a lot

21reactions
jzimmekcommented, Mar 18, 2018

this issue has been created almost a year ago and i was heavily affected by it at that time. after switching projects in between i am now back into a new apollo based project. i am still running into the same issue. last time i had no time to really address it, then switched over to non-apollo based projects and forgot about it. this time i really wanna fix it.

i appreciate all the explanations and possible solutions (workarounds) for this. but for me a resolved promise should be 100% complete before being resolved - everything else is imho just confusing.

i think the problem can be addressed with minimal changes and i prepared a pull request for discussion: https://github.com/apollographql/apollo-client/pull/3169

i have not yet created any dedicated tests for this change, but all existing tests pass.

before putting more time on this issue / pullrequest i want to ask upfront if:

  • this issue is something apollo team would generally accept as pull request or not (keep behavior as is)
  • it is fine to add async/await to the code-base (found those only in the unit tests)
  • this should be put behind some flag to not make it a breaking change
  • who this kind of change is being tested best (without introducing even more changes just for testing sake)

@helfer would love to hear some feedback from you. maybe you even have first thoughts by looking into the referenced pull request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I wait for a promise to finish before returning the ...
I have a simple JS function which queries a Parse database. It's supposed to return the array of results, but obviously due to...
Read more >
Mutations in Apollo Client - Apollo GraphQL Docs
Returning a Promise from onQueryUpdated will cause the final mutation Promise to await the returned Promise . Returning false causes the query to...
Read more >
How to wait for a promise to finish before returning the ...
Here a promise is a returned object from an asynchronous function, to that callback methods can be added based on the previous function's...
Read more >
Async data made simple with React Query - Hey! I'm Tyler
This hook takes a function which will run when a mutation is needed. This provided function can be an async function that returns...
Read more >
Sync, async, and promises | Cloud Functions for Firebase
Simplify asynchronous code with JavaScript promises; How promises work with functions ... To indicate that a function has completed its work successfully, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found