apollo-client icon indicating copy to clipboard operation
apollo-client copied to clipboard

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

Open mogzol opened this issue 1 year ago • 1 comments

Issue Description

When using the cache-only fetch policy with useQuery, if data is cleared from the cache in a client.refetchQueries operation, the query will make a network request, even though that should never happen with a cache-only fetch policy.

Link to Reproduction

https://codesandbox.io/p/sandbox/apollo-cache-policy-issue-dj2w8s

Reproduction Steps

  • Open the reproduction sandbox link above
  • Observe that initially only the NetworkQuery request is sent, the cache-only query does correctly use only the cache
  • Click the "refetch" button
  • Observe that a CacheOnlyQuery network request is made.

@apollo/client version

3.11.4

mogzol avatar Aug 08 '24 21:08 mogzol

Hi @mogzol 👋

Thanks for opening this issue. We agree this is surprising behavior, and we're looking at changing this in Apollo Client 4.0, currently in the planning stage, to eliminate the possibility of a query with a fetch policy of cache-only of ever going to the network.

In the meantime, you can leverage onQueryUpdated which is described in the "refetching selectively" section of our docs to prevent the network request:

async function refetch() {
  await client.refetchQueries({
+    onQueryUpdated(observableQuery) {
+      return observableQuery.options.fetchPolicy !== "cache-only";
+    },
    updateCache(cache) {
      cache.modify({
        id: "ROOT_QUERY",
        fields: { user: (_, { DELETE }) => DELETE },
      });
    },
  });
}

alessbell avatar Aug 12 '24 18:08 alessbell

Hey @mogzol 👋

Thanks for your patience on this issue! I've got good news! We've fixed this issue and will release it in the next major version of Apollo Client. refetchQueries will now filter out all cache-only queries by default. If updateCache modifies data for a cache-only query and onQueryUpdated was not provided (like your reproduction), the query will no longer be refetched.

We just released this change in 4.0.0-alpha.23 and 4.0.0-rc.0. We'd love for you to try this out and provide any feedback before we publicly release 4.0.

For good measure, I've updated your reproduction using the the first 4.0 release candidate which shows the fix in place: https://codesandbox.io/p/devbox/modest-tdd-mswhyd

Since this has been merged and released, I'm going to go ahead and close this issue. Thanks!

jerelmiller avatar Jun 18 '25 23:06 jerelmiller

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

github-actions[bot] avatar Jun 18 '25 23:06 github-actions[bot]

Thanks! I tried with 4.0.0-rc.0 and can confirm the issue is resolved!

mogzol avatar Jun 19 '25 22:06 mogzol

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. For general questions, we recommend using our Community Forum or Stack Overflow.

github-actions[bot] avatar Jul 20 '25 00:07 github-actions[bot]