@@ -15,6 +15,8 @@ import {
1515 InlineFragmentNode ,
1616 assertCompositeType ,
1717 GraphQLField , isCompositeType , GraphQLCompositeType , GraphQLFieldMap ,
18+ GraphQLSchema , DocumentNode , TypeInfo ,
19+ visit , visitWithTypeInfo
1820} from 'graphql' ;
1921import {
2022 GraphQLUnionType ,
@@ -74,6 +76,26 @@ function queryComplexityMessage(max: number, actual: number): string {
7476 ) ;
7577}
7678
79+ export function calculateComplexity ( options : {
80+ estimators : ComplexityEstimator [ ] ,
81+ schema : GraphQLSchema ,
82+ query : DocumentNode ,
83+ variables ?: Object
84+ } ) : number {
85+ const typeInfo = new TypeInfo ( options . schema ) ;
86+
87+ const context = new ValidationContext ( options . schema , options . query , typeInfo ) ;
88+ const visitor = new QueryComplexity ( context , {
89+ // Maximum complexity does not matter since we're only interested in the calculated complexity.
90+ maximumComplexity : Infinity ,
91+ estimators : options . estimators ,
92+ variables : options . variables
93+ } ) ;
94+
95+ visit ( options . query , visitWithTypeInfo ( typeInfo , visitor ) ) ;
96+ return visitor . complexity ;
97+ }
98+
7799export default class QueryComplexity {
78100 context : ValidationContext ;
79101 complexity : number ;
0 commit comments