Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The onCompleted callback of useQuery executes from cache writes. #10076

Closed
lawrence-yu opened this issue Sep 6, 2022 · 9 comments · Fixed by #10229
Closed

The onCompleted callback of useQuery executes from cache writes. #10076

lawrence-yu opened this issue Sep 6, 2022 · 9 comments · Fixed by #10229

Comments

@lawrence-yu
Copy link

lawrence-yu commented Sep 6, 2022

Intended outcome:

The onCompleted option provided to useQuery should not be called after data was changed from an update via cache.writeQuery (where it was just a local cache update without a network request).

Actual outcome:

When using cache.writeQuery to update local data, the onCompleted option of a useQuery for the same query gets called. This is a change from what we see on 3.4.x, where the onCompleted callback is not called after these cache updates.

How to reproduce the issue:

https://github.com/lawrence-yu/react-apollo-error-onCompleted

  1. After having it running locally, click on the button with "Update person's name via writeQuery". This does a cache write to append a 1 to a person's name, which is reflected in UI with the list of names.
  2. In 3.6.8, note that the "onCompleted count" increases with each button click / cache write.
    1. There's a 3.4.17 branch in the fork that can be used to see that, with each button click, the onCompleted count remains at 1 (the one being from after initial query completion).

Versions

 System:
    OS: Windows 10 10.0.19042
  Binaries:
    Node: 16.13.2 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.18 - ~\.yarn\bin\yarn.CMD
    npm: 8.1.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (105.0.1343.27)
  npmPackages:
    @apollo/client: ^3.6.8 => 3.6.8

Other notes

The limited documentation for the onCompleted option leaves me unsure about what is the "correct" behavior. Is getting called by cache updates actually correct now? Or was the old behavior correct and this is an actual bug?

Potentially related to:
#9688
#10059

@srmagura
Copy link

srmagura commented Sep 8, 2022

As you suspected, this is very likely the same underlying issue as #10059.

@bignimbus bignimbus removed the 🔍 investigate Investigate further label Sep 15, 2022
@lawrence-yu
Copy link
Author

Hi @bignimbus, I'm quite confused about your comment and how it relates to my issue. Is it possible your comment/issue-close here was meant for a different issue?

I see you had a recent docs PR here, https://github.com/apollographql/apollo-client/pull/10066/files, but with that I still don't see how it relates to my ask here.

@bignimbus
Copy link
Contributor

You're quite right @lawrence-yu, sorry for the confusion! I've deleted the comment and will repost it where it belongs.

@bignimbus bignimbus reopened this Sep 15, 2022
@bignimbus bignimbus added the 🔍 investigate Investigate further label Sep 15, 2022
@sanex3339
Copy link

Seems we have the same error:

  • we call query on a button click via useLazyQuery that lazily checks GQL operation by querying operation field using network-only fetch policy.
  • if query is completed successfully and the operation is enabled, we call mutation that updates data and returns the same operation field. Operation is disabled in this case, because we updated the data.
  • after the mutation is finished and its result is written to the cache, onCompleted callback of query is triggered automatically

@eveevans
Copy link

eveevans commented Oct 20, 2022

Same problem here, And Im using Apollo 3.6.9

@alessbell
Copy link
Contributor

Thanks for the reproduction, @lawrence-yu!

#10229 is open with the fix, I'll update this issue again when it's merged and released 🙇‍♀️

@alessbell
Copy link
Contributor

The fix is now available in the current 3.8 alpha release which can be installed via npm i @apollo/client@alpha.

@alessbell
Copy link
Contributor

Hi all - I'm going to close this issue out since this is fixed in a prerelease. Feel free to install the latest alpha with the command above. Thanks!

@github-actions
Copy link
Contributor

github-actions bot commented May 6, 2023

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants