Open mogzol opened 3 months ago
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 },
});
},
});
}
Issue Description
When using the
cache-only
fetch policy withuseQuery
, if data is cleared from the cache in aclient.refetchQueries
operation, the query will make a network request, even though that should never happen with acache-only
fetch policy.Link to Reproduction
https://codesandbox.io/p/sandbox/apollo-cache-policy-issue-dj2w8s
Reproduction Steps
NetworkQuery
request is sent, thecache-only
query does correctly use only the cacheCacheOnlyQuery
network request is made.@apollo/client
version3.11.4