Skip to content

Don't allow fetchMore or updates to polling for cache-only queries and exclude from client.refetchQueries #12712

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 52 commits into from
Jun 18, 2025

Conversation

jerelmiller
Copy link
Member

@jerelmiller jerelmiller commented Jun 18, 2025

Fixes #11997

Throws an error when fetchMore is called on a cache-only query. Additionally a warning will be emitted when setting a poll interval on a cache-only query and polling will be cancelled.

cache-only queries are now excluded from refetchQueries when using include: 'all' | 'active' and when listed as a query in include.

cache-only queries are also excluded from client.reFetchObservableQueries when includeStandby is true.

@jerelmiller jerelmiller requested a review from phryneas June 18, 2025 06:57
Copy link

changeset-bot bot commented Jun 18, 2025

🦋 Changeset detected

Latest commit: 0d15e5a

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

Copy link

pkg-pr-new bot commented Jun 18, 2025

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

commit: 0d15e5a

Copy link
Contributor

github-actions bot commented Jun 18, 2025

size-limit report 📦

Path Size
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (CJS) 42.67 KB (+0.17% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) (CJS) 37.75 KB (+0.01% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" 32.64 KB (+0.32% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) 27.01 KB (+0.27% 🔺)
import { ApolloProvider } from "@apollo/client/react" 5.72 KB (+0.68% 🔺)
import { ApolloProvider } from "@apollo/client/react" (production) 963 B (0%)
import { useQuery } from "@apollo/client/react" 7.5 KB (+0.79% 🔺)
import { useQuery } from "@apollo/client/react" (production) 2.7 KB (0%)
import { useLazyQuery } from "@apollo/client/react" 6.97 KB (+0.46% 🔺)
import { useLazyQuery } from "@apollo/client/react" (production) 2.19 KB (-0.09% 🔽)
import { useMutation } from "@apollo/client/react" 6.55 KB (+0.5% 🔺)
import { useMutation } from "@apollo/client/react" (production) 1.76 KB (0%)
import { useSubscription } from "@apollo/client/react" 6.66 KB (+0.65% 🔺)
import { useSubscription } from "@apollo/client/react" (production) 1.86 KB (0%)
import { useSuspenseQuery } from "@apollo/client/react" 8.51 KB (+0.79% 🔺)
import { useSuspenseQuery } from "@apollo/client/react" (production) 3.74 KB (0%)
import { useBackgroundQuery } from "@apollo/client/react" 8.25 KB (+0.55% 🔺)
import { useBackgroundQuery } from "@apollo/client/react" (production) 3.5 KB (0%)
import { useLoadableQuery } from "@apollo/client/react" 8.57 KB (+0.59% 🔺)
import { useLoadableQuery } from "@apollo/client/react" (production) 3.82 KB (0%)
import { useReadQuery } from "@apollo/client/react" 6.46 KB (+0.41% 🔺)
import { useReadQuery } from "@apollo/client/react" (production) 1.69 KB (0%)
import { useFragment } from "@apollo/client/react" 6.51 KB (+0.58% 🔺)
import { useFragment } from "@apollo/client/react" (production) 1.74 KB (0%)

@jerelmiller

This comment was marked as resolved.

Comment on lines 764 to 768
).rejects.toEqual(
new InvariantError(
"Cannot execute `refetch` for 'cache-only' query 'C'. Please use a different fetch policy."
)
);
Copy link
Member

@phryneas phryneas Jun 18, 2025

Choose a reason for hiding this comment

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

What if you have 10 ObservableQuery instances with the same query, but different variables and fetchPolicies and a single one with cache-only?

I think this shouldn't error. It should just be skipped.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair enough. Should we at least warn if you provide a specific query to include that is a cache-only query? Or just silently not include it?

Comment on lines 1255 to 1250
if (pollInterval && fetchPolicy === "cache-only") {
invariant.warn(
"Cannot poll on 'cache-only' query '%s' and as such, polling is disabled. Please use a different fetch policy.",
getOperationName(this.query, "(anonymous)")
);
}
Copy link
Member

Choose a reason for hiding this comment

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

reobserve, and as a result updatePolling can be called quite often - this shouldn't warn every time. Maybe only once per ObservableQuery instance?

Comment on lines 1302 to 1301
) {
return;
}

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
) {
return;
}
if (oq.options.fetchPolicy === "cache-only") {
return;
}

As a result of my previous comment, this should probably do it.

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.

One follow-up question might be if we should also disallow it for standby queries.

@jerelmiller
Copy link
Member Author

I started doing this for standby queries but quickly abandoned it because we allow client.refetchQueries({ include: 'all' }) which specifically fetches standby queries. That and client.reFetchObservableQueries(true) also fetches standby queries.

I think limiting it to just cache-only makes sense given the other functionality, otherwise we'd need to rethink refetchQueries with include: 'all' vs active since that list becomes the same and I'm not sure we care to do that right now.

);

this.setError(error);
return Promise.reject(error);
Copy link
Member Author

Choose a reason for hiding this comment

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

Let's just throw and avoid emitting an error for fetchMore.

@@ -1683,6 +1716,18 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`,
});
}

private setError(error: ErrorLike) {
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 can go away

@@ -660,6 +665,23 @@ export class ObservableQuery<
public refetch(
variables?: Partial<TVariables>
): ObservableQuery.ResultPromise<QueryResult<TData>> {
const { fetchPolicy } = this.options;

if (fetchPolicy === "cache-only") {
Copy link
Member Author

Choose a reason for hiding this comment

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

Remove this. We will allow refetch with cache-only queries.

@@ -1954,6 +2139,44 @@ describe("ObservableQuery", () => {
expect(observable.options.fetchPolicy).toBe("no-cache");
});

it("does not allow refetch on cache-only query", async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Update to allow this

@@ -599,7 +600,7 @@ describe("cache-first", () => {
});

describe("cache-only", () => {
it("allows explicit refetch to happen", async () => {
it("does not allow refetch", async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Revert back

@@ -6080,6 +6080,80 @@ test("rerenders with data: undefined when changing variables and an error is ret
await expect(takeSnapshot).not.toRerender();
});

test("throws when calling `refetch` on a cache-only query", async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Allow this

@@ -6418,6 +6464,64 @@ describe("useQuery Hook", () => {
await expect(takeSnapshot).not.toRerender();
});

it("does not allow refetch on a cache-only query", async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Reverse this

@jerelmiller jerelmiller changed the title Don't allow refetch, fetchMore or updates to polling for cache-only queries Don't allow fetchMore or updates to polling for cache-only queries and exclude from client.refetchQueries Jun 18, 2025
@jerelmiller jerelmiller requested a review from phryneas June 18, 2025 20:05
jerelmiller and others added 5 commits June 18, 2025 14:37
Co-authored-by: Lenz Weber-Tronic <lorenz.weber-tronic@apollographql.com>
Co-authored-by: Lenz Weber-Tronic <lorenz.weber-tronic@apollographql.com>
@jerelmiller jerelmiller merged commit bbb2b61 into release-4.0 Jun 18, 2025
42 of 43 checks passed
@jerelmiller jerelmiller deleted the jerel/no-refetch-standby-cache-only branch June 18, 2025 21:14
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.

cache-only fetchPolicy results in network request after deleting cached data
2 participants