NerdWalletOSS / apollo-cache-policies

An extension of the Apollo 3 cache with support for advanced cache policies.
Apache License 2.0
157 stars 21 forks source link

`onQueryUpdated` not getting called #86

Closed VillePakarinenPosti closed 3 months ago

VillePakarinenPosti commented 3 months ago

Issue with InvalidationPolicyCache Handling of Broadcasts in Apollo Client

I encountered a minor issue when using the InvalidationPolicyCache instead of Apollo's built-in InMemoryCache. It appears there are subtle differences in how the extended class handles broadcasts.

While experimenting with Apollo's client.refetchQueries, I noticed that the onQueryUpdated callback was never triggered when using InvalidationPolicyCache. After investigating, I traced the issue to the broadcastWatches method in the Apollo Client. This method seems responsible for this functionality: https://github.com/apollographql/apollo-client/blob/717421c2817f655bea9eec6aca42e3fa0edb1be5/src/cache/inmemory/inMemoryCache.ts#L531-L535.

The issue seems to arise because InvalidationPolicyCache does not pass the optional BroadcastOptions to the underlying super.broadcastWatches method. This omission appears to prevent the onQueryUpdated callback from being invoked as expected.

Here is the relevant section of the InvalidationPolicyCache code: https://github.com/NerdWalletOSS/apollo-cache-policies/blob/42b9004feeaebfc3cdfa175ad2a9798e33aee03e/src/cache/InvalidationPolicyCache.ts#L132-L136.

Thank you for looking into this!

danReynolds commented 3 months ago

Ah good catch! Would you be interested in spinning up a PR to relay those optional parameters? Otherwise I'll get to it when I have time. Thanks for the report.

VillePakarinenPosti commented 3 months ago

Sounds good to me & would love to get this merged. 👍

See https://github.com/NerdWalletOSS/apollo-cache-policies/pull/87 @danReynolds