apollo-client: Mutations failures optional rollbackOptimistic
I 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
]
}
}
}
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 4
- Comments: 21 (7 by maintainers)
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.