Skip to content

Commit

Permalink
Ignore fields without complexity directive in directiveEstimator #15
Browse files Browse the repository at this point in the history
  • Loading branch information
ivome committed Feb 10, 2019
1 parent d237a07 commit fde0ef6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/QueryComplexity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export default class QueryComplexity {
const validScore = this.estimators.find(estimator => {
const tmpComplexity = estimator(estimatorArgs);

if (typeof tmpComplexity === 'number') {
if (typeof tmpComplexity === 'number' && !isNaN(tmpComplexity)) {
nodeComplexity = tmpComplexity;
return true;
}
Expand All @@ -205,7 +205,7 @@ export default class QueryComplexity {
if (!validScore) {
return this.context.reportError(
new GraphQLError(
`No complexity could be calculated for field ${typeDef.astNode}.${field.name}. ` +
`No complexity could be calculated for field ${typeDef.name}.${field.name}. ` +
'At least one complexity estimator has to return a complexity score.'
)
);
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/QueryComplexity-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ describe('QueryComplexity analysis', () => {
visit(ast, visitWithTypeInfo(typeInfo, visitor));
expect(context.getErrors().length).to.equal(1);
expect(context.getErrors()[0].message).to.equal(
'No complexity could be calculated for field undefined.scalar. ' +
'No complexity could be calculated for field Query.scalar. ' +
'At least one complexity estimator has to return a complexity score.'
);
});
Expand All @@ -279,7 +279,7 @@ describe('QueryComplexity analysis', () => {
visit(ast, visitWithTypeInfo(typeInfo, visitor));
expect(context.getErrors().length).to.equal(1);
expect(context.getErrors()[0].message).to.equal(
'No complexity could be calculated for field undefined.scalar. ' +
'No complexity could be calculated for field Query.scalar. ' +
'At least one complexity estimator has to return a complexity score.'
);
});
Expand Down
22 changes: 22 additions & 0 deletions src/estimators/directive/__tests__/directiveEstimator-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,26 @@ describe('directiveEstimator analysis', () => {
visit(ast, visitWithTypeInfo(typeInfo, visitor));
expect(visitor.complexity).to.equal(10);
});

it('ignores fields without compexity directive', () => {
const ast = parse(`
query {
noDirective
}
`);

const context = new ValidationContext(schema, ast, typeInfo);
const visitor = new ComplexityVisitor(context, {
maximumComplexity: 100,
estimators: [
directiveEstimator()
]
});

visit(ast, visitWithTypeInfo(typeInfo, visitor));
expect(visitor.context.getErrors().length).to.equal(1);
expect(visitor.context.getErrors()[0].message).to.include(
'No complexity could be calculated for field Query.noDirective',
);
});
});
2 changes: 2 additions & 0 deletions src/estimators/directive/__tests__/fixtures/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type Query {
negativeCostScalar: String @complexity(value: -20)
multiDirective: String @cost(value: 1) @complexity(value: 2)
noDirective: Boolean
childList(
limit: Int,
ids: [ID],
Expand Down
5 changes: 5 additions & 0 deletions src/estimators/directive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export default function (options?: {}): ComplexityEstimator {
return (args: ComplexityEstimatorArgs) => {
const values = getDirectiveValues(directive, args.field.astNode);

// Ignore if no directive set
if (!values) {
return;
}

// Get multipliers
let totalMultiplier = 1;
if (values.multipliers) {
Expand Down

0 comments on commit fde0ef6

Please sign in to comment.