@@ -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 }
0 commit comments