Cannot update cache after mutation with errors
See original GitHub issueIntended 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:
- Created 4 years ago
- Reactions:15
- Comments:8 (2 by maintainers)
Top GitHub Comments
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 toall
.Sorry for the inconvenience !
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.