Skip to content

Commit

Permalink
fix(formatting, crash): fixed possible crashing when @cache directi…
Browse files Browse the repository at this point in the history
…ve has missing arguments
  • Loading branch information
BowlingX committed Jun 1, 2021
1 parent 79ae8a9 commit 6671b5c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
26 changes: 19 additions & 7 deletions src/proxyCacheLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@ export const proxyCacheLink = <K extends string, V, T extends Cache<K, V>>(

const server = removeCacheDirective(operation.query)
const { query } = operation
if (server) operation.query = server
const { id, timeout } = calculateArguments<K>(
query,
operation.variables,
cacheKeyModifier,
operation.getContext()
)

if (server) {
operation.query = server
}

let id: K, timeout: number
try {
const { id: thisId, timeout: thisTimeout } = calculateArguments<K>(
query,
operation.variables,
cacheKeyModifier,
operation.getContext()
)
id = thisId
timeout = thisTimeout
} catch(e) {
errorOnGet(e)
return forward(operation)
}
let subscriber: ZenObservable.Subscription
return new Observable((observer) => {
queryCache
Expand Down
17 changes: 10 additions & 7 deletions src/utils-browser-only.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import _get from 'lodash/get'
export const DIRECTIVE = 'cache'

export function removeCacheDirective(query: DocumentNode): DocumentNode {
return removeDirectivesFromDocument([
{
// if the directive should be removed
test: (directive?: DirectiveNode) => {
return directive?.name?.value === DIRECTIVE
return removeDirectivesFromDocument(
[
{
// if the directive should be removed
test: (directive?: DirectiveNode) => {
return directive?.name?.value === DIRECTIVE
},
},
}
], query) as DocumentNode
],
query
) as DocumentNode
}

type ValueValueNodes = StringValueNode
Expand Down

0 comments on commit 6671b5c

Please sign in to comment.