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.

ApolloClient: mutate Promise resolves before mutation takes place

See original GitHub issue

Intended 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:open
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jpvajdacommented, Aug 1, 2022

@rwilliams3088 I agree, it be nice to offer this in the client.

0reactions
rwilliams3088commented, Sep 22, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

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