ApolloClient: mutate Promise resolves before mutation takes place
See original GitHub issueIntended outcome: I am attempting to use the ApolloClient in combination with react-hook-form to submit a mutation, wait for it to succeed/fail, and respond accordingly.
Actual outcome: The mutate function resolves successfully immediately after being invoked - regardless of whether or not the mutation request succeeded. There’s no easy way for me to capture and respond to errors as part of the form submission process because of this.
How to reproduce the issue:
const { handleSubmit } = useForm();
const client = useApolloClient();
const onSubmit = (data, e) => {
return client.mutate(...).then(
response => { ..., },
err => { ... } // <--- never gets triggered
);
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
...
</form>
);
Versions
System:
OS: Linux 5.10 Ubuntu 20.04.4 LTS (Focal Fossa)
Binaries:
Node: 15.14.0 - ~/.nvm/versions/node/v15.14.0/bin/node
Yarn: 3.1.1 - ~/repos/fifthsun/web/node_modules/.bin/yarn
npm: 7.7.6 - ~/.nvm/versions/node/v15.14.0/bin/npm
Browsers:
Chrome: 102.0.5005.61
npmPackages:
@apollo/client: ^3.3.6 => 3.6.9
@apollo/react-hoc: ^4.0.0 => 4.0.0
apollo: ^2.32.1 => 2.34.0
apollo-link-scalars: ^3.0.0 => 3.0.0
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Mutate Promise - Wait for query to be done #1618 - GitHub
Having await apolloClient.mutate(...) resolve BEFORE the mutation + query fully resolves creates non-obvious bugs because the JS way is to ...
Read more >Mutations in Apollo Client - Apollo GraphQL Docs
Call reset to reset the mutation's result to its initial state (i.e., before the mutate function was called). You can use this to...
Read more >Mutations - Apollo GraphQL Docs
If a mutation updates a single existing entity, Apollo Client can automatically update that entity's value in its cache when the mutation returns....
Read more >Refetching queries in Apollo Client - Apollo GraphQL Docs
To selectively refetch queries outside of a mutation, you instead use the refetchQueries method of ApolloClient , which is documented here. client.
Read more >Handling operation errors - Apollo GraphQL Docs
Apollo Client helps you handle these errors according to their type, enabling you to show appropriate information to the user when an error...
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
@rwilliams3088 I agree, it be nice to offer this in the client.
Update: One of the updates between my solution and the present version has broken the above so that it no longer serves to fix the problem… So this is a bug again