Skip to content

Commit

Permalink
fix(context): pass request or current context to key builder
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlingX committed Mar 4, 2019
1 parent 4e24acc commit b5604c4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/proxyCacheLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const proxyCacheLink = (queryCache: Cache<string, Object>, cacheKeyModifi
const server = removeCacheDirective(operation.query)
const { query } = operation
if (server) operation.query = server
const { id, timeout } = calculateArguments(query, operation.variables, cacheKeyModifier)
const { id, timeout } = calculateArguments(query, operation.variables, cacheKeyModifier, operation.getContext())

const possibleData = queryCache.get(id)

Expand Down
2 changes: 1 addition & 1 deletion src/proxyCacheMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const proxyCacheMiddleware =
// we remove the @cache directive if it exists
if (isCache) {
const nextQuery = removeCacheDirective(doc)
const { id, timeout } = calculateArguments(doc, req.body.variables, cacheKeyModifier)
const { id, timeout } = calculateArguments(doc, req.body.variables, cacheKeyModifier, req)
const possibleData = queryCache.get(id)
if (possibleData) {
const { data, time } = possibleData
Expand Down
29 changes: 15 additions & 14 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,25 @@ export interface Cache<K, V> {
set(key: K, value: V): Cache<K, V>;
}

export type CacheKeyModifier = (?string, ?Object) => ?string
export type CacheKeyModifier = (?string, ?Object, ?Object) => ?string

export const didTimeout = (timeout: number, time: number) =>
timeout > 0 && ((time + (Number(timeout) * 1000)) < Number(new Date()))

export const calculateArguments = (query: DocumentNode, variables: ?Object, cacheKeyModifier: ?CacheKeyModifier) => {
const { id, timeout, modifier } = getDirectiveArgumentsAsObject(query, DIRECTIVE)
let thisId = modifier ? modifier.reduce((next, path) => {
return `${next}.${_get(variables, path, '')}`
}, id) : id
export const calculateArguments =
(query: DocumentNode, variables: ?Object, cacheKeyModifier: ?CacheKeyModifier, context: Object) => {
const { id, timeout, modifier } = getDirectiveArgumentsAsObject(query, DIRECTIVE)
let thisId = modifier ? modifier.reduce((next, path) => {
return `${next}.${_get(variables, path, '')}`
}, id) : id

if (cacheKeyModifier) {
thisId = cacheKeyModifier(thisId, variables)
}
if (cacheKeyModifier) {
thisId = cacheKeyModifier(thisId, variables, context)
}

if (!thisId) {
throw new Error(`@${DIRECTIVE} directive requires a unique id.`)
}
if (!thisId) {
throw new Error(`@${DIRECTIVE} directive requires a unique id.`)
}

return { id: thisId, timeout, modifier }
}
return { id: thisId, timeout, modifier }
}

0 comments on commit b5604c4

Please sign in to comment.