|
| 1 | +/* eslint-disable @typescript-eslint/no-use-before-define */ |
| 2 | +import { |
| 3 | + DocumentNode, |
| 4 | + FieldNode, |
| 5 | + SelectionSetNode, |
| 6 | + DefinitionNode, |
| 7 | + Kind, |
| 8 | + SelectionNode, |
| 9 | + ArgumentNode, |
| 10 | +} from 'graphql'; |
| 11 | +import { FieldWeight, TypeWeightObject } from '../@types/buildTypeWeights'; |
| 12 | + |
| 13 | +// TODO: handle variables and arguments |
| 14 | +// ! this is not functional |
| 15 | +const getArgObj = (args: ArgumentNode[]): { [index: string]: any } => { |
| 16 | + const argObj: { [index: string]: any } = {}; |
| 17 | + for (let i = 0; i < args.length; i + 1) { |
| 18 | + const node = args[i]; |
| 19 | + if (args[i].value.kind !== Kind.VARIABLE) { |
| 20 | + if (args[i].value.kind === Kind.INT) { |
| 21 | + // FIXME: this does not work |
| 22 | + argObj[args[i].name.value] = args[i].value; |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + return argObj; |
| 27 | +}; |
| 28 | +/** |
| 29 | + * The AST node functions call each other following the nested structure below |
| 30 | + * Each function handles a specific GraphQL AST node type |
| 31 | + * |
| 32 | + * AST nodes call each other in the following way |
| 33 | + * |
| 34 | + * Document Node |
| 35 | + * | |
| 36 | + * Definiton Node |
| 37 | + * (operation and fragment definitons) |
| 38 | + * / \ |
| 39 | + * |-----> Selection Set Node not done |
| 40 | + * | / |
| 41 | + * | Selection Node |
| 42 | + * | (Field, Inline fragment and fragment spread) |
| 43 | + * | | \ \ |
| 44 | + * |--Field Node not done not done |
| 45 | + * |
| 46 | + */ |
| 47 | + |
| 48 | +export function fieldNode( |
| 49 | + node: FieldNode, |
| 50 | + typeWeights: TypeWeightObject, |
| 51 | + variables: any | undefined, |
| 52 | + parentName: string |
| 53 | +): number { |
| 54 | + let complexity = 0; |
| 55 | + // console.log('fieldNode', node, parentName); |
| 56 | + // check if the field name is in the type weight object. |
| 57 | + if (node.name.value.toLocaleLowerCase() in typeWeights) { |
| 58 | + // if it is, than the field is an object type, add itss type weight to the total |
| 59 | + complexity += typeWeights[node.name.value].weight; |
| 60 | + // call the function to handle selection set node with selectionSet property if it is not undefined |
| 61 | + if (node.selectionSet) { |
| 62 | + complexity += selectionSetNode( |
| 63 | + node.selectionSet, |
| 64 | + typeWeights, |
| 65 | + variables, |
| 66 | + node.name.value |
| 67 | + ); |
| 68 | + } |
| 69 | + } else { |
| 70 | + // otherwise the field is a scalar or a list. |
| 71 | + const fieldWeight: FieldWeight = typeWeights[parentName].fields[node.name.value]; |
| 72 | + if (typeof fieldWeight === 'number') { |
| 73 | + // if the feild weight is a number, add the number to the total complexity |
| 74 | + complexity += fieldWeight; |
| 75 | + } else if (node.arguments) { |
| 76 | + // otherwise the the feild weight is a list, invoke the function with variables |
| 77 | + // TODO: calculate the complexity for lists with arguments and varibales |
| 78 | + // ! this is not functional |
| 79 | + // iterate through the arguments to build the object to |
| 80 | + complexity += fieldWeight([...node.arguments]); |
| 81 | + } |
| 82 | + } |
| 83 | + return complexity; |
| 84 | +} |
| 85 | + |
| 86 | +export function selectionNode( |
| 87 | + node: SelectionNode, |
| 88 | + typeWeights: TypeWeightObject, |
| 89 | + variables: any | undefined, |
| 90 | + parentName: string |
| 91 | +): number { |
| 92 | + let complexity = 0; |
| 93 | + // console.log('selectionNode', node, parentName); |
| 94 | + // check the kind property against the set of selection nodes that are possible |
| 95 | + if (node.kind === Kind.FIELD) { |
| 96 | + // call the function that handle field nodes |
| 97 | + complexity += fieldNode(node, typeWeights, variables, parentName); |
| 98 | + } |
| 99 | + // TODO: add checks for Kind.FRAGMENT_SPREAD and Kind.INLINE_FRAGMENT here |
| 100 | + return complexity; |
| 101 | +} |
| 102 | + |
| 103 | +export function selectionSetNode( |
| 104 | + node: SelectionSetNode, |
| 105 | + typeWeights: TypeWeightObject, |
| 106 | + variables: any | undefined, |
| 107 | + parentName: string |
| 108 | +): number { |
| 109 | + let complexity = 0; |
| 110 | + // iterate shrough the 'selections' array on the seletion set node |
| 111 | + for (let i = 0; i < node.selections.length; i += 1) { |
| 112 | + // call the function to handle seletion nodes |
| 113 | + // pass the current parent through because selection sets act only as intermediaries |
| 114 | + complexity += selectionNode(node.selections[i], typeWeights, variables, parentName); |
| 115 | + } |
| 116 | + return complexity; |
| 117 | +} |
| 118 | + |
| 119 | +export function definitionNode( |
| 120 | + node: DefinitionNode, |
| 121 | + typeWeights: TypeWeightObject, |
| 122 | + variables: any | undefined |
| 123 | +): number { |
| 124 | + let complexity = 0; |
| 125 | + // check the kind property against the set of definiton nodes that are possible |
| 126 | + if (node.kind === Kind.OPERATION_DEFINITION) { |
| 127 | + // check if the operation is in the type weights object. |
| 128 | + if (node.operation.toLocaleLowerCase() in typeWeights) { |
| 129 | + // if it is, it is an object type, add it's type weight to the total |
| 130 | + complexity += typeWeights[node.operation].weight; |
| 131 | + // call the function to handle selection set node with selectionSet property if it is not undefined |
| 132 | + if (node.selectionSet) |
| 133 | + complexity += selectionSetNode( |
| 134 | + node.selectionSet, |
| 135 | + typeWeights, |
| 136 | + variables, |
| 137 | + node.operation |
| 138 | + ); |
| 139 | + } |
| 140 | + } |
| 141 | + // TODO: add checks for Kind.FRAGMENT_DEFINITION here (there are other type definition nodes that i think we can ignore. see ast.d.ts in 'graphql') |
| 142 | + return complexity; |
| 143 | +} |
| 144 | + |
| 145 | +export function documentNode( |
| 146 | + node: DocumentNode, |
| 147 | + typeWeights: TypeWeightObject, |
| 148 | + variables: any | undefined |
| 149 | +): number { |
| 150 | + let complexity = 0; |
| 151 | + // iterate through 'definitions' array on the document node |
| 152 | + for (let i = 0; i < node.definitions.length; i += 1) { |
| 153 | + // call the function to handle the various types of definition nodes |
| 154 | + complexity += definitionNode(node.definitions[i], typeWeights, variables); |
| 155 | + } |
| 156 | + return complexity; |
| 157 | +} |
0 commit comments