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.

Duplicate entries with optimistic mutations and subscriptions

See original GitHub issue

We have been trying to replicate the behaviour of the githunt sample implementing both optimistic updates and websocket subscriptions.

On some occasions, ie with a slow network, the order of the update changes and the store becomes corrupted and show duplicate entries. I could repro on githunt

Intended outcome:

No duplicate comments

Actual outcome:

Duplicate comments

How to reproduce the issue:

Go to http://www.githunt.com/, open chrome inspector and throttle network to Slow 3G
Submit a comment

repro_subscription3

In normal operations, the order of updates is optimistic mutation, real mutation, subscription. The problem happens when the subscription is triggered before the real mutation, ie: optimistic mutation, subscription, real mutation.

From what I can see, it’s related to the fact that subscribeToMore’s updateQuery is being passed the optimistic version of the query data, not the original.

Is there any reason for this? Retrieving the non optimistic with query solves the problem but I was not able to find a clean workaround.

Version

  • apollo-client@2.2.5

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
lorensrcommented, Jan 2, 2019

I was still getting duplicates in the store in v2.4.8. It was not fixed by subscribeToMore’s updateQuery having:

const alreadyHaveNewItem =
  some(prev.items, { id: newItem.id }) ||
  some(prev.items, { id: null })
if (alreadyHaveNewItem) {
  return prev
}

It was fixed by the mutation update having:

const alreadyHaveNewItem = some(data.items, { id: newItem.id })
if (!alreadyHaveNewItem) {
  data.items.unshift(newItem)
  store.writeQuery({ ...query, data })
}
0reactions
mikeRChambers610commented, May 6, 2021

So the issue is closed now… what was the solution??

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using subscription and update after mutation creates ...
I think the reason for this is the mutation update and the subscription both return a new node, creating a duplicate entry. Is...
Read more >
Subscriptions - Apollo GraphQL Docs
In addition to queries and mutations, GraphQL supports a third operation type: subscriptions. Like queries, subscriptions enable you to fetch data.
Read more >
GraphQL mutations - Relay
In GraphQL, data on the server is updated using GraphQL mutations. ... can be multiple optimistic responses affecting that store value.
Read more >
Optimistic updates with Apollo Graphql - Discuss Dgraph
ADD_TASK mutation export const ADD_TASK = gql` mutation ... This may cause some issues with creating duplicate entries on the client but ...
Read more >
Conflict Detection and Sync - AWS AppSync
When an AWS AppSync mutation changes an item in a versioned data source, ... Optimistic Concurrency is a conflict resolution strategy that AWS...
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