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

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

Open
mogzol opened this issue Aug 8, 2024 · 1 comment
Open
Labels

Comments

@mogzol
Copy link

mogzol commented Aug 8, 2024

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

@alessbell
Copy link
Member

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 },
      });
    },
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants