1
1
import type { ASTNode , DirectiveNode , GraphQLSchema } from 'graphql' ;
2
2
import { valueFromAST , valueFromASTUntyped } from 'graphql' ;
3
3
import { getArgumentValues } from './getArgumentValues.js' ;
4
+ import { memoize1 } from './memoize.js' ;
4
5
5
6
export type DirectableASTNode = ASTNode & { directives ?: readonly DirectiveNode [ ] } ;
6
7
export type DirectableObject = {
@@ -25,6 +26,39 @@ export function getDirectiveExtensions<
25
26
TDirectiveAnnotationsMap [ directiveName ]
26
27
> ;
27
28
} = { } ;
29
+
30
+ if ( directableObj . extensions ) {
31
+ let directivesInExtensions = directableObj . extensions ;
32
+ for ( const pathSegment of pathToDirectivesInExtensions ) {
33
+ directivesInExtensions = directivesInExtensions ?. [ pathSegment ] ;
34
+ }
35
+ if ( directivesInExtensions != null ) {
36
+ for ( const directiveNameProp in directivesInExtensions ) {
37
+ const directiveObjs = directivesInExtensions [ directiveNameProp ] ;
38
+ const directiveName = directiveNameProp as keyof TDirectiveAnnotationsMap ;
39
+ if ( Array . isArray ( directiveObjs ) ) {
40
+ for ( const directiveObj of directiveObjs ) {
41
+ let existingDirectiveExtensions = directiveExtensions [ directiveName ] ;
42
+ if ( ! existingDirectiveExtensions ) {
43
+ existingDirectiveExtensions = [ ] ;
44
+ directiveExtensions [ directiveName ] = existingDirectiveExtensions ;
45
+ }
46
+ existingDirectiveExtensions . push ( directiveObj ) ;
47
+ }
48
+ } else {
49
+ let existingDirectiveExtensions = directiveExtensions [ directiveName ] ;
50
+ if ( ! existingDirectiveExtensions ) {
51
+ existingDirectiveExtensions = [ ] ;
52
+ directiveExtensions [ directiveName ] = existingDirectiveExtensions ;
53
+ }
54
+ existingDirectiveExtensions . push ( directiveObjs ) ;
55
+ }
56
+ }
57
+ }
58
+ }
59
+
60
+ const memoizedStringify = memoize1 ( obj => JSON . stringify ( obj ) ) ;
61
+
28
62
const astNodes : DirectableASTNode [ ] = [ ] ;
29
63
if ( directableObj . astNode ) {
30
64
astNodes . push ( directableObj . astNode ) ;
@@ -60,39 +94,16 @@ export function getDirectiveExtensions<
60
94
}
61
95
}
62
96
}
63
- existingDirectiveExtensions . push ( value ) ;
64
- }
65
- }
66
- }
67
-
68
- if ( directableObj . extensions ) {
69
- let directivesInExtensions = directableObj . extensions ;
70
- for ( const pathSegment of pathToDirectivesInExtensions ) {
71
- directivesInExtensions = directivesInExtensions ?. [ pathSegment ] ;
72
- }
73
- if ( directivesInExtensions != null ) {
74
- for ( const directiveNameProp in directivesInExtensions ) {
75
- const directiveObjs = directivesInExtensions [ directiveNameProp ] ;
76
- const directiveName = directiveNameProp as keyof TDirectiveAnnotationsMap ;
77
- if ( Array . isArray ( directiveObjs ) ) {
78
- for ( const directiveObj of directiveObjs ) {
79
- let existingDirectiveExtensions = directiveExtensions [ directiveName ] ;
80
- if ( ! existingDirectiveExtensions ) {
81
- existingDirectiveExtensions = [ ] ;
82
- directiveExtensions [ directiveName ] = existingDirectiveExtensions ;
83
- }
84
- existingDirectiveExtensions . push ( directiveObj ) ;
97
+ if ( astNodes . length > 0 && existingDirectiveExtensions . length > 0 ) {
98
+ const valStr = memoizedStringify ( value ) ;
99
+ if ( existingDirectiveExtensions . some ( val => memoizedStringify ( val ) === valStr ) ) {
100
+ continue ;
85
101
}
86
- } else {
87
- let existingDirectiveExtensions = directiveExtensions [ directiveName ] ;
88
- if ( ! existingDirectiveExtensions ) {
89
- existingDirectiveExtensions = [ ] ;
90
- directiveExtensions [ directiveName ] = existingDirectiveExtensions ;
91
- }
92
- existingDirectiveExtensions . push ( directiveObjs ) ;
93
102
}
103
+ existingDirectiveExtensions . push ( value ) ;
94
104
}
95
105
}
96
106
}
107
+
97
108
return directiveExtensions ;
98
109
}
0 commit comments