Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6e0e3ff

Browse files
committed
Add node to ComplexityEstimatorArgs
1 parent d2fc8b3 commit 6e0e3ff

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

‎README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ type ComplexityEstimatorArgs = {
100100
// The GraphQLField that is being evaluated
101101
field: GraphQLField<any, any>,
102102

103+
// The GraphQL node that is being evaluated
104+
node: FieldNode,
105+
103106
// The input arguments of the field
104107
args: {[key: string]: any},
105108

‎src/QueryComplexity.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
export type ComplexityEstimatorArgs = {
3434
type: GraphQLCompositeType,
3535
field: GraphQLField<any, any>,
36+
node: FieldNode,
3637
args: {[key: string]: any},
3738
childComplexity: number
3839
}
@@ -250,6 +251,7 @@ export default class QueryComplexity {
250251
childComplexity,
251252
args,
252253
field,
254+
node: childNode,
253255
type: typeDef
254256
};
255257
const validScore = this.estimators.find(estimator => {

‎src/__tests__/QueryComplexity-test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {expect} from 'chai';
1313

1414
import schema from './fixtures/schema';
1515

16-
import ComplexityVisitor, {getComplexity} from '../QueryComplexity';
16+
import ComplexityVisitor, {getComplexity,ComplexityEstimator} from '../QueryComplexity';
1717
import {
1818
simpleEstimator,
1919
directiveEstimator,
@@ -758,4 +758,30 @@ describe('QueryComplexity analysis', () => {
758758
});
759759
expect(complexity).to.equal(41); // 1 for interface, 20 * 2 for complexScalar
760760
});
761+
762+
it('should include the current node in the estimator args', () => {
763+
const ast = parse(`
764+
query {
765+
nonNullItem {
766+
scalar
767+
complexScalar
768+
variableScalar(count: 10)
769+
}
770+
}
771+
`);
772+
773+
const fieldCountEstimator: ComplexityEstimator = ({ node }) => {
774+
if (node.selectionSet) {
775+
return 10 * node.selectionSet.selections.length;
776+
}
777+
return 0;
778+
};
779+
780+
const complexity = getComplexity({
781+
estimators: [ fieldCountEstimator ],
782+
schema,
783+
query: ast
784+
});
785+
expect(complexity).to.equal(30); // 3 fields on nonNullItem * 10
786+
});
761787
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /