@@ -24,24 +24,22 @@ import {
2424 getNamedType ,
2525 GraphQLError
2626} from 'graphql' ;
27- import { simpleEstimator } from './estimators' ;
28-
29- /**
30- * @deprecated Use new complexity resolver
31- */
32- type SimpleComplexityEstimator = ( args : any , complexity : number ) => number ;
27+ import {
28+ simpleEstimator ,
29+ legacyEstimator
30+ } from './estimators' ;
3331
3432export type ComplexityEstimatorArgs = {
3533 type : GraphQLCompositeType ,
36- field : GraphQLField < any , any > ,
34+ field : ComplexityGraphQLField < any , any > ,
3735 args : { [ key : string ] : any } ,
3836 childComplexity : number
3937}
4038
4139export type ComplexityEstimator = ( options : ComplexityEstimatorArgs ) => number | void ;
4240
4341type ComplexityGraphQLField < TSource , TContext > = GraphQLField < TSource , TContext > & {
44- complexity ?: SimpleComplexityEstimator | number | undefined
42+ complexity ?: any
4543}
4644
4745type ComplexityGraphQLFieldMap < TSource , TContext > = {
@@ -95,6 +93,7 @@ export default class QueryComplexity {
9593 this . complexity = 0 ;
9694 this . options = options ;
9795 this . estimators = options . estimators || [
96+ legacyEstimator ( ) ,
9897 simpleEstimator ( )
9998 ] ;
10099
@@ -178,37 +177,29 @@ export default class QueryComplexity {
178177 childComplexity = this . nodeComplexity ( childNode , fieldType ) ;
179178 }
180179
181- // Calculate complexity score
182- if ( typeof field . complexity === 'number' ) {
183- nodeComplexity = childComplexity + field . complexity ;
184- } else if ( typeof field . complexity === 'function' ) {
185- nodeComplexity = field . complexity ( args , childComplexity ) ;
186- } else {
187- // Run estimators one after another and return first valid complexity
188- // score
189- const estimatorArgs : ComplexityEstimatorArgs = {
190- childComplexity,
191- args,
192- field,
193- type : typeDef
194- } ;
195- const validScore = this . estimators . find ( estimator => {
196- const tmpComplexity = estimator ( estimatorArgs ) ;
197-
198- if ( typeof tmpComplexity === 'number' ) {
199- nodeComplexity = tmpComplexity ;
200- return true ;
201- }
202-
203- return false ;
204- } ) ;
205- if ( ! validScore ) {
206- throw new Error (
207- `No complexity could be calculated for field ${ typeDef . astNode } .${ field . name } . ` +
208- 'Make sure you always have at least one estimator configured that returns a value.'
209- ) ;
180+ // Run estimators one after another and return first valid complexity
181+ // score
182+ const estimatorArgs : ComplexityEstimatorArgs = {
183+ childComplexity,
184+ args,
185+ field,
186+ type : typeDef
187+ } ;
188+ const validScore = this . estimators . find ( estimator => {
189+ const tmpComplexity = estimator ( estimatorArgs ) ;
190+
191+ if ( typeof tmpComplexity === 'number' ) {
192+ nodeComplexity = tmpComplexity ;
193+ return true ;
210194 }
211- // nodeComplexity = this.getDefaultComplexity(args, childComplexity);
195+
196+ return false ;
197+ } ) ;
198+ if ( ! validScore ) {
199+ throw new Error (
200+ `No complexity could be calculated for field ${ typeDef . astNode } .${ field . name } . ` +
201+ 'At least one complexity estimator has to return a complexity score.'
202+ ) ;
212203 }
213204 break ;
214205 }
0 commit comments