@@ -60,6 +60,9 @@ export interface QueryComplexityOptions {
60
60
// in the visitor of the graphql-js library
61
61
variables ?: Object ,
62
62
63
+ // specify operation name only when pass multi-operation documents
64
+ operationName ?: string ,
65
+
63
66
// Optional callback function to retrieve the determined query complexity
64
67
// Will be invoked whether the query is rejected or not
65
68
// This can be used for logging or to implement rate limiting
@@ -83,7 +86,8 @@ export function getComplexity(options: {
83
86
estimators : ComplexityEstimator [ ] ,
84
87
schema : GraphQLSchema ,
85
88
query : DocumentNode ,
86
- variables ?: Object
89
+ variables ?: Object ,
90
+ operationName ?: string
87
91
} ) : number {
88
92
const typeInfo = new TypeInfo ( options . schema ) ;
89
93
@@ -92,7 +96,8 @@ export function getComplexity(options: {
92
96
// Maximum complexity does not matter since we're only interested in the calculated complexity.
93
97
maximumComplexity : Infinity ,
94
98
estimators : options . estimators ,
95
- variables : options . variables
99
+ variables : options . variables ,
100
+ operationName : options . operationName
96
101
} ) ;
97
102
98
103
visit ( options . query , visitWithTypeInfo ( typeInfo , visitor ) ) ;
@@ -141,6 +146,10 @@ export default class QueryComplexity {
141
146
}
142
147
143
148
onOperationDefinitionEnter ( operation : OperationDefinitionNode ) {
149
+ if ( typeof this . options . operationName === 'string' && this . options . operationName !== operation . name . value ) {
150
+ return ;
151
+ }
152
+
144
153
switch ( operation . operation ) {
145
154
case 'query' :
146
155
this . complexity += this . nodeComplexity (
@@ -165,7 +174,11 @@ export default class QueryComplexity {
165
174
}
166
175
}
167
176
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
+
169
182
if ( this . options . onComplete ) {
170
183
this . options . onComplete ( this . complexity ) ;
171
184
}
0 commit comments