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

Fix an issue where incorrect results are returned when changing variables and skipping #12461

Merged
merged 7 commits into from
Mar 20, 2025

Conversation

jerelmiller
Copy link
Member

Fixes #12458

If a query changed variables and simultaneously skipped a query, QueryInfo would get updated with the right variables, but the previous cache.watch was active. This was a problem when the fetchPolicy changed to standby as a result of skipping the query while changing variables. If something else cache a cache update during the period while the query was in standby, the diff was updated to the result from the previous variables which meant the next time queryInfo.getDiff() was called, it used the already stored wrong result.

This change cancels a cache.watch if variables change so that cache updates while a query is skipped don't update QueryInfo.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@jerelmiller jerelmiller requested a review from phryneas March 19, 2025 19:58
Copy link

changeset-bot bot commented Mar 19, 2025

🦋 Changeset detected

Latest commit: fba44bf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@apollo/client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svc-apollo-docs
Copy link

svc-apollo-docs commented Mar 19, 2025

✅ Docs preview has no changes

The preview was not built because there were no changes.

Build ID: ebdd4ed547fdaec639db5627

Copy link

pkg-pr-new bot commented Mar 19, 2025

npm i https://pkg.pr.new/@apollo/client@12461

commit: fba44bf

Copy link

netlify bot commented Mar 19, 2025

Deploy Preview for apollo-client-docs ready!

Name Link
🔨 Latest commit fba44bf
🔍 Latest deploy log https://app.netlify.com/sites/apollo-client-docs/deploys/67dc2851234ecf0008c19fe6
😎 Deploy Preview https://deploy-preview-12461--apollo-client-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

github-actions bot commented Mar 19, 2025

size-limit report 📦

Path Size
dist/apollo-client.min.cjs 41.27 KB (+0.05% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (CJS) 50.72 KB (+0.04% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) (CJS) 47.8 KB (+0.05% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" 36.27 KB (+0.06% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) 33.66 KB (+0.06% 🔺)
import { ApolloProvider } from "@apollo/client/react" 1.26 KB (0%)
import { ApolloProvider } from "@apollo/client/react" (production) 1.24 KB (0%)
import { useQuery } from "@apollo/client/react" 5.21 KB (0%)
import { useQuery } from "@apollo/client/react" (production) 4.29 KB (0%)
import { useLazyQuery } from "@apollo/client/react" 4.78 KB (0%)
import { useMutation } from "@apollo/client/react" 3.62 KB (0%)
import { useMutation } from "@apollo/client/react" (production) 2.84 KB (0%)
import { useSubscription } from "@apollo/client/react" 4.42 KB (0%)
import { useSubscription } from "@apollo/client/react" (production) 3.48 KB (0%)
import { useSuspenseQuery } from "@apollo/client/react" 5.89 KB (0%)
import { useSuspenseQuery } from "@apollo/client/react" (production) 4.55 KB (0%)
import { useBackgroundQuery } from "@apollo/client/react" 5.38 KB (0%)
import { useBackgroundQuery } from "@apollo/client/react" (production) 4.04 KB (0%)
import { useLoadableQuery } from "@apollo/client/react" 5.47 KB (0%)
import { useLoadableQuery } from "@apollo/client/react" (production) 4.12 KB (0%)
import { useReadQuery } from "@apollo/client/react" 3.43 KB (0%)
import { useReadQuery } from "@apollo/client/react" (production) 3.37 KB (0%)
import { useFragment } from "@apollo/client/react" 2.36 KB (0%)
import { useFragment } from "@apollo/client/react" (production) 2.31 KB (0%)

variables: { id: 1 },
});

await rerender({ id: 2, skip: true });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the core issues at heart. When variables change and skipping a query (which sets the fetchPolicy to standby), getDiff is not called, which meant that updateWatch in QueryInfo was not called with the new variables. If a cache update occurred while the query was in standby (line 1697 in this test), the lastDiff was updated with the result from old variables. QueryInfo.getDiff() then just returned that lastDiff from that point forward because it didn't think variables had changed.


using _disabledAct = disableActEnvironment();
const { takeSnapshot, rerender } = await renderHookToSnapshotStream(
({ id, skip }) => useQuery(query, { skip, variables: { id } }),
Copy link
Member Author

@jerelmiller jerelmiller Mar 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to whittle this reproduction down to skip and variables without the need for another query to reproduce. The trick is writing to change variables and set skip at the same time, then issue cache updates while the query is in standby to reproduce this behavior.

Copy link
Member

@phryneas phryneas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phew, that one looks like it was tricky to find - good call!

@github-actions github-actions bot added the auto-cleanup 🤖 label Mar 20, 2025
@jerelmiller jerelmiller merged commit 12c8d06 into main Mar 20, 2025
45 checks passed
@jerelmiller jerelmiller deleted the jerel/skip-bug branch March 20, 2025 14:50
@github-actions github-actions bot mentioned this pull request Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useQuery does not return the right results when changing variables with skip: true -> false
3 participants