Introduce useOptimisticEvict (#1629)
This commit is contained in:
@ -18,12 +18,12 @@ export const useOptimisticEffect = () => {
|
||||
|
||||
const registerOptimisticEffect = useRecoilCallback(
|
||||
({ snapshot, set }) =>
|
||||
({
|
||||
<T>({
|
||||
variables,
|
||||
definition,
|
||||
}: {
|
||||
variables: OperationVariables;
|
||||
definition: OptimisticEffectDefinition<unknown>;
|
||||
definition: OptimisticEffectDefinition<T>;
|
||||
}) => {
|
||||
const optimisticEffects = snapshot
|
||||
.getLoadable(optimisticEffectState)
|
||||
@ -55,8 +55,8 @@ export const useOptimisticEffect = () => {
|
||||
variables,
|
||||
data: {
|
||||
people: definition.resolver({
|
||||
currentData: (existingData as GetPeopleQuery).people,
|
||||
newData,
|
||||
currentData: (existingData as GetPeopleQuery).people as T[],
|
||||
newData: newData as T[],
|
||||
variables,
|
||||
}),
|
||||
},
|
||||
@ -69,8 +69,9 @@ export const useOptimisticEffect = () => {
|
||||
variables,
|
||||
data: {
|
||||
companies: definition.resolver({
|
||||
currentData: (existingData as GetCompaniesQuery).companies,
|
||||
newData,
|
||||
currentData: (existingData as GetCompaniesQuery)
|
||||
.companies as T[],
|
||||
newData: newData as T[],
|
||||
variables,
|
||||
}),
|
||||
},
|
||||
|
||||
@ -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,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user