11import { ASTNode , DocumentNode , Kind } from 'graphql' ;
2+ import {
3+ selectionSetNode ,
4+ fieldNode ,
5+ documentNode ,
6+ operationDefinitionNode ,
7+ } from './ASTnodefunctions' ;
28
39/**
410 * Calculate the complexity for the query by recursivly traversing through the query AST,
@@ -13,55 +19,12 @@ import { ASTNode, DocumentNode, Kind } from 'graphql';
1319// TODO add queryVaribables parameter
1420function getQueryTypeComplexity (
1521 queryAST : DocumentNode ,
16- varibales : any | undefined ,
22+ variables : any | undefined ,
1723 typeWeights : TypeWeightObject
1824) : number {
19- const getComplexityOfNode = ( node : ASTNode , parent : ASTNode = node ) : number => {
20- let complexity = 0 ;
21-
22- if ( node . kind === Kind . DOCUMENT ) {
23- // if 'kind' property is a 'Document'
24- // iterate through queryAST.definitions array
25- for ( let i = 0 ; i < node . definitions . length ; i + 1 ) {
26- // call recursive with the definition node
27- complexity += getComplexityOfNode ( node . definitions [ i ] , node ) ;
28- }
29- } else if ( node . kind === Kind . OPERATION_DEFINITION ) {
30- // if 'kind' property is 'operationDefinition'
31- // TODO: case-sensitive
32- if ( node . operation . toLocaleLowerCase ( ) in typeWeights ) {
33- // check 'operation' value against the type weights and add to total
34- complexity += typeWeights [ node . operation ] . weight ;
35- // call recursive with selectionSet property if it is not undefined
36- if ( node . selectionSet ) complexity += getComplexityOfNode ( node . selectionSet , node ) ;
37- }
38- } else if ( node . kind === Kind . SELECTION_SET ) {
39- // if 'kind' is 'selectionSet'
40- // iterate shrough the 'selections' array of fields
41- for ( let i = 0 ; i < node . selections . length ; i + 1 ) {
42- // call recursive with the field
43- complexity += getComplexityOfNode ( node . selections [ i ] , parent ) ; // passing the current parent through because selection sets act only as intermediaries
44- }
45- } else if ( node . kind === Kind . FIELD ) {
46- // if 'kind' property is 'field'
47- // check the fields name.value against the type weights and total
48- // TODO: case-sensitive
49- if ( node . name . value . toLocaleLowerCase ( ) in typeWeights ) {
50- // if there is a match, it is an objcet type with feilds,
51- complexity += typeWeights [ node . name . value ] . weight ;
52- // call recursive with selectionSet property if it is not undefined
53- if ( node . selectionSet ) complexity += getComplexityOfNode ( node . selectionSet , node ) ;
54- // node.name.value in typeWeights[parent.operation || parent.name.value].fields
55- } else {
56- // TODO: if it is not a match, it is a scalar field or list,
57- // if (parent?.objective !== null) {
58- // }
59- // const weight = typeWeights[parent.name.value].fields[node.name.value];
60- }
61- }
62- return complexity ;
63- } ;
64- return getComplexityOfNode ( queryAST ) ;
25+ let complexity = 0 ;
26+ complexity += documentNode ( queryAST , typeWeights , variables ) ;
27+ return complexity ;
6528}
6629
6730export default getQueryTypeComplexity ;
0 commit comments