Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ type ComplexityEstimatorArgs = {
// The GraphQLField that is being evaluated
field: GraphQLField<any, any>,

// The GraphQL node that is being evaluated
node: FieldNode,

// The input arguments of the field
args: {[key: string]: any},

Expand Down
2 changes: 2 additions & 0 deletions src/QueryComplexity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
export type ComplexityEstimatorArgs = {
type: GraphQLCompositeType,
field: GraphQLField<any, any>,
node: FieldNode,
args: {[key: string]: any},
childComplexity: number
}
Expand Down Expand Up @@ -250,6 +251,7 @@ export default class QueryComplexity {
childComplexity,
args,
field,
node: childNode,
type: typeDef
};
const validScore = this.estimators.find(estimator => {
Expand Down
28 changes: 27 additions & 1 deletion src/__tests__/QueryComplexity-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {expect} from 'chai';

import schema from './fixtures/schema';

import ComplexityVisitor, {getComplexity} from '../QueryComplexity';
import ComplexityVisitor, {getComplexity, ComplexityEstimator} from '../QueryComplexity';
import {
simpleEstimator,
directiveEstimator,
Expand Down Expand Up @@ -758,4 +758,30 @@ describe('QueryComplexity analysis', () => {
});
expect(complexity).to.equal(41); // 1 for interface, 20 * 2 for complexScalar
});

it('should include the current node in the estimator args', () => {
const ast = parse(`
query {
nonNullItem {
scalar
complexScalar
variableScalar(count: 10)
}
}
`);

const fieldCountEstimator: ComplexityEstimator = ({ node }) => {
if (node.selectionSet) {
return 10 * node.selectionSet.selections.length;
}
return 0;
};

const complexity = getComplexity({
estimators: [ fieldCountEstimator ],
schema,
query: ast
});
expect(complexity).to.equal(30); // 3 fields on nonNullItem * 10
});
});