7
7
import {
8
8
getArgumentValues ,
9
9
getDirectiveValues ,
10
+ getVariableValues ,
10
11
} from 'graphql/execution/values' ;
11
12
12
13
import {
@@ -121,6 +122,7 @@ export default class QueryComplexity {
121
122
estimators : Array < ComplexityEstimator > ;
122
123
includeDirectiveDef : GraphQLDirective ;
123
124
skipDirectiveDef : GraphQLDirective ;
125
+ variableValues : Record < string , any > ;
124
126
125
127
constructor ( context : ValidationContext , options : QueryComplexityOptions ) {
126
128
if (
@@ -139,6 +141,7 @@ export default class QueryComplexity {
139
141
this . includeDirectiveDef = this . context . getSchema ( ) . getDirective ( 'include' ) ;
140
142
this . skipDirectiveDef = this . context . getSchema ( ) . getDirective ( 'skip' ) ;
141
143
this . estimators = options . estimators ;
144
+ this . variableValues = { } ;
142
145
143
146
this . OperationDefinition = {
144
147
enter : this . onOperationDefinitionEnter ,
@@ -154,6 +157,14 @@ export default class QueryComplexity {
154
157
return ;
155
158
}
156
159
160
+ // Get variable values from variables that are passed from options, merged
161
+ // with default values defined in the operation
162
+ this . variableValues = getVariableValues (
163
+ this . context . getSchema ( ) ,
164
+ operation . variableDefinitions ?? [ ] ,
165
+ this . options . variables ?? { }
166
+ ) . coerced ;
167
+
157
168
switch ( operation . operation ) {
158
169
case 'query' :
159
170
this . complexity += this . nodeComplexity (
@@ -246,7 +257,7 @@ export default class QueryComplexity {
246
257
const values = getDirectiveValues (
247
258
this . includeDirectiveDef ,
248
259
childNode ,
249
- this . options . variables || { }
260
+ this . variableValues || { }
250
261
) ;
251
262
includeNode = values . if ;
252
263
break ;
@@ -255,7 +266,7 @@ export default class QueryComplexity {
255
266
const values = getDirectiveValues (
256
267
this . skipDirectiveDef ,
257
268
childNode ,
258
- this . options . variables || { }
269
+ this . variableValues || { }
259
270
) ;
260
271
skipNode = values . if ;
261
272
break ;
@@ -282,7 +293,7 @@ export default class QueryComplexity {
282
293
args = getArgumentValues (
283
294
field ,
284
295
childNode ,
285
- this . options . variables || { }
296
+ this . variableValues || { }
286
297
) ;
287
298
} catch ( e ) {
288
299
return this . context . reportError ( e ) ;
0 commit comments