Skip to content

Commit

Permalink
feat(directives): support array type of result
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Jul 30, 2020
1 parent 734306a commit f5a028f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 90 deletions.
67 changes: 0 additions & 67 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@matters/apollo-response-cache",
"version": "1.1.0",
"version": "1.2.0",
"description": "Caching and invalidation mechanisms (plugins, directives) of Apollo GraphQL",
"author": "Matters <hi@matters.news>",
"license": "MIT",
Expand Down
29 changes: 18 additions & 11 deletions src/directives/logCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,27 @@ export const LogCacheDirective = ({
const [root, _, { __nodeFQCKeySet, __redis }] = args
const result = await resolve.apply(this, args)

const nodeType = typeResolver ? typeResolver(type, result) : type
const nodeId =
get(result, identifier) || get(result, 'id') || get(result, '_id')

const shouldLogCache = __redis && __nodeFQCKeySet && nodeType && nodeId
if (!shouldLogCache) {
if (!__redis || !__nodeFQCKeySet) {
return result
}

try {
__nodeFQCKeySet.add(toNodeFQCKey({ type: nodeType, id: nodeId }))
} catch (error) {
console.warn(error)
}
const nodes = Array.isArray(result) ? result : [result]

nodes.forEach((node) => {
const nodeType = typeResolver ? typeResolver(type, node) : type
const nodeId =
get(node, identifier) || get(node, 'id') || get(node, '_id')

if (!nodeType || !nodeId) {
return
}

try {
__nodeFQCKeySet.add(toNodeFQCKey({ type: nodeType, id: nodeId }))
} catch (error) {
console.warn(error)
}
})

return result
}
Expand Down
26 changes: 15 additions & 11 deletions src/directives/purgeCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,28 @@ export const PurgeCacheDirective = ({
const [root, _, { __redis }] = args
const result = await resolve.apply(this, args)

const nodeType = typeResolver ? typeResolver(type, result) : type
const nodeId =
get(result, identifier) || get(result, 'id') || get(result, '_id')
const defaultNode = { id: nodeId, type: nodeType }

const shouldPurgeCache = __redis && nodeType && nodeId
if (!shouldPurgeCache) {
if (!__redis) {
return result
}

const extraNodes = extraNodesPath ? get(result, extraNodesPath, []) : []
const nodes: Node[] = [...extraNodes, defaultNode]
nodes.map((node) => {
if (!node) {
const nodes = Array.isArray(result)
? [...extraNodes, ...result]
: [...extraNodes, result]

nodes.forEach((node) => {
const nodeType = typeResolver ? typeResolver(type, node) : type
const nodeId =
get(node, identifier) || get(node, 'id') || get(node, '_id')

if (!nodeType || !nodeId) {
return
}

invalidateFQC({ node, redis: __redis })
invalidateFQC({
node: { type: nodeType, id: nodeId },
redis: __redis,
})
})

return result
Expand Down

0 comments on commit f5a028f

Please sign in to comment.