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.

Cannot update cache after mutation with errors

See original GitHub issue

Intended outcome: I have a mutation which will fail when called with stale data. For example, if I had loaded the page with version 1 of a todo with an ID of 1, and while I was updating it a different user also updated the todo incrementing its version to 2, the server will respond with the following:

{
  "data": {
    "todo": {
      "id": 1,
      "version": 2,
      "description": "The updated todo description."
    }
  },
  "errors": [
    {
      "message": "Todo has been updated",
      "extensions": {
        "staleData": true
      }
    }
  ]
}

The idea here is that the server is responding with an error indicating that our mutation failed. In addition, it’s also providing the updated todo so the user can choose to cancel their edit or make it again. Since the server responds with the new version (and the rest of the latest todo information), the mutation will succeed the next time it’s called.

With this server response, the intent is to update the Todo:1 in the cache, and use the error details to trigger a “Are you sure you want to do this?” workflow.

Actual outcome: The update method of my mutation is never called because the response contains errors. None of the available errorPolicy options change this outcome, and I’ve been able to trace it down to this line of code.

How to reproduce the issue: https://codesandbox.io/s/apollo-client-error-template-1o8ix

In the above sandbox, nothing will be displayed in the UI and you’ll have to open the console. Look for the two INFO comments for instructions on switches that can be toggled. When errors exist in the response, we don’t see the My response is: console statement. When those errors don’t exist, we do.

I would expect to be able to update the cache in either case, but the Apollo client is preventing me from doing so when the server responds with errors.

Versions

  System:
    OS: macOS Mojave 10.14.6
  Binaries:
    Node: 10.16.0 - ~/.nvm/versions/node/v10.16.0/bin/node
    Yarn: 1.19.1 - /usr/local/bin/yarn
    npm: 6.9.0 - ~/.nvm/versions/node/v10.16.0/bin/npm
  Browsers:
    Firefox: 69.0
    Safari: 13.0.3
  npmPackages:
    apollo-cache: ^1.2.1 => 1.3.2 
    apollo-cache-inmemory: ^1.6.3 => 1.6.3 
    apollo-client: ^2.6.4 => 2.6.4 
    apollo-link-context: ^1.0.19 => 1.0.19 
    apollo-link-error: ^1.1.12 => 1.1.12 
    apollo-link-http: ^1.5.16 => 1.5.16 

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:15
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
cunisiacommented, May 20, 2021

Sorry I had not seen @jcreighton answer to this other related issue. The provided code snippet demonstrates how to trigger the update method if the mutation’s result contains errors. errorPolicy option needs to be set to all.

Sorry for the inconvenience !

0reactions
mbergeronupgradecommented, Oct 7, 2021

Leaving a comment in case someone encounters this in version 3+, the bug was present in 3.1.4 despite all the above recommendations but is working fine after updating to latest.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mutations in Apollo Client - Apollo GraphQL Docs
When a mutation's response is insufficient to update all modified fields in your cache (such as certain list fields), you can define an...
Read more >
apollo client 3 cache doesn't update after mutation
I use a query to initially load the user's data then update the cache with the result during the mutation. Below are the...
Read more >
How to update the Apollo Client's cache after a mutation
Basically, you should make your mutation results have all of the data necessary to update the queries previously fetched. That's also why is...
Read more >
Advanced Topics on Caching – Angular
Using update gives you full control over the cache, allowing you to make changes to your data model in response to a mutation...
Read more >
Update Apollo Cache after a mutation and get instant benefits ...
You must create a new object, you cannot mutate the return value of cache.readQuery anymore and apollo will throw an error. 2 ...
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