-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQueryParser.ts
More file actions
309 lines (281 loc) · 13.3 KB
/
QueryParser.ts
File metadata and controls
309 lines (281 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import {
DocumentNode,
FieldNode,
SelectionSetNode,
DefinitionNode,
Kind,
DirectiveNode,
SelectionNode,
} from 'graphql';
import { FieldWeight, TypeWeightObject, Variables } from '../@types/buildTypeWeights.js';
/**
* The AST node functions call each other following the nested structure below
* Each function handles a specific GraphQL AST node type
*
* AST nodes call each other in the following way
*
* Document Node
* |
* Definiton Node
* (operation and fragment definitons)
* / |
* |-----> Selection Set Node <-------|
* | /
* | Selection Node
* | (Field, Inline fragment and fragment spread)
* | | | \
* | Field Node | fragmentCache
* | | |
* |<--calculateCast |
* | |
* |<------------------|
*/
class QueryParser {
private typeWeights: TypeWeightObject;
private depth: number;
public maxDepth: number;
private variables: Variables;
private fragmentCache: { [index: string]: { complexity: number; depth: number } };
constructor(typeWeights: TypeWeightObject, variables: Variables) {
this.typeWeights = typeWeights;
this.variables = variables;
this.fragmentCache = {};
this.depth = 0;
this.maxDepth = 0;
}
private calculateCost(
node: FieldNode,
parentName: string,
typeName: string,
typeWeight: FieldWeight
) {
let complexity = 0;
// field resolves to an object or a list with possible selections
let selectionsCost = 0;
let calculatedWeight = 0;
if (node.selectionSet) {
selectionsCost += this.selectionSetNode(node.selectionSet, typeName);
}
// if there are arguments and this is a list, call the 'weightFunction' to get the weight of this field. otherwise the weight is static and can be accessed through the typeWeights object
if (node.arguments && typeof typeWeight === 'function') {
// FIXME: May never happen but what if weight is a function and arguments don't exist
calculatedWeight += typeWeight([...node.arguments], this.variables, selectionsCost);
} else if (typeof typeWeight === 'number') {
calculatedWeight += typeWeight + selectionsCost;
} else {
calculatedWeight += this.typeWeights[typeName].weight + selectionsCost;
}
complexity += calculatedWeight;
return complexity;
}
private fieldNode(node: FieldNode, parentName: string): number {
try {
let complexity = 0;
// the node must have a parent in typeweights or the analysis will fail. this should never happen
const parentType = this.typeWeights[parentName];
if (!parentType) {
throw new Error(
`ERROR: QueryParser Failed to obtain parentType for parent: ${parentName} and node: ${node.name.value}`
);
}
let typeName: string | undefined;
let typeWeight: FieldWeight | undefined;
if (node.name.value === '__typename') return complexity; // this will be zero, ie. this field has no complexity
if (node.name.value in this.typeWeights) {
// node is an object type in the typeWeight root
typeName = node.name.value;
typeWeight = this.typeWeights[typeName].weight;
complexity += this.calculateCost(node, parentName, typeName, typeWeight);
} else if (parentType.fields[node.name.value].resolveTo) {
// node is a field on a typeWeight root, field resolves to another type in type weights or a list
typeName = parentType.fields[node.name.value].resolveTo;
typeWeight = parentType.fields[node.name.value].weight;
// if this is a list typeWeight is a weight function
// otherwise the weight would be null as the weight is defined on the typeWeights root
if (typeName && typeWeight) {
// Type is a list and has a weight function
complexity += this.calculateCost(node, parentName, typeName, typeWeight);
} else if (typeName) {
// resolve type exists at root of typeWeight object and is not a list
typeWeight = this.typeWeights[typeName].weight;
complexity += this.calculateCost(node, parentName, typeName, typeWeight);
} else {
throw new Error(
`ERROR: QueryParser Failed to obtain resolved type name or weight for node: ${parentName}.${node.name.value}`
);
}
} else {
// field is a scalar
typeName = node.name.value;
if (typeName) {
typeWeight = parentType.fields[typeName].weight;
if (typeof typeWeight === 'number') {
complexity += typeWeight;
} else {
throw new Error(
`ERROR: QueryParser Failed to obtain type weight for ${parentName}.${node.name.value}`
);
}
} else {
throw new Error(
`ERROR: QueryParser Failed to obtain type name for ${parentName}.${node.name.value}`
);
}
}
return complexity;
} catch (err) {
throw new Error(
`ERROR: QueryParser.fieldNode Uncaught error handling ${parentName}.${
node.name.value
}\n
${err instanceof Error && err.stack}`
);
}
}
/**
* Return true if:
* 1. there is no directive
* 2. there is a directive named inlcude and the value is true
* 3. there is a directive named skip and the value is false
*/
// THIS IS NOT CALLED ANYWEHERE. IN PROGRESS
private directiveCheck(directive: DirectiveNode): boolean {
if (directive?.arguments) {
// get the first argument
const argument = directive.arguments[0];
// ensure the argument name is 'if'
const argumentHasVariables =
argument.value.kind === Kind.VARIABLE && argument.name.value === 'if';
// access the value of the argument depending on whether it is passed as a variable or not
let directiveArgumentValue;
if (argument.value.kind === Kind.BOOLEAN) {
directiveArgumentValue = Boolean(argument.value.value);
} else if (argumentHasVariables) {
directiveArgumentValue = Boolean(this.variables[argument.value.name.value]);
}
return (
(directive.name.value === 'include' && directiveArgumentValue === true) ||
(directive.name.value === 'skip' && directiveArgumentValue === false)
);
}
return true;
}
private selectionNode(node: SelectionNode, parentName: string): number {
let complexity = 0;
// TODO: complete implementation of directives include and skip
/**
* process this node only if:
* 1. there is no directive
* 2. there is a directive named inlcude and the value is true
* 3. there is a directive named skip and the value is false
*/
// const directive = node.directives;
// if (directive && this.directiveCheck(directive[0])) {
this.depth += 1;
if (this.depth > this.maxDepth) this.maxDepth = this.depth;
// the kind of a field node will either be field, fragment spread or inline fragment
if (node.kind === Kind.FIELD) {
complexity += this.fieldNode(node, parentName.toLowerCase());
} else if (node.kind === Kind.FRAGMENT_SPREAD) {
// add complexity and depth from fragment cache
const { complexity: fragComplexity, depth: fragDepth } =
this.fragmentCache[node.name.value];
complexity += fragComplexity;
this.depth += fragDepth;
if (this.depth > this.maxDepth) this.maxDepth = this.depth;
this.depth -= fragDepth;
// This is a leaf
// need to parse fragment definition at root and get the result here
} else if (node.kind === Kind.INLINE_FRAGMENT) {
const { typeCondition } = node;
// named type is the type from which inner fields should be take
// If the TypeCondition is omitted, an inline fragment is considered to be of the same type as the enclosing context
const namedType = typeCondition ? typeCondition.name.value.toLowerCase() : parentName;
// TODO: Handle directives like @include and @skip
// subtract 1 before, and add one after, entering the fragment selection to negate the additional level of depth added
this.depth -= 1;
complexity += this.selectionSetNode(node.selectionSet, namedType);
this.depth += 1;
} else {
throw new Error(`ERROR: QueryParser.selectionNode: node type not supported`);
}
this.depth -= 1;
//* }
return complexity;
}
private selectionSetNode(node: SelectionSetNode, parentName: string): number {
let complexity = 0;
let maxFragmentComplexity = 0;
for (let i = 0; i < node.selections.length; i += 1) {
// pass the current parent through because selection sets act only as intermediaries
const selectionNode = node.selections[i];
const selectionCost = this.selectionNode(selectionNode, parentName);
// we need to get the largest possible complexity so we save the largest inline fragment
// e.g. ...UnionType and ...PartofTheUnion
// this case these complexities should be summed in order to be accurate
// However an estimation suffice
// FIXME: Consider the case where 2 typed fragments are applicable
if (selectionNode.kind === Kind.INLINE_FRAGMENT) {
if (!selectionNode.typeCondition) {
// complexity is always applicable
complexity += selectionCost;
} else if (selectionCost > maxFragmentComplexity)
maxFragmentComplexity = selectionCost;
} else {
complexity += selectionCost;
}
}
return complexity + maxFragmentComplexity;
}
private definitionNode(node: DefinitionNode): number {
let complexity = 0;
// Operation definition is either query, mutation or subscripiton
if (node.kind === Kind.OPERATION_DEFINITION) {
if (node.operation.toLocaleLowerCase() in this.typeWeights) {
complexity += this.typeWeights[node.operation].weight;
if (node.selectionSet) {
complexity += this.selectionSetNode(node.selectionSet, node.operation);
}
}
} else if (node.kind === Kind.FRAGMENT_DEFINITION) {
// Fragments can only be defined on the root type.
// Parse the complexity of this fragment once and store it for use when analyzing other nodes
const namedType = node.typeCondition.name.value;
// Duplicate fragment names are not allowed by the GraphQL spec and an error is thrown if used.
const fragmentName = node.name.value;
const fragmentComplexity = this.selectionSetNode(
node.selectionSet,
namedType.toLowerCase()
);
// Don't count fragment complexity in the node's complexity. Only when fragment is used.
this.fragmentCache[fragmentName] = {
complexity: fragmentComplexity,
depth: this.maxDepth - 1, // subtract one from the calculated depth of the fragment to correct for the additional depth the fragment adds to the query when used
};
}
// TODO: Verify that there are no other type definition nodes that need to be handled (see ast.d.ts in 'graphql')
// else {
//
// // Other types include TypeSystemDefinitionNode (Schema, Type, Directvie) and
// // TypeSystemExtensionNode(Schema, Type);
// throw new Error(`ERROR: QueryParser.definitionNode: ${node.kind} type not supported`);
// }
return complexity;
}
private documentNode(node: DocumentNode): number {
let complexity = 0;
// Sort the definitions array by kind so that fragments are always parsed first.
// Fragments must be parsed first so that their complexity is available to other nodes.
const sortedDefinitions = [...node.definitions].sort((a, b) =>
a.kind.localeCompare(b.kind)
);
for (let i = 0; i < sortedDefinitions.length; i += 1) {
complexity += this.definitionNode(sortedDefinitions[i]);
}
return complexity;
}
public processQuery(queryAST: DocumentNode): number {
return this.documentNode(queryAST);
}
}
export default QueryParser;