From 669f158ead171c30f0f3cb5b5e667e6095709b1f Mon Sep 17 00:00:00 2001 From: RoberTu <4065233+robertu7@users.noreply.github.com> Date: Mon, 10 Aug 2020 11:08:46 +0800 Subject: [PATCH] fix(list): fix wrong prasing results --- package.json | 2 +- src/directives/purgeCache.ts | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 7f04da6..e98c859 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@matters/apollo-response-cache", - "version": "1.2.0", + "version": "1.2.1", "description": "Caching and invalidation mechanisms (plugins, directives) of Apollo GraphQL", "author": "Matters ", "license": "MIT", diff --git a/src/directives/purgeCache.ts b/src/directives/purgeCache.ts index 0377b8f..8774e11 100644 --- a/src/directives/purgeCache.ts +++ b/src/directives/purgeCache.ts @@ -57,12 +57,10 @@ export const PurgeCacheDirective = ({ return result } - const extraNodes = extraNodesPath ? get(result, extraNodesPath, []) : [] - const nodes = Array.isArray(result) - ? [...extraNodes, ...result] - : [...extraNodes, result] - - nodes.forEach((node) => { + // parse results + const results = Array.isArray(result) ? [...result] : [result] + const parsedResults: Node[] = [] + results.map((node) => { const nodeType = typeResolver ? typeResolver(type, node) : type const nodeId = get(node, identifier) || get(node, 'id') || get(node, '_id') @@ -71,8 +69,19 @@ export const PurgeCacheDirective = ({ return } + parsedResults.push({ type: nodeType, id: nodeId }) + }) + + // merge results and extras + const extraNodes: Node[] = extraNodesPath + ? get(result, extraNodesPath, []) + : [] + const nodes = [...extraNodes, ...parsedResults] + + // invalidate + nodes.forEach((node) => { invalidateFQC({ - node: { type: nodeType, id: nodeId }, + node, redis: __redis, }) })