Mutate Promise - Wait for query to be done
See original GitHub issueSo 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:
- Created 6 years ago
- Reactions:41
- Comments:39 (10 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
IMO, this ticket should be reopen. I do not need to hack arround
componentWillUpdate
and leverage thenextProps
so as to guarantee that the mutation along with therefetchQueries
are allsetteled
. I really want to know what the Apollo team thinks about this? Thanks a lotthis 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:
@helfer would love to hear some feedback from you. maybe you even have first thoughts by looking into the referenced pull request.