Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
graphql-helpers.ts
export function createInCache<ReadQueryResponseT>(
toCreate: EntityObject,
readQuery: DocumentNode,
cache: ApolloCache<any>,
entityName: keyof ReadQueryResponseT
) {
const existingEntities = cache.readQuery<
Record<keyof ReadQueryResponseT, EntityObject[]>
>({
query: readQuery,
});
if (toCreate && existingEntities) {
cache.writeQuery({
query: readQuery,
data: {
[entityName]: [...existingEntities[entityName], toCreate],
},
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment