Skip to content

Instantly share code, notes, and snippets.

@lydemann
Created March 7, 2021 11:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lydemann/c63e263ff016219a0d583c5f17ccd518 to your computer and use it in GitHub Desktop.
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