-
Notifications
You must be signed in to change notification settings - Fork 26
/
costAnalysis.js
460 lines (413 loc) · 12.8 KB
/
costAnalysis.js
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// @flow
import assert from 'assert'
import selectn from 'selectn'
import { getArgumentValues } from 'graphql/execution/values'
import {
GraphQLObjectType,
GraphQLInterfaceType,
Kind,
getNamedType,
GraphQLError
} from 'graphql'
import type {
ValidationContext,
FragmentDefinitionNode,
OperationDefinitionNode,
FieldNode,
InlineFragmentNode,
DirectiveNode,
GraphQLNamedType,
ValueNode,
ArgumentNode,
SelectionNode
} from 'graphql'
export type CostAnalysisOptions = {
maximumCost: number,
variables?: Object,
onComplete?: (cost: number) => void,
createError?: (maximumCost: number, cost: number) => GraphQLError,
defaultCost?: number,
costMap?: Object,
complexityRange?: { min: number, max: number }
}
type NodeType =
| FieldNode
| OperationDefinitionNode
| FragmentDefinitionNode
| InlineFragmentNode
type NodeCostConfiguration = {
multiplier?: ?number,
useMultipliers?: boolean,
complexity?: number,
multipliers?: Array<number>
}
function costAnalysisMessage (max, actual) {
return (
`The query exceeds the maximum cost of ${max}. ` +
`Actual cost is ${actual}`
)
}
export default class CostAnalysis {
context: ValidationContext
cost: number
options: CostAnalysisOptions
fragments: { [name: string]: FragmentDefinitionNode }
OperationDefinition: Object
operationMultipliers: Array<number>
defaultCost: number
defaultComplexity: number
constructor (context: ValidationContext, options: CostAnalysisOptions) {
assert(
typeof options.maximumCost === 'number' && options.maximumCost > 0,
'Maximum query cost must be a positive number'
)
if (options.complexityRange) {
assert(
options.complexityRange.min &&
options.complexityRange.max &&
options.complexityRange.min < options.complexityRange.max,
'Invalid minimum and maximum complexity'
)
}
this.context = context
this.cost = 0
this.options = options
this.operationMultipliers = []
this.defaultCost = this.options.defaultCost || 0
this.defaultComplexity =
(this.options.complexityRange && this.options.complexityRange.min) || 1
this.OperationDefinition = {
enter: this.onOperationDefinitionEnter,
leave: this.onOperationDefinitionLeave
}
}
onOperationDefinitionEnter (operation: OperationDefinitionNode) {
switch (operation.operation) {
case 'query':
this.cost += this.computeNodeCost(
operation,
this.context.getSchema().getQueryType()
)
break
case 'mutation':
this.cost += this.computeNodeCost(
operation,
this.context.getSchema().getMutationType()
)
break
case 'subscription':
this.cost += this.computeNodeCost(
operation,
this.context.getSchema().getSubscriptionType()
)
break
default:
throw new Error(
`Query Cost could not be calculated for operation of type ${
operation.operation
}`
)
}
}
onOperationDefinitionLeave (): ?GraphQLError {
if (this.options.onComplete) {
this.options.onComplete(this.cost)
}
if (this.cost > this.options.maximumCost) {
return this.context.reportError(this.createError())
}
}
computeCost ({
multiplier,
useMultipliers = true,
complexity = this.defaultComplexity,
multipliers = []
}: NodeCostConfiguration = {}): number {
if (typeof arguments[0] !== 'object') {
return this.defaultCost
}
// multiplier is deprecated
if (multiplier) {
multipliers = multipliers.length ? multipliers : [multiplier]
process.env.NODE_ENV !== 'production' &&
console.warn(
`The multiplier property is DEPRECATED and will be removed in the next release. \n` +
`Please use the multipliers field instead.`
)
}
if (
this.options.complexityRange &&
(complexity > this.options.complexityRange.max ||
complexity < this.options.complexityRange.min)
) {
this.context.reportError(
new GraphQLError(
`The complexity argument must be between ` +
`${this.options.complexityRange.min} and ${
this.options.complexityRange.max
}`
)
)
return this.defaultCost
}
if (useMultipliers) {
if (multipliers.length) {
const multiplier = multipliers.reduce(
(total, current) => total + current,
0
)
this.operationMultipliers = [...this.operationMultipliers, multiplier]
}
return this.operationMultipliers.reduce(
(acc, multiplier) => acc * multiplier,
complexity
)
}
return complexity
}
getArgsFromCostMap (
node: FieldNode,
parentType: string,
fieldArgs: { [argument: string]: mixed }
) {
const costObject =
this.options.costMap &&
this.options.costMap[parentType] &&
this.options.costMap[parentType][node.name.value]
if (!costObject) {
return
}
let { useMultipliers, multiplier, complexity, multipliers } = costObject
multiplier = multiplier && selectn(multiplier, fieldArgs)
multipliers = this.getMultipliersFromString(multipliers, fieldArgs)
return {
useMultipliers,
multiplier,
complexity,
multipliers
}
}
getMultipliersFromListNode (
listNode: $ReadOnlyArray<ValueNode>,
fieldArgs: { [argument: string]: mixed }
) {
const multipliers = []
listNode.forEach(node => {
if (node.kind === Kind.STRING) {
multipliers.push(node.value)
}
})
return this.getMultipliersFromString(multipliers, fieldArgs)
}
getMultipliersFromString (
multipliers: Array<string> = [],
fieldArgs: { [argument: string]: mixed }
): Array<number> {
// get arguments values, convert to integer and delete 0 values from list
return multipliers
.map(multiplier => {
const value = selectn(multiplier, fieldArgs)
// if the argument is an array, the multiplier will be the length of it
if (Array.isArray(value)) {
return value.length
}
return Number(value) || 0
})
.filter(multiplier => multiplier !== 0)
}
getArgsFromDirectives (
directives: $ReadOnlyArray<DirectiveNode>,
fieldArgs: { [argument: string]: mixed }
) {
const costDirective = directives.find(
directive => directive.name.value === 'cost'
)
if (costDirective && costDirective.arguments) {
// get cost arguments
const complexityArg = costDirective.arguments.find(
arg => arg.name.value === 'complexity'
)
const useMultipliersArg =
costDirective.arguments &&
costDirective.arguments.find(arg => arg.name.value === 'useMultipliers')
const multiplierArg =
costDirective.arguments &&
costDirective.arguments.find(arg => arg.name.value === 'multiplier')
const multipliersArg: ?ArgumentNode =
costDirective.arguments &&
costDirective.arguments.find(arg => arg.name.value === 'multipliers')
// get arguments's values
const useMultipliers =
useMultipliersArg &&
useMultipliersArg.value &&
useMultipliersArg.value.kind === Kind.BOOLEAN
? useMultipliersArg.value.value
: true
const multipliers: Array<number> =
multipliersArg &&
multipliersArg.value &&
multipliersArg.value.kind === Kind.LIST
? this.getMultipliersFromListNode(
multipliersArg.value.values,
fieldArgs
)
: []
const multiplier: ?number =
multiplierArg && multiplierArg.value.value
? Number(selectn(multiplierArg.value.value, fieldArgs))
: undefined
const complexity =
complexityArg &&
complexityArg.value &&
complexityArg.value.kind === Kind.INT
? Number(complexityArg.value.value)
: this.defaultComplexity
return {
complexity,
multiplier,
multipliers,
useMultipliers
}
}
}
computeNodeCost (
node: NodeType,
typeDef: ?GraphQLNamedType,
parentMultipliers: Array<number> = []
): number {
if (!node.selectionSet) {
return 0
}
let fields = {}
if (
typeDef instanceof GraphQLObjectType ||
typeDef instanceof GraphQLInterfaceType
) {
fields = typeDef.getFields()
}
const selections: $ReadOnlyArray<SelectionNode> = node.selectionSet.selections
let total = 0
let fragmentCosts = []
for (const childNode of selections) {
// reset the operation multipliers with parentMultipliers for each childNode
// it resolves issue #14: https://github.com/pa-bru/graphql-cost-analysis/issues/14
this.operationMultipliers = [...parentMultipliers]
let nodeCost: number = this.defaultCost
switch (childNode.kind) {
case Kind.FIELD: {
const field: Object = fields[childNode.name.value]
// Invalid field, should be caught by other validation rules
if (!field) {
break
}
const fieldType = getNamedType(field.type)
// get field's arguments
let fieldArgs = {}
try {
fieldArgs = getArgumentValues(
field,
childNode,
this.options.variables || {}
)
} catch (e) {
this.context.reportError(e)
}
// it the costMap option is set, compute the cost with the costMap provided
// by the user.
if (
this.options.costMap &&
typeof this.options.costMap === 'object'
) {
const costMapArgs =
typeDef && typeDef.name
? this.getArgsFromCostMap(childNode, typeDef.name, fieldArgs)
: undefined
nodeCost = this.computeCost(costMapArgs)
} else {
// Compute cost of current field with its cost directive
let costIsComputed: boolean = false
if (field.astNode && field.astNode.directives) {
const directiveArgs = this.getArgsFromDirectives(
field.astNode.directives,
fieldArgs
)
nodeCost = this.computeCost(directiveArgs)
if (directiveArgs) {
costIsComputed = true
}
}
// if the cost directive is defined on the Type
// and the nodeCost has not already been computed
if (
fieldType &&
fieldType.astNode &&
fieldType.astNode.directives &&
fieldType instanceof GraphQLObjectType &&
costIsComputed === false
) {
const directiveArgs = this.getArgsFromDirectives(
fieldType.astNode.directives,
fieldArgs
)
nodeCost = this.computeCost(directiveArgs)
}
}
let childCost = 0
childCost = this.computeNodeCost(
childNode,
fieldType,
this.operationMultipliers
)
nodeCost += childCost
break
}
case Kind.FRAGMENT_SPREAD: {
const fragment = this.context.getFragment(childNode.name.value)
const fragmentType =
fragment &&
this.context
.getSchema()
.getType(fragment.typeCondition.name.value)
let fragmentNodeCost = fragment
? this.computeNodeCost(fragment, fragmentType, this.operationMultipliers)
: this.defaultCost
fragmentCosts.push(fragmentNodeCost)
nodeCost = 0
break
}
case Kind.INLINE_FRAGMENT: {
let inlineFragmentType = typeDef
if (childNode.typeCondition && childNode.typeCondition.name) {
inlineFragmentType = this.context
.getSchema()
// $FlowFixMe: don't know why Flow thinks it could be undefined
.getType(childNode.typeCondition.name.value)
}
let fragmentNodeCost = childNode
? this.computeNodeCost(childNode, inlineFragmentType, this.operationMultipliers)
: this.defaultCost
fragmentCosts.push(fragmentNodeCost)
nodeCost = 0
break
}
default: {
nodeCost = this.computeNodeCost(childNode, typeDef)
break
}
}
total = Math.max(nodeCost, 0) + total
}
if (!fragmentCosts.length) {
return total
}
return total + Math.max(...fragmentCosts)
}
createError (): GraphQLError {
if (typeof this.options.createError === 'function') {
return this.options.createError(this.options.maximumCost, this.cost)
}
return new GraphQLError(
costAnalysisMessage(this.options.maximumCost, this.cost)
)
}
}