Skip to content

Commit fde0ef6

Browse files
committed
Ignore fields without complexity directive in directiveEstimator #15
1 parent d237a07 commit fde0ef6

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

src/QueryComplexity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export default class QueryComplexity {
195195
const validScore = this.estimators.find(estimator => {
196196
const tmpComplexity = estimator(estimatorArgs);
197197

198-
if (typeof tmpComplexity === 'number') {
198+
if (typeof tmpComplexity === 'number' && !isNaN(tmpComplexity)) {
199199
nodeComplexity = tmpComplexity;
200200
return true;
201201
}
@@ -205,7 +205,7 @@ export default class QueryComplexity {
205205
if (!validScore) {
206206
return this.context.reportError(
207207
new GraphQLError(
208-
`No complexity could be calculated for field ${typeDef.astNode}.${field.name}. ` +
208+
`No complexity could be calculated for field ${typeDef.name}.${field.name}. ` +
209209
'At least one complexity estimator has to return a complexity score.'
210210
)
211211
);

src/__tests__/QueryComplexity-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ describe('QueryComplexity analysis', () => {
258258
visit(ast, visitWithTypeInfo(typeInfo, visitor));
259259
expect(context.getErrors().length).to.equal(1);
260260
expect(context.getErrors()[0].message).to.equal(
261-
'No complexity could be calculated for field undefined.scalar. ' +
261+
'No complexity could be calculated for field Query.scalar. ' +
262262
'At least one complexity estimator has to return a complexity score.'
263263
);
264264
});
@@ -279,7 +279,7 @@ describe('QueryComplexity analysis', () => {
279279
visit(ast, visitWithTypeInfo(typeInfo, visitor));
280280
expect(context.getErrors().length).to.equal(1);
281281
expect(context.getErrors()[0].message).to.equal(
282-
'No complexity could be calculated for field undefined.scalar. ' +
282+
'No complexity could be calculated for field Query.scalar. ' +
283283
'At least one complexity estimator has to return a complexity score.'
284284
);
285285
});

src/estimators/directive/__tests__/directiveEstimator-test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,26 @@ describe('directiveEstimator analysis', () => {
223223
visit(ast, visitWithTypeInfo(typeInfo, visitor));
224224
expect(visitor.complexity).to.equal(10);
225225
});
226+
227+
it('ignores fields without compexity directive', () => {
228+
const ast = parse(`
229+
query {
230+
noDirective
231+
}
232+
`);
233+
234+
const context = new ValidationContext(schema, ast, typeInfo);
235+
const visitor = new ComplexityVisitor(context, {
236+
maximumComplexity: 100,
237+
estimators: [
238+
directiveEstimator()
239+
]
240+
});
241+
242+
visit(ast, visitWithTypeInfo(typeInfo, visitor));
243+
expect(visitor.context.getErrors().length).to.equal(1);
244+
expect(visitor.context.getErrors()[0].message).to.include(
245+
'No complexity could be calculated for field Query.noDirective',
246+
);
247+
});
226248
});

src/estimators/directive/__tests__/fixtures/schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type Query {
2222
negativeCostScalar: String @complexity(value: -20)
2323
multiDirective: String @cost(value: 1) @complexity(value: 2)
2424
25+
noDirective: Boolean
26+
2527
childList(
2628
limit: Int,
2729
ids: [ID],

src/estimators/directive/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export default function (options?: {}): ComplexityEstimator {
3030
return (args: ComplexityEstimatorArgs) => {
3131
const values = getDirectiveValues(directive, args.field.astNode);
3232

33+
// Ignore if no directive set
34+
if (!values) {
35+
return;
36+
}
37+
3338
// Get multipliers
3439
let totalMultiplier = 1;
3540
if (values.multipliers) {

0 commit comments

Comments
 (0)