1+ /* eslint-disable @typescript-eslint/no-explicit-any */
2+ /* eslint-disable @typescript-eslint/no-use-before-define */
13/**
24 * Created by Ivo Meißner on 28.07.17.
35 */
@@ -11,11 +13,9 @@ import {
1113 ValidationContext ,
1214 FragmentDefinitionNode ,
1315 OperationDefinitionNode ,
14- DirectiveNode ,
1516 FieldNode ,
1617 FragmentSpreadNode ,
1718 InlineFragmentNode ,
18- assertCompositeType ,
1919 GraphQLField , isCompositeType , GraphQLCompositeType , GraphQLFieldMap ,
2020 GraphQLSchema , DocumentNode , TypeInfo ,
2121 visit , visitWithTypeInfo ,
@@ -31,11 +31,11 @@ import {
3131} from 'graphql' ;
3232
3333export type ComplexityEstimatorArgs = {
34- type : GraphQLCompositeType ,
35- field : GraphQLField < any , any > ,
36- node : FieldNode ,
37- args : { [ key : string ] : any } ,
38- childComplexity : number
34+ type : GraphQLCompositeType ;
35+ field : GraphQLField < any , any > ;
36+ node : FieldNode ;
37+ args : { [ key : string ] : any } ;
38+ childComplexity : number ;
3939}
4040
4141export type ComplexityEstimator = ( options : ComplexityEstimatorArgs ) => number | void ;
@@ -45,27 +45,27 @@ export type Complexity = any;
4545
4646// Map of complexities for possible types (of Union, Interface types)
4747type ComplexityMap = {
48- [ typeName : string ] : number ,
48+ [ typeName : string ] : number ;
4949}
5050
5151export interface QueryComplexityOptions {
5252 // The maximum allowed query complexity, queries above this threshold will be rejected
53- maximumComplexity : number ,
53+ maximumComplexity : number ;
5454
5555 // The query variables. This is needed because the variables are not available
5656 // in the visitor of the graphql-js library
57- variables ?: Object ,
57+ variables ?: Record < string , any > ;
5858
5959 // specify operation name only when pass multi-operation documents
60- operationName ?: string ,
60+ operationName ?: string ;
6161
6262 // Optional callback function to retrieve the determined query complexity
6363 // Will be invoked whether the query is rejected or not
6464 // This can be used for logging or to implement rate limiting
65- onComplete ?: ( complexity : number ) => void ,
65+ onComplete ?: ( complexity : number ) => void ;
6666
6767 // Optional function to create a custom error
68- createError ?: ( max : number , actual : number ) => GraphQLError ,
68+ createError ?: ( max : number , actual : number ) => GraphQLError ;
6969
7070 // An array of complexity estimators to use for estimating the complexity
7171 estimators : Array < ComplexityEstimator > ;
@@ -79,11 +79,11 @@ function queryComplexityMessage(max: number, actual: number): string {
7979}
8080
8181export function getComplexity ( options : {
82- estimators : ComplexityEstimator [ ] ,
83- schema : GraphQLSchema ,
84- query : DocumentNode ,
85- variables ?: Object ,
86- operationName ?: string
82+ estimators : ComplexityEstimator [ ] ;
83+ schema : GraphQLSchema ;
84+ query : DocumentNode ;
85+ variables ?: Record < string , any > ;
86+ operationName ?: string ;
8787} ) : number {
8888 const typeInfo = new TypeInfo ( options . schema ) ;
8989
@@ -104,7 +104,7 @@ export default class QueryComplexity {
104104 context : ValidationContext ;
105105 complexity : number ;
106106 options : QueryComplexityOptions ;
107- OperationDefinition : Object ;
107+ OperationDefinition : Record < string , any > ;
108108 estimators : Array < ComplexityEstimator > ;
109109 includeDirectiveDef : GraphQLDirective ;
110110 skipDirectiveDef : GraphQLDirective ;
@@ -123,15 +123,15 @@ export default class QueryComplexity {
123123
124124 this . includeDirectiveDef = this . context . getSchema ( ) . getDirective ( 'include' ) ;
125125 this . skipDirectiveDef = this . context . getSchema ( ) . getDirective ( 'skip' ) ;
126- this . estimators = options . estimators
126+ this . estimators = options . estimators ;
127127
128128 this . OperationDefinition = {
129129 enter : this . onOperationDefinitionEnter ,
130130 leave : this . onOperationDefinitionLeave
131131 } ;
132132 }
133133
134- onOperationDefinitionEnter ( operation : OperationDefinitionNode ) {
134+ onOperationDefinitionEnter ( operation : OperationDefinitionNode ) : void {
135135 if ( typeof this . options . operationName === 'string' && this . options . operationName !== operation . name . value ) {
136136 return ;
137137 }
@@ -181,7 +181,7 @@ export default class QueryComplexity {
181181 typeDef : GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType ,
182182 ) : number {
183183 if ( node . selectionSet ) {
184- let fields :GraphQLFieldMap < any , any > = { } ;
184+ let fields : GraphQLFieldMap < any , any > = { } ;
185185 if ( typeDef instanceof GraphQLObjectType || typeDef instanceof GraphQLInterfaceType ) {
186186 fields = typeDef . getFields ( ) ;
187187 }
@@ -191,18 +191,19 @@ export default class QueryComplexity {
191191 if ( isAbstractType ( typeDef ) ) {
192192 possibleTypeNames = this . context . getSchema ( ) . getPossibleTypes ( typeDef ) . map ( t => t . name ) ;
193193 } else {
194- possibleTypeNames = [ typeDef . name ] ;
194+ possibleTypeNames = [ typeDef . name ] ;
195195 }
196196
197197 // Collect complexities for all possible types individually
198198 const selectionSetComplexities : ComplexityMap = node . selectionSet . selections . reduce (
199199 ( complexities : ComplexityMap , childNode : FieldNode | FragmentSpreadNode | InlineFragmentNode ) => {
200200 // let nodeComplexity = 0;
201+ let innerComplexities = complexities ;
201202
202203 let includeNode = true ;
203204 let skipNode = false ;
204205
205- childNode . directives ?. forEach ( ( directive : DirectiveNode ) => {
206+ for ( const directive of childNode . directives ?? [ ] ) {
206207 const directiveName = directive . name . value ;
207208 switch ( directiveName ) {
208209 case 'include' : {
@@ -216,7 +217,7 @@ export default class QueryComplexity {
216217 break ;
217218 }
218219 }
219- } ) ;
220+ }
220221
221222 if ( ! includeNode || skipNode ) {
222223 return complexities ;
@@ -258,7 +259,7 @@ export default class QueryComplexity {
258259 const tmpComplexity = estimator ( estimatorArgs ) ;
259260
260261 if ( typeof tmpComplexity === 'number' && ! isNaN ( tmpComplexity ) ) {
261- complexities = addComplexities (
262+ innerComplexities = addComplexities (
262263 tmpComplexity ,
263264 complexities ,
264265 possibleTypeNames ,
@@ -292,17 +293,17 @@ export default class QueryComplexity {
292293 const nodeComplexity = this . nodeComplexity ( fragment , fragmentType ) ;
293294 if ( isAbstractType ( fragmentType ) ) {
294295 // Add fragment complexity for all possible types
295- complexities = addComplexities (
296+ innerComplexities = addComplexities (
296297 nodeComplexity ,
297298 complexities ,
298299 this . context . getSchema ( ) . getPossibleTypes ( fragmentType ) . map ( t => t . name ) ,
299300 ) ;
300301 } else {
301302 // Add complexity for object type
302- complexities = addComplexities (
303+ innerComplexities = addComplexities (
303304 nodeComplexity ,
304305 complexities ,
305- [ fragmentType . name ] ,
306+ [ fragmentType . name ] ,
306307 ) ;
307308 }
308309 break ;
@@ -319,23 +320,23 @@ export default class QueryComplexity {
319320 const nodeComplexity = this . nodeComplexity ( childNode , inlineFragmentType ) ;
320321 if ( isAbstractType ( inlineFragmentType ) ) {
321322 // Add fragment complexity for all possible types
322- complexities = addComplexities (
323+ innerComplexities = addComplexities (
323324 nodeComplexity ,
324325 complexities ,
325326 this . context . getSchema ( ) . getPossibleTypes ( inlineFragmentType ) . map ( t => t . name ) ,
326327 ) ;
327328 } else {
328329 // Add complexity for object type
329- complexities = addComplexities (
330+ innerComplexities = addComplexities (
330331 nodeComplexity ,
331332 complexities ,
332- [ inlineFragmentType . name ] ,
333+ [ inlineFragmentType . name ] ,
333334 ) ;
334335 }
335336 break ;
336337 }
337338 default : {
338- complexities = addComplexities (
339+ innerComplexities = addComplexities (
339340 this . nodeComplexity ( childNode , typeDef ) ,
340341 complexities ,
341342 possibleTypeNames ,
@@ -344,7 +345,7 @@ export default class QueryComplexity {
344345 }
345346 }
346347
347- return complexities ;
348+ return innerComplexities ;
348349 } , { } ) ;
349350 // Only return max complexity of all possible types
350351 if ( ! selectionSetComplexities ) {
@@ -381,8 +382,8 @@ function addComplexities(
381382 possibleTypes : string [ ] ,
382383) : ComplexityMap {
383384 for ( const type of possibleTypes ) {
384- if ( complexityMap . hasOwnProperty ( type ) ) {
385- complexityMap [ type ] = complexityMap [ type ] + complexity ;
385+ if ( Object . prototype . hasOwnProperty . call ( complexityMap , type ) ) {
386+ complexityMap [ type ] += complexity ;
386387 } else {
387388 complexityMap [ type ] = complexity ;
388389 }
0 commit comments