@@ -3,6 +3,7 @@ import type {
33 ListValueNode ,
44 StringValueNode ,
55 DirectiveNode ,
6+ OperationDefinitionNode ,
67} from 'graphql'
78import { removeDirectivesFromDocument } from 'apollo-utilities'
89import _get from 'lodash/get'
@@ -35,8 +36,11 @@ export function getDirectiveArgumentsAsObject<K>(
3536 doc : DocumentNode ,
3637 directive : string
3738) {
38- return _get ( doc . definitions , '0.directives' , [ ] )
39- . filter ( ( v : DirectiveNode ) => v . name . value === directive )
39+ const operationDef = doc . definitions . find (
40+ ( v ) => v . kind === 'OperationDefinition' && v . directives
41+ ) as OperationDefinitionNode | undefined
42+ return operationDef ?. directives
43+ ?. filter ( ( v : DirectiveNode ) => v . name . value === directive )
4044 . reduce ( ( next : Record < string , string [ ] | string > , v : DirectiveNode ) => {
4145 return {
4246 ...v . arguments ?. reduce (
@@ -54,7 +58,7 @@ export function getDirectiveArgumentsAsObject<K>(
5458 ) ,
5559 ...next ,
5660 }
57- } , { } ) as Directive < K >
61+ } , { } ) as Directive < K > | undefined
5862}
5963
6064export type CacheKeyModifier = < T > (
@@ -72,12 +76,17 @@ export const calculateArguments = <K extends string, T = Record<string, any>>(
7276 cacheKeyModifier ?: CacheKeyModifier ,
7377 context ?: T
7478) => {
75- const { id, timeout, modifier } = getDirectiveArgumentsAsObject < K > (
76- query ,
77- DIRECTIVE
78- )
79+ const directiveArgs = getDirectiveArgumentsAsObject < K > ( query , DIRECTIVE )
80+
81+ if ( ! directiveArgs ) {
82+ throw new Error (
83+ `@${ DIRECTIVE } not found, this is unexpected and likely a bug. Please report this issue.`
84+ )
85+ }
86+
87+ const { id, timeout, modifier } = directiveArgs
7988 let thisId = modifier
80- ? modifier . reduce ( ( next , path ) => {
89+ ? modifier ? .reduce ( ( next , path ) => {
8190 return `${ next } .${ _get ( variables , path , '' ) } `
8291 } , id )
8392 : id
0 commit comments