Last active
March 23, 2021 16:28
-
-
Save lydemann/0ebcbf52cafc5dca64c301bdce462468 to your computer and use it in GitHub Desktop.
graphql-helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function removeFromCache<ReadQueryResponseT>( | |
toRemove: EntityObject, | |
readQuery: DocumentNode, | |
cache: ApolloCache<any>, | |
entityName: keyof ReadQueryResponseT | |
) { | |
const existingEntities = cache.readQuery< | |
Record<keyof ReadQueryResponseT, EntityObject[]> | |
>({ | |
query: readQuery, | |
}); | |
if (toRemove && existingEntities) { | |
cache.writeQuery({ | |
query: readQuery, | |
data: { | |
[entityName]: existingEntities[entityName].filter( | |
(entity) => entity.id !== toRemove.id | |
), | |
}, | |
}); | |
cache.evict({ id: toRemove.id }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment