Skip to content

Commit 16291df

Browse files
committed
Moved complexity field config to dedicated LegacyEstimator
1 parent b7bc4df commit 16291df

File tree

5 files changed

+63
-39
lines changed

5 files changed

+63
-39
lines changed

src/QueryComplexity.ts

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3432
export 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

4139
export type ComplexityEstimator = (options: ComplexityEstimatorArgs) => number | void;
4240

4341
type ComplexityGraphQLField<TSource, TContext> = GraphQLField<TSource, TContext> & {
44-
complexity?: SimpleComplexityEstimator | number | undefined
42+
complexity?: any
4543
}
4644

4745
type 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
}

src/estimators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export {default as simpleEstimator} from './simple';
2+
export {default as legacyEstimator} from './legacy';

src/estimators/legacy/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {ComplexityEstimator, ComplexityEstimatorArgs} from '../../QueryComplexity';
2+
3+
export default function (): ComplexityEstimator {
4+
return (args: ComplexityEstimatorArgs) => {
5+
// Calculate complexity score
6+
if (typeof args.field.complexity === 'number') {
7+
return args.childComplexity + args.field.complexity;
8+
} else if (typeof args.field.complexity === 'function') {
9+
return args.field.complexity(args.args, args.childComplexity);
10+
}
11+
};
12+
}

src/estimators/simple/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Simple Estimator
2+
3+
The simple estimator just adds a fixed complexity to every field that is queried.
4+
This can be used as the last estimator in the chain to return the default value.
5+
6+
## Usage
7+
8+
````typescript
9+
import queryComplexity, {simpleEstimator} from 'graphql-query-complexity';
10+
11+
const rule = queryComplexity({
12+
estimators: [
13+
simpleEstimator({
14+
// Add a default complexity of 1 for each queried field
15+
defaultComplexity: 1
16+
})
17+
]
18+
// ... other config
19+
});
20+
````

src/estimators/simple.ts renamed to src/estimators/simple/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ComplexityEstimator, ComplexityEstimatorArgs} from '../QueryComplexity';
1+
import {ComplexityEstimator, ComplexityEstimatorArgs} from '../../QueryComplexity';
22

33
export default function (options?: {defaultComplexity?: number}): ComplexityEstimator {
44
const defaultComplexity = options && typeof options.defaultComplexity === 'number' ? options.defaultComplexity : 1;

0 commit comments

Comments
 (0)