Skip to content

Don't call reobserve when changing most options in useQuery and instead apply them #12681

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

Merged
merged 30 commits into from
Jun 18, 2025

Conversation

jerelmiller
Copy link
Member

Fixes #11835

Changing most options for useQuery no longer calls reobserve to avoid network requests when unnecessary. Instead the options are applied to the next fetch.

@jerelmiller jerelmiller requested a review from phryneas June 6, 2025 22:23
Copy link

changeset-bot bot commented Jun 6, 2025

🦋 Changeset detected

Latest commit: b75c2d7

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

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

newOptions: Partial<ObservableQuery.Options<TData, TVariables>>
) {
const mergedOptions = compact(this.options, newOptions || {});
assign(this.options, mergedOptions);

if (this.options.fetchPolicy === "standby") {
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 mimics what reobserve does so that polling doesn't continue.

Copy link

pkg-pr-new bot commented Jun 6, 2025

npm i https://pkg.pr.new/apollographql/apollo-client/@apollo/client@12681

commit: d92356a

: T extends SnapshotStream<infer Snapshot, any> ?
(options: ToRerenderWithSimilarSnapshotOptions<Snapshot>) => Promise<R>
(options?: ToRerenderWithSimilarSnapshotOptions<Snapshot>) => Promise<R>
Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like the implementation allows has this argument as optional so I updated this to work the same.

Copy link
Member

Choose a reason for hiding this comment

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

Yup, that was the intention, thank you for catching that!

Copy link
Contributor

github-actions bot commented Jun 6, 2025

size-limit report 📦

Path Size
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (CJS) 42.52 KB (-0.05% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) (CJS) 37.66 KB (-0.11% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" 32.55 KB (-0.03% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) 26.97 KB (+0.12% 🔺)
import { ApolloProvider } from "@apollo/client/react" 5.68 KB (0%)
import { ApolloProvider } from "@apollo/client/react" (production) 963 B (0%)
import { useQuery } from "@apollo/client/react" 7.44 KB (+0.39% 🔺)
import { useQuery } from "@apollo/client/react" (production) 2.7 KB (+1.77% 🔺)
import { useLazyQuery } from "@apollo/client/react" 6.93 KB (-0.05% 🔽)
import { useLazyQuery } from "@apollo/client/react" (production) 2.19 KB (-0.14% 🔽)
import { useMutation } from "@apollo/client/react" 6.52 KB (0%)
import { useMutation } from "@apollo/client/react" (production) 1.76 KB (0%)
import { useSubscription } from "@apollo/client/react" 6.62 KB (0%)
import { useSubscription } from "@apollo/client/react" (production) 1.86 KB (0%)
import { useSuspenseQuery } from "@apollo/client/react" 8.44 KB (-0.4% 🔽)
import { useSuspenseQuery } from "@apollo/client/react" (production) 3.74 KB (-0.29% 🔽)
import { useBackgroundQuery } from "@apollo/client/react" 8.21 KB (-0.11% 🔽)
import { useBackgroundQuery } from "@apollo/client/react" (production) 3.5 KB (-0.31% 🔽)
import { useLoadableQuery } from "@apollo/client/react" 8.52 KB (0%)
import { useLoadableQuery } from "@apollo/client/react" (production) 3.82 KB (-0.08% 🔽)
import { useReadQuery } from "@apollo/client/react" 6.44 KB (+0.13% 🔺)
import { useReadQuery } from "@apollo/client/react" (production) 1.69 KB (+0.24% 🔺)
import { useFragment } from "@apollo/client/react" 6.47 KB (0%)
import { useFragment } from "@apollo/client/react" (production) 1.74 KB (0%)

@@ -946,11 +946,15 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`,
}

/** @internal */
public silentSetOptions(
public applyOptions(
Copy link
Member Author

Choose a reason for hiding this comment

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

The old name was never my favorite. Hope this is ok. Sorry for the noise 😆

@jerelmiller jerelmiller linked an issue Jun 9, 2025 that may be closed by this pull request
- `query`
- `variables`
- `skip`
- Changing `fetchPolicy` from `standby` to a different `fetchPolicy`
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- Changing `fetchPolicy` from `standby` to a different `fetchPolicy`
- Changing `fetchPolicy` from or to `standby`

Comment on lines 505 to 497
function shouldReobserve<TData, TVariables extends OperationVariables>(
previousOptions: Readonly<WatchQueryOptions<TVariables, TData>>,
options: Readonly<WatchQueryOptions<TVariables, TData>>
) {
return (
previousOptions.query !== options.query ||
!equal(previousOptions.variables, options.variables) ||
(previousOptions.fetchPolicy === "standby" &&
options.fetchPolicy !== "standby")
);
}
Copy link
Member

Choose a reason for hiding this comment

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

Should this whole logic maybe go into applyOptions? It doesn't really hurt and also could lead to some of the logic in InternalQueryReference.applyOptions being removed 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Discussed in person. We don't want to move here so that useLazyQuery doesn't get reobserve behavior when rerendering with new options before calling execute. execute is the catalyst that will potentially kick off the request (since it calls reobserve). Keeping it out of applyOptions will ensure we have more control over which behavior to use based on the hook using it.

: T extends SnapshotStream<infer Snapshot, any> ?
(options: ToRerenderWithSimilarSnapshotOptions<Snapshot>) => Promise<R>
(options?: ToRerenderWithSimilarSnapshotOptions<Snapshot>) => Promise<R>
Copy link
Member

Choose a reason for hiding this comment

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

Yup, that was the intention, thank you for catching that!

@jerelmiller jerelmiller marked this pull request as draft June 12, 2025 06:17
@jerelmiller jerelmiller force-pushed the jerel/use-query-less-fetch branch 2 times, most recently from 7ff3b66 to 9577cd7 Compare June 16, 2025 18:35
@jerelmiller jerelmiller marked this pull request as ready for review June 16, 2025 18:35
@jerelmiller jerelmiller requested a review from phryneas June 16, 2025 18:35

await expect(renderStream).toRerenderWithSimilarSnapshot({
// We waited 50ms before, so waiting another 50ms + 50ms = 100ms
timeout: 50,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
timeout: 50,
timeout: 55,

This will get very flaky in tests otherwise.

Copy link
Member Author

Choose a reason for hiding this comment

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

@jerelmiller jerelmiller force-pushed the jerel/use-query-less-fetch branch from 9577cd7 to fe8f0fa Compare June 18, 2025 18:31
@jerelmiller jerelmiller merged commit b181f98 into release-4.0 Jun 18, 2025
31 of 32 checks passed
@jerelmiller jerelmiller deleted the jerel/use-query-less-fetch branch June 18, 2025 18:43
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.

Changes to context cause refetches in useQuery
2 participants