1- import { DocumentNode } from 'graphql' ;
1+ import { query } from 'express' ;
2+ import { ASTNode , DocumentNode , Kind } from 'graphql' ;
23
34/**
45 * This function should
@@ -11,18 +12,85 @@ import { DocumentNode } from 'graphql';
1112 *
1213 * TO DO: extend the functionality to work for mutations and subscriptions
1314 *
14- * @param {string } queryString
15- * @param {TypeWeightObject } typeWeights
15+ * @param {string } queryAST
1616 * @param {any | undefined } varibales
17- * @param {string } complexityOption
17+ * @param {TypeWeightObject } typeWeights
1818 */
1919// TODO add queryVaribables parameter
2020function getQueryTypeComplexity (
21- queryString : DocumentNode ,
21+ queryAST : DocumentNode ,
2222 varibales : any | undefined ,
2323 typeWeights : TypeWeightObject
2424) : number {
25- throw Error ( 'getQueryComplexity is not implemented.' ) ;
25+ const recursive = ( node : ASTNode , parent : ASTNode | null = null ) : number => {
26+ /**
27+ * pseudo code of the process
28+ *
29+ // if 'kind' property is 'Document'
30+ // iterate through queryAST.definitions array
31+ // call recursive with object
32+
33+ // if 'kind' property is 'operationDefinition'
34+ // check 'operation' value against the type weights and add to total
35+ // call recursive with selectionSet property if it is not undefined
36+
37+ // if 'kind' is 'selectionSet'
38+ // iterate shrough the 'selections' array of fields
39+ // if 'selectinSet' is not undefined, call recursive with the field
40+
41+ // if 'kind' property is 'feild'
42+ // check the fields name.value against the type weights and total
43+ // if there is a match, it is an objcet type with feilds,
44+ // call recursive with selectionSet property if it is not undefined
45+ // if it is not a match, it is a scalar field, look in the parent.name.value to check type weights feilds
46+ */
47+
48+ let complexity = 0 ;
49+ const parentName : string = parent ?. operation || parent ?. name . value || null ;
50+
51+ if ( node . kind === Kind . DOCUMENT ) {
52+ // if 'kind' property is a 'Document'
53+ // iterate through queryAST.definitions array
54+ for ( let i = 0 ; i < node . definitions . length ; i + 1 ) {
55+ // call recursive with the definition node
56+ complexity += recursive ( node . definitions [ i ] , node ) ;
57+ }
58+ } else if ( node . kind === Kind . OPERATION_DEFINITION ) {
59+ // if 'kind' property is 'operationDefinition'
60+ // TODO: case-sensitive
61+ if ( node . operation in typeWeights ) {
62+ // check 'operation' value against the type weights and add to total
63+ complexity += typeWeights [ node . operation ] . weight ;
64+ // call recursive with selectionSet property if it is not undefined
65+ if ( node . selectionSet ) complexity += recursive ( node . selectionSet , node ) ;
66+ }
67+ } else if ( node . kind === Kind . SELECTION_SET ) {
68+ // if 'kind' is 'selectionSet'
69+ // iterate shrough the 'selections' array of fields
70+ for ( let i = 0 ; i < node . selections . length ; i + 1 ) {
71+ // call recursive with the field
72+ complexity += recursive ( node . selections [ i ] , parent ) ; // passing the current parent through because selection sets act only as intermediaries
73+ }
74+ } else if ( node . kind === Kind . FIELD ) {
75+ // if 'kind' property is 'field'
76+ // check the fields name.value against the type weights and total
77+ // TODO: case-sensitive
78+ if ( node . name . value in typeWeights ) {
79+ // if there is a match, it is an objcet type with feilds,
80+ complexity += typeWeights [ node . name . value ] . weight ;
81+ // call recursive with selectionSet property if it is not undefined
82+ if ( node . selectionSet ) complexity += recursive ( node . selectionSet , node ) ;
83+ // node.name.value in typeWeights[parent.operation || parent.name.value].fields
84+ } else if ( parent ?. opeartion ) {
85+ // if it is not a match, it is a scalar field, look in the parent.name.value to check type weights feilds
86+ // TODO: if it is a list, need to look at the parent
87+ complexity += typeWeights [ parent . name . value ] . fields [ node . name . value ] ;
88+ }
89+ }
90+
91+ return complexity ;
92+ } ;
93+ return recursive ( queryAST ) ;
2694}
2795
2896export default getQueryTypeComplexity ;
0 commit comments