Introduce useOptimisticEvict (#1629)

This commit is contained in:
Charles Bochet
2023-09-16 22:23:43 -07:00
committed by GitHub
parent 9be674e440
commit c82c5a191e
3 changed files with 36 additions and 6 deletions

View File

@ -0,0 +1,24 @@
import { useApolloClient } from '@apollo/client';
export const useOptimisticEvict = () => {
const cache = useApolloClient().cache;
const performOptimisticEvict = (
typename: string,
fieldName: string,
fieldValue: string,
) => {
const serializedCache = cache.extract();
Object.values(serializedCache)
.filter((item) => item.__typename === typename)
.forEach((item) => {
if (item[fieldName] === fieldValue) {
cache.evict({ id: cache.identify(item) });
}
});
};
return {
performOptimisticEvict,
};
};