From d6a85389e3703b87926b9ca3dbcfa50c0f0d872f Mon Sep 17 00:00:00 2001 From: neopostmodern Date: Sat, 20 May 2023 15:52:01 +0200 Subject: [PATCH] fix(client): add new tag reference directly to query cache, fixes #221 --- client/src/renderer/components/AddTagForm.tsx | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/client/src/renderer/components/AddTagForm.tsx b/client/src/renderer/components/AddTagForm.tsx index 00ace12..1a237a1 100644 --- a/client/src/renderer/components/AddTagForm.tsx +++ b/client/src/renderer/components/AddTagForm.tsx @@ -59,19 +59,24 @@ const AddTagForm = ({ } const { addTagByNameToNote } = data; - const newTags = addTagByNameToNote.tags.filter((tag) => - cache.readFragment({ - id: cache.identify(tag), - fragment: BASE_TAG_FRAGMENT, - }) - ); + cache.modify({ + id: 'ROOT_QUERY', + fields: { + tags( + existingTagsRefs: Array<{ __ref: string }> = [] + ): Array<{ __ref: string }> { + const newTags = addTagByNameToNote.tags + .filter((tag) => { + const tagCacheId = cache.identify(tag); - newTags.forEach((tag) => { - cache.writeFragment({ - id: cache.identify(tag), - fragment: BASE_TAG_FRAGMENT, - data: tag, - }); + return !existingTagsRefs.some( + ({ __ref }) => __ref === tagCacheId + ); + }) + .map((tag) => ({ __ref: cache.identify(tag)! })); + return [...existingTagsRefs, ...newTags]; + }, + }, }); }, });