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.

Update() being called twice on mutation

See original GitHub issue

BUG REPORT

What is the current behavior? My mutation’s update callback is being ran twice, adding it twice to the view. When you refresh the page, the duplicate item is gone. I initally thought it was adding it to local storage twice, but this is not the case. Items get added once to local storage but twice to my component data.

Steps to recreate I’ve been following this guide to create a simple API. I created a simple schema using the default wizard, (called it posts), then exported it to my application. I just added one extra query (that I don’t use right now) and that’s all I did. My code can be found here at line 61

What is the expected behavior? When using a mutation to create a new post, it should only create 1 new item in my UI. However the mutation update() is being called twice. At first I thought this was related to defining an optimistic response, but this behavior still occurs when I comment out that property.

The only “fix” for this I’ve found is by adding disableOffline: true to my AppSync client config. However this is not a great fix since it removes offline capabilities.

Which versions and which environment (browser, react-native, nodejs) / OS are affected by this issue? Did this work in previous versions? Windows 10, Chrome, VueJS. No idea if it worked previously.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:24 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
tqhoughtoncommented, Nov 9, 2018

Can someone change this back to a bug report instead of a question?

5reactions
undefobjcommented, Nov 12, 2018

@tqhoughton ok sounds good. If you want to send me a gist I can look at it.

@xSequential I was actually replying to @tqhoughton who is the original poster on this thread with my comments earlier. To address your questions I looked at your code and one thing I noticed is this line of code:

data: {
  getSkills: getSkills.concat([addedSkill]),
},

This will duplicate data in the cache, you need to filter with something like: https://github.com/awslabs/aws-mobile-appsync-sdk-js/blob/7a3f5cec214ea4a7c64a41c9106967e2e2fad0fc/packages/aws-appsync/src/helpers/offline.ts#L154

Or like this: https://github.com/aws-samples/aws-mobile-appsync-events-starter-react/blob/master/src/Components/NewEvent.js#L116

We really recommend that you use the Offline Helpers if possible because they do all these common cache operations for you. Take a look here: https://github.com/awslabs/aws-mobile-appsync-sdk-js#offline-helpers

Read more comments on GitHub >

github_iconTop Results From Across the Web

Apollo - update() method getting called twice, both times with ...
I'm calling an Apollo mutation, using optimisticResponse . The way it's supposed to work, as I understand it, is that update() gets called...
Read more >
Mutations in Apollo Client - Apollo GraphQL Docs
If your mutation specifies an optimistic response, your update function is called twice: once with the optimistic result, and again with the actual...
Read more >
Apollo - update() method getting called twice, both times with ...
ApolloClient.QueryManager initializes the mutation that triggers your update the first time with the optimistic response. That is happening inside ApolloClient ...
Read more >
MutationObserver.observe() - Web APIs - MDN Web Docs
You can call observe() multiple times on the same MutationObserver to watch for changes to different parts of the DOM tree and/or different ......
Read more >
Mastering Mutations in React Query | TkDodo's blog
11 // ✓ mutation is invoked when the form is submitted ... You can invoke the same useQuery call multiple times in different...
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