Skip to content

Commit 66b68e7

Browse files
feat: support query by operationName, related #22
1 parent 75f5c4c commit 66b68e7

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const rule = queryComplexity({
3434
// The query variables. This is needed because the variables are not available
3535
// in the visitor of the graphql-js library
3636
variables: {},
37+
38+
// specify operation name only when pass multi-operation documents
39+
operationName?: string,
3740

3841
// Optional callback function to retrieve the determined query complexity
3942
// Will be invoked whether the query is rejected or not

src/QueryComplexity.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ export interface QueryComplexityOptions {
6060
// in the visitor of the graphql-js library
6161
variables?: Object,
6262

63+
// specify operation name only when pass multi-operation documents
64+
operationName?: string,
65+
6366
// Optional callback function to retrieve the determined query complexity
6467
// Will be invoked whether the query is rejected or not
6568
// This can be used for logging or to implement rate limiting
@@ -83,7 +86,8 @@ export function getComplexity(options: {
8386
estimators: ComplexityEstimator[],
8487
schema: GraphQLSchema,
8588
query: DocumentNode,
86-
variables?: Object
89+
variables?: Object,
90+
operationName?: string
8791
}): number {
8892
const typeInfo = new TypeInfo(options.schema);
8993

@@ -92,7 +96,8 @@ export function getComplexity(options: {
9296
// Maximum complexity does not matter since we're only interested in the calculated complexity.
9397
maximumComplexity: Infinity,
9498
estimators: options.estimators,
95-
variables: options.variables
99+
variables: options.variables,
100+
operationName: options.operationName
96101
});
97102

98103
visit(options.query, visitWithTypeInfo(typeInfo, visitor));
@@ -141,6 +146,10 @@ export default class QueryComplexity {
141146
}
142147

143148
onOperationDefinitionEnter(operation: OperationDefinitionNode) {
149+
if (typeof this.options.operationName === 'string' && this.options.operationName !== operation.name.value) {
150+
return;
151+
}
152+
144153
switch (operation.operation) {
145154
case 'query':
146155
this.complexity += this.nodeComplexity(
@@ -165,7 +174,11 @@ export default class QueryComplexity {
165174
}
166175
}
167176

168-
onOperationDefinitionLeave(): GraphQLError | undefined {
177+
onOperationDefinitionLeave(operation: OperationDefinitionNode): GraphQLError | undefined {
178+
if (typeof this.options.operationName === 'string' && this.options.operationName !== operation.name.value) {
179+
return;
180+
}
181+
169182
if (this.options.onComplete) {
170183
this.options.onComplete(this.complexity);
171184
}

src/__tests__/QueryComplexity-test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,4 +448,38 @@ describe('QueryComplexity analysis', () => {
448448
});
449449
expect(complexity).to.equal(2);
450450
});
451+
452+
it('should calculate complexity for specific operation', () => {
453+
const ast = parse(`
454+
query Primary {
455+
scalar
456+
complexScalar
457+
}
458+
459+
query Secondary {
460+
complexScalar
461+
}
462+
`);
463+
464+
const complexity1 = getComplexity({
465+
estimators: [
466+
fieldConfigEstimator(),
467+
simpleEstimator({defaultComplexity: 1})
468+
],
469+
schema,
470+
query: ast
471+
});
472+
expect(complexity1).to.equal(41);
473+
474+
const complexity2 = getComplexity({
475+
estimators: [
476+
fieldConfigEstimator(),
477+
simpleEstimator({defaultComplexity: 1})
478+
],
479+
schema,
480+
query: ast,
481+
operationName: 'Secondary'
482+
});
483+
expect(complexity2).to.equal(20);
484+
});
451485
});

0 commit comments

Comments
 (0)