apollo-feature-requests icon indicating copy to clipboard operation
apollo-feature-requests copied to clipboard

`onCacheHit` callback when query result is found in Apollo cache

Open alessio-mms opened this issue 3 years ago • 3 comments

The Problem

When using cache-first fetch policy I often require side-effects as result of querying data. The onCompleted callback generally addresses this problem, but only triggers when data is fetched from the server and not from cache. Currently, to address this problem, I need to implement a custom solution or change the fetch policy to cache-and-network, which obviously means we no longer gain the benefit from caching the data.

The Solution

Introduce an additional callback onCacheHit (Or something similar) that allows side-effects as a result of a query returning a result from cache.

Why is it better?

No custom solutions required for detecting Apollo client cache hits. This really would fix the headaches I get working with apollo client.

alessio-mms avatar Dec 01 '21 13:12 alessio-mms

Any update on this? We are running into the same problem where we need to rely on the onCompleted callback which, as mentioned above, does not run when retrieving data from the cache. In our use case, we do not care about whether the result was retrieved from the cache or through a network call. We need to run the onCompleted callback in all cases or, as suggested here, provide an onCacheHit callback.

Frankly I'm a bit dismayed that Apollo decided that this should be the default behavior without exposing any kind of override.

dbehmoaras avatar Jun 21 '22 19:06 dbehmoaras

Following up to share that it appears that Apollo has pretty much flat out ignored this request since it has been an outstanding bug for YEARS. They just close the issue and do nothing. See below for just two of several examples: https://github.com/apollographql/react-apollo/issues/2177 https://github.com/apollographql/react-apollo/issues/3488

dbehmoaras avatar Jun 21 '22 20:06 dbehmoaras

Also here scratching my head as to how something so fundamental has been purposely overlooked.

cfator avatar Dec 20 '22 00:12 cfator