@@ -4,11 +4,13 @@ import type { ObjMap } from '../jsutils/ObjMap.js';
4
4
import type { GraphQLError } from '../error/GraphQLError.js' ;
5
5
6
6
import type {
7
+ ConstValueNode ,
7
8
DocumentNode ,
8
9
FragmentDefinitionNode ,
9
10
FragmentSpreadNode ,
10
11
OperationDefinitionNode ,
11
12
SelectionSetNode ,
13
+ VariableDefinitionNode ,
12
14
VariableNode ,
13
15
} from '../language/ast.js' ;
14
16
import { Kind } from '../language/kinds.js' ;
@@ -34,6 +36,10 @@ interface VariableUsage {
34
36
readonly type : Maybe < GraphQLInputType > ;
35
37
readonly defaultValue : Maybe < unknown > ;
36
38
}
39
+ interface DefinedVariable {
40
+ readonly variableDefinition : VariableDefinitionNode ;
41
+ readonly operation : OperationDefinitionNode ;
42
+ }
37
43
38
44
/**
39
45
* An instance of this class is passed as the "this" context to all validators,
@@ -50,11 +56,16 @@ export class ASTValidationContext {
50
56
Array < FragmentDefinitionNode >
51
57
> ;
52
58
59
+ private _variableDefinedOperations :
60
+ | ObjMap < ReadonlyArray < DefinedVariable > >
61
+ | undefined ;
62
+
53
63
constructor ( ast : DocumentNode , onError : ( error : GraphQLError ) => void ) {
54
64
this . _ast = ast ;
55
65
this . _fragments = undefined ;
56
66
this . _fragmentSpreads = new Map ( ) ;
57
67
this . _recursivelyReferencedFragments = new Map ( ) ;
68
+ this . _variableDefinedOperations = undefined ;
58
69
this . _onError = onError ;
59
70
}
60
71
@@ -134,6 +145,28 @@ export class ASTValidationContext {
134
145
}
135
146
return fragments ;
136
147
}
148
+
149
+ getVariableDefinitions ( node : VariableNode ) : ReadonlyArray < DefinedVariable > {
150
+ if ( ! this . _variableDefinedOperations ) {
151
+ const variableDefinedOperations : ObjMap < Array < DefinedVariable > > = { } ;
152
+ visit ( this . _ast , {
153
+ OperationDefinition ( operation ) {
154
+ for ( const varDef of operation . variableDefinitions ?? [ ] ) {
155
+ if ( ! variableDefinedOperations [ varDef . variable . name . value ] ) {
156
+ variableDefinedOperations [ varDef . variable . name . value ] = [ ] ;
157
+ }
158
+ variableDefinedOperations [ varDef . variable . name . value ] . push ( {
159
+ variableDefinition : varDef ,
160
+ operation,
161
+ } ) ;
162
+ }
163
+ } ,
164
+ } ) ;
165
+
166
+ this . _variableDefinedOperations = variableDefinedOperations ;
167
+ }
168
+ return this . _variableDefinedOperations [ node . name . value ] ?? [ ] ;
169
+ }
137
170
}
138
171
139
172
export type ASTValidationRule = ( context : ASTValidationContext ) => ASTVisitor ;
@@ -245,6 +278,10 @@ export class ValidationContext extends ASTValidationContext {
245
278
return this . _typeInfo . getInputType ( ) ;
246
279
}
247
280
281
+ getDefaultValue ( ) : Maybe < ConstValueNode > {
282
+ return this . _typeInfo . getDefaultValue ( ) as Maybe < ConstValueNode > ;
283
+ }
284
+
248
285
getParentInputType ( ) : Maybe < GraphQLInputType > {
249
286
return this . _typeInfo . getParentInputType ( ) ;
250
287
}
0 commit comments