Skip to content

keep ObservableQuery initiated from useQuery inactive before first subscription#12285

Merged
phryneas merged 17 commits intomainfrom
pr/inactiveBeforeSubscription
Apr 4, 2025
Merged

keep ObservableQuery initiated from useQuery inactive before first subscription#12285
phryneas merged 17 commits intomainfrom
pr/inactiveBeforeSubscription

Conversation

@phryneas
Copy link
Copy Markdown
Member

Triggered by this Discord discussion

Generally, creating a ObservableQuery via watchQuery is already a side effect and adds the query to QueryManager.queries - and it will only be removed if it is subscribed and then unsubscribed.

There might be a number of edge-case scenarios where that is not the case, so this is an attempt from a different angle to solve this problem.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jan 21, 2025

🦋 Changeset detected

Latest commit: 0a85d41

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
Copy Markdown

svc-apollo-docs commented Jan 21, 2025

✅ Docs preview has no changes

The preview was not built because there were no changes.

Build ID: 5a482ab3fda914a46fea4fb4

@netlify
Copy link
Copy Markdown

netlify Bot commented Jan 21, 2025

Deploy Preview for apollo-client-docs ready!

Name Link
🔨 Latest commit 0a85d41
🔍 Latest deploy log https://app.netlify.com/sites/apollo-client-docs/deploys/67eff38662908d0008ab2f5b
😎 Deploy Preview https://deploy-preview-12285--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.

@phryneas phryneas force-pushed the pr/inactiveBeforeSubscription branch from 507624d to ff3c4bd Compare January 21, 2025 11:32
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Jan 21, 2025

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

commit: 0a85d41

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 21, 2025

size-limit report 📦

Path Size
dist/apollo-client.min.cjs 41.36 KB (+0.22% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (CJS) 50.81 KB (+0.19% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) (CJS) 47.9 KB (+0.2% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" 36.34 KB (+0.19% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) 33.74 KB (+0.23% 🔺)
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" 9.94 KB (+90.78% 🔺)
import { useQuery } from "@apollo/client/react" (production) 8.86 KB (+106.19% 🔺)
import { useLazyQuery } from "@apollo/client/react" 9.32 KB (+95.12% 🔺)
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.03% 🔺)
import { useSubscription } from "@apollo/client/react" (production) 3.48 KB (+0.03% 🔺)
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%)

@nikdimm
Copy link
Copy Markdown

nikdimm commented Jan 21, 2025

@nikdimm

@phryneas
Copy link
Copy Markdown
Member Author

There is one point I'm a bit concerned about, and that's this:

private getQuery(queryId: string): QueryInfo {
if (queryId && !this.queries.has(queryId)) {
this.queries.set(queryId, new QueryInfo(this, queryId));
}
return this.queries.get(queryId)!;
}

That means that if we delay registration like this and for whatever reason getQuery gets called (e.g. by a refetch call before the subscription started), a new QueryInfo that doesn't match the QueryInfo from the ObservableQuery gets injected.

Copy link
Copy Markdown
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

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

I'd be ok with trying this out.

Comment thread src/react/hoc/__tests__/queries/lifecycle.test.tsx Outdated
Comment thread src/core/__tests__/ApolloClient/general.test.ts Outdated
@github-actions github-actions Bot added the auto-cleanup 🤖 label Apr 4, 2025
phryneas and others added 2 commits April 4, 2025 16:57
Co-authored-by: Jerel Miller <jerelmiller@gmail.com>
Co-authored-by: Jerel Miller <jerelmiller@gmail.com>
@phryneas phryneas changed the title experiment: keep ObservableQuery initiated from useQuery inactive before first subscription keep ObservableQuery initiated from useQuery inactive before first subscription Apr 4, 2025
@phryneas phryneas marked this pull request as ready for review April 4, 2025 15:01
@phryneas phryneas merged commit cdc55ff into main Apr 4, 2025
45 checks passed
@github-actions github-actions Bot mentioned this pull request Apr 4, 2025
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators May 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants