-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(directive): add
@purgeCache
& @logCache
directives
- Loading branch information
Showing
9 changed files
with
2,272 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { defaultFieldResolver, GraphQLField } from 'graphql' | ||
import { SchemaDirectiveVisitor } from 'graphql-tools' | ||
|
||
import { CACHE_PREFIX } from '../enums' | ||
|
||
interface Params { | ||
_type: string | ||
} | ||
|
||
export class LogCacheDirective extends SchemaDirectiveVisitor { | ||
visitFieldDefinition(field: GraphQLField<any, any> & Params): void { | ||
const { resolve = defaultFieldResolver } = field | ||
field._type = this.args.type | ||
field.resolve = async function (...args) { | ||
const [root, _, { cacheKeys, redis }] = args | ||
const result = await resolve.apply(this, args) | ||
|
||
if (result && result.id && redis && cacheKeys && field._type) { | ||
try { | ||
let cacheType = field._type | ||
switch (field._type) { | ||
case 'Node': | ||
case 'Response': { | ||
cacheType = result.__type | ||
break | ||
} | ||
} | ||
cacheKeys.add(`${CACHE_PREFIX.KEYS}:${cacheType}:${result.id}`) | ||
} catch (error) { | ||
// TOOD: logger | ||
} | ||
} | ||
|
||
return result | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { defaultFieldResolver, GraphQLField } from 'graphql' | ||
import { SchemaDirectiveVisitor } from 'graphql-tools' | ||
import { compact, replace, get } from 'lodash' | ||
|
||
import { CACHE_KEYWORD, CACHE_PREFIX } from '../enums' | ||
|
||
interface CacheSet { | ||
id: string | ||
type: string | ||
} | ||
|
||
const getCacheKeys = (customs: CacheSet[], fallback: CacheSet): string[] => { | ||
if (customs && customs.length > 0) { | ||
return compact( | ||
customs.map((custom: CacheSet) => { | ||
if (custom && custom.id && custom.type) { | ||
return `${CACHE_PREFIX.KEYS}:${custom.type}:${custom.id}` | ||
} | ||
}) | ||
) | ||
} | ||
|
||
return [ | ||
`${CACHE_PREFIX.KEYS}:${replace(fallback.type, '!', '')}:${fallback.id}`, | ||
] | ||
} | ||
|
||
export class PurgeCacheDirective extends SchemaDirectiveVisitor { | ||
visitFieldDefinition(field: GraphQLField<any, any>): void { | ||
const { resolve = defaultFieldResolver } = field | ||
field.resolve = async function (...args) { | ||
const [root, _, { redis }, { returnType }] = args | ||
|
||
const result = await resolve.apply(this, args) | ||
if (result && result.id && redis && returnType) { | ||
try { | ||
const cache = get(result, CACHE_KEYWORD, []) | ||
const keys = getCacheKeys(cache, { | ||
id: result.id, | ||
type: `${returnType}`, | ||
}) | ||
keys.map(async (key: string) => { | ||
const hashes = await redis.client.smembers(key) | ||
hashes.map(async (hash: string) => { | ||
await redis.client | ||
.pipeline() | ||
.del(`fqc:${hash}`) | ||
.srem(key, hash) | ||
.exec() | ||
}) | ||
}) | ||
} catch (error) { | ||
// TODO: logger | ||
} | ||
} | ||
return result | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// keyword notating for cache invalidation | ||
export const CACHE_KEYWORD = '__cache__' | ||
|
||
// redis cache for apq keys or resolver returned objects | ||
export const CACHE_PREFIX = { | ||
KEYS: 'cache-keys', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import responseCachePlugin from './ApolloServerPluginResponseCache' | ||
import responseCachePlugin from './plugins/responseCachePlugin' | ||
import { LogCacheDirective } from './directives/logCache' | ||
import { PurgeCacheDirective } from './directives/purgeCache' | ||
|
||
export { responseCachePlugin } | ||
export { responseCachePlugin, LogCacheDirective, PurgeCacheDirective } |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
{ | ||
"compilerOptions": {}, | ||
"compilerOptions": { | ||
"esModuleInterop": true | ||
}, | ||
"exclude": ["dist", "node_modules"] | ||
} |