Mutations failures optional rollbackOptimistic
See original GitHub issueI am creating a chat app. I receive new messages by subscriptions. I submit new messages by mutations (apollo http).
Intended outcome: Mutations optimistic updates to be rollback optionally. I want to let people now the message failed, and later give them the option to “resend” (or even automatically retry to resend when back online, this latter not that important in apollo-http).
Actual outcome: When apollo http server goes offline (fail for some reason), mutations fail. Optimistic updates happen but as soon as it realizes the network failed they are rolled back. Mutation’s optimistic updates are rollback by design. So the message vanishes.
How to reproduce the issue:
optimisticResponse: {
__typename: 'Mutation',
messageNew: {
__typename: 'Message',
id: -generateId(),
completed: false,
createdBy: {
__typename: 'User',
username: myUser.username,
id: myUser.id
},
createdAt: +new Date,
text
}
},
updateQueries: {
MessagesForChannelQuery: (previousResult, args) => {
return {
...previousResult,
messagesForChannel: [
...previousResult.messagesForChannel,
args.mutationResult.data.messageNew
]
}
}
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:21 (7 by maintainers)
Top Results From Across the Web
Optimistic mutation results - Apollo GraphQL Docs
As this example shows, the value of optimisticResponse is an object that matches the shape of the mutation response we expect from the...
Read more >Optimistic Updates | TanStack Query Docs
When you optimistically update your state before performing a mutation, there is a chance that the mutation will fail. In most of these...
Read more >Mutations - Sanity.io
A patch may include an optional ifRevisionID field. If the document's current revision ID does not match the provided revision ID, the patch...
Read more >Optimistic Updates | Svelte Query | SvelteStack
When you optimistically update your state before performing a mutation, there is a non-zero chance that the mutation will fail. In most cases,...
Read more >Mastering Mutations in React Query | TkDodo's blog
The code needed to make optimistic updates work is non-trivial, especially compared to "standard" mutations. You need to mimic what the backend ...
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
I think this still something important to address. Key part of the data design solution Apollo should provide.
Hi @gaston23 👋 I think the right way to solve this would not be to not roll back the optimistic update, but to build retries into the network interface, such that a mutation doesn’t fail immediately if the network is down. However, once the mutation has failed, the optimistic update should be rolled back immediately.
PS: I think some people already implemented custom network interfaces or middleware+afterware to retry failed requests. I don’t remember where, but it might be worth asking on slack.