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.

The `onCompleted` callback of `useQuery` executes from cache writes.

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:5
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
alessbellcommented, Oct 26, 2022

Thanks for the reproduction, @lawrence-yu!

https://github.com/apollographql/apollo-client/pull/10229 is open with the fix, I’ll update this issue again when it’s merged and released 🙇‍♀️

1reaction
lawrence-yucommented, Sep 15, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Queries - Apollo GraphQL Docs
Whenever Apollo Client fetches query results from your server, it automatically caches those results locally. This makes later executions of that same query ......
Read more >
onCompleted callback is not calling for 2nd ...
It seems to be a known issue that Apollo's onCompleted option works differently between queries fulfilled over the network and by the cache....
Read more >
Apollo oncompleted example. The ...
1 proper use of onCompleted for GraphQL mutations I want to run the query first. ... writeData to write local data to the...
Read more >
useQuery | TanStack Query Docs
useQuery · Optional · If set, this value will be used as the initial data for the query cache (as long as the...
Read more >
Apollo GraphQL - @apollo react hooks
import {useQuery} from '@apollo/react-hooks'; import gql from ... onCompleted, (data: TData | {}) => void, A callback executed once your ...
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