react-relay-network-modern
react-relay-network-modern copied to clipboard
hook to invalidate cache items.
For now It can be done in such way:
let _globalCacheRef;
const RRNL = new RelayNetworkLayer([
cacheMiddleware({
size: 100,
ttl: 900000,
onInit: cache => (_globalCacheRef = cache),
})
]);
if (_globalCacheRef) {
_globalCacheRef.clear();
}
If you have any ideas how to improve this, let me know.
Hmm I'd like to be able to clear individual items
The cache class is used from Relay. This one https://github.com/facebook/relay/blob/master/packages/relay-runtime/network/RelayQueryResponseCache.js
Copy method from there
function getCacheKey(queryID: string, variables: Variables): string {
return JSON.stringify(stableCopy({queryID, variables}));
}
And then remove cached data in such way:
_globalCacheRef._responses.delete(getCacheKey(queryID, variables));
Cache keep server responses for your query by query name and its variables. So when the same query with variables requested, it takes response from cache.
Can we get a public API to do this?
BTW you may open PR to relay repo for improving its cache - add delete
method. And I think, it has big chances to be included in 1.5.0
Sure, as a workaround you may add this code to my repo.
Put here something like:
cache.delete = (queryID, variables) => {
cache._responses.delete(getCacheKey(queryID, variables));
}
and getCacheKey
function put outside (before) queryMiddleware
function in same file.
https://github.com/facebook/relay/issues/1687#issuecomment-302965908 suggests that the cache should be cleared on each mutation. It would be nice to have the ability to do something more granular, but that seems like it should be the default?