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.

Mutations failures optional rollbackOptimistic

See original GitHub issue

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.

3ohzdw8next6e6y360

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:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:21 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
gastonmorixecommented, Jul 31, 2017

I think this still something important to address. Key part of the data design solution Apollo should provide.

2reactions
helfercommented, May 1, 2017

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.

Read more comments on GitHub >

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

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