-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathComplexityVisitor.js
More file actions
239 lines (192 loc) · 6.21 KB
/
Copy pathComplexityVisitor.js
File metadata and controls
239 lines (192 loc) · 6.21 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
import { GraphQLList, GraphQLNonNull, GraphQLObjectType } from 'graphql';
import { print } from 'graphql/language/printer';
import * as IntrospectionTypes from 'graphql/type/introspection';
// -- code vendored from: https://github.com/graphql/graphql-js/blob/15.x.x/src/validation/rules/OverlappingFieldsCanBeMerged.js#L636-L657
function isSameArguments(arguments1, arguments2) {
if (!arguments1 || !arguments2) return false;
if (arguments1.length !== arguments2.length) {
return false;
}
return arguments1.every((argument1) => {
const argument2 = arguments2.find(
({ name }) => name.value === argument1.name.value,
);
if (!argument2) {
return false;
}
return print(argument1.value) === print(argument2.value);
});
}
// ---
function isSameInlineFragment(selection1, selection2) {
return (
selection1.kind === 'InlineFragment' &&
selection1.typeCondition.name.value === selection2.typeCondition.name.value
);
}
function isSameSelection(selection1, selection2) {
return (
selection1.kind === selection2.kind &&
selection1.name?.value === selection2.name?.value &&
(isSameInlineFragment(selection1, selection2) ||
isSameArguments(selection1.arguments, selection2.arguments))
);
}
function uniqSelections(selections) {
const results = [];
for (const selection of selections) {
const other = results.find((s) => isSameSelection(selection, s));
if (!other) {
// clone nodes with selections to avoid mutating the original AST below
results.push(selection.selectionSet ? { ...selection } : selection);
continue;
}
const { selectionSet } = other;
if (selectionSet) {
// merge nested selections they will be deduped later on
other.selectionSet = {
...selectionSet,
selections: [
...selectionSet.selections,
...selection.selectionSet.selections,
],
};
}
}
return results;
}
export default class ComplexityVisitor {
constructor(
context,
{
scalarCost = 1,
objectCost = 0,
listFactor = 10,
// Special list factor to make schema queries not have huge costs.
introspectionListFactor = 2,
},
) {
this.context = context;
this.scalarCost = scalarCost;
this.objectCost = objectCost;
this.listFactor = listFactor;
this.introspectionListFactor = introspectionListFactor;
this.costFactor = 1;
this.cost = 0;
this.Field = {
enter: this.enterField,
leave: this.leaveField,
};
this.FragmentDefinition = () => {
// don't visit any further we will include these at the spread location
return false;
};
this.SelectionSet = this.flattenFragmentSpreads;
}
flattenFragmentSpreads(selectionSet, visitedFragments) {
// Ensure that visitedFragments is a Set or initialize a new Set if not provided
visitedFragments = visitedFragments instanceof Set ? visitedFragments : new Set();
var nextSelections = selectionSet.selections.flatMap((node) => {
if (node.kind === 'FragmentSpread') {
var fragmentName = node.name.value;
if (visitedFragments.has(fragmentName)) {
return [];
}
var fragment = this.context.getFragment(fragmentName);
if (!fragment) return [];
var fragment = this.context.getFragment(node.name.value);
// Add the fragment to the set before the recursive call
visitedFragments.add(fragmentName);
return this.flattenFragmentSpreads(fragment.selectionSet, visitedFragments).selections;
}
return node;
});
return {
...selectionSet,
selections: uniqSelections(nextSelections),
};
}
enterField() {
this.costFactor *= this.getFieldCostFactor();
this.cost += this.costFactor * this.getFieldCost();
}
leaveField() {
this.costFactor /= this.getFieldCostFactor();
}
getFieldCostFactor() {
const fieldDef = this.context.getFieldDef();
if (fieldDef?.extensions?.getCostFactor) {
return fieldDef.extensions.getCostFactor();
}
const directiveCostFactor = this.getDirectiveValue('costFactor');
if (directiveCostFactor != null) {
return directiveCostFactor;
}
return this.getTypeCostFactor(this.context.getType());
}
getTypeCostFactor(type) {
if (type instanceof GraphQLNonNull) {
return this.getTypeCostFactor(type.ofType);
}
if (type instanceof GraphQLList) {
const typeListFactor = this.isIntrospectionList(type)
? this.introspectionListFactor
: this.listFactor;
return typeListFactor * this.getTypeCostFactor(type.ofType);
}
return 1;
}
isIntrospectionList({ ofType }) {
let type = ofType;
if (type instanceof GraphQLNonNull) {
type = type.ofType;
}
return IntrospectionTypes[type.name] === type;
}
getFieldCost() {
const fieldDef = this.context.getFieldDef();
if (fieldDef?.extensions?.getCost) {
return fieldDef.extensions.getCost();
}
const directiveCost = this.getDirectiveValue('cost');
if (directiveCost != null) {
return directiveCost;
}
return this.getTypeCost(this.context.getType());
}
getTypeCost(type) {
if (type instanceof GraphQLNonNull || type instanceof GraphQLList) {
return this.getTypeCost(type.ofType);
}
return type instanceof GraphQLObjectType
? this.objectCost
: this.scalarCost;
}
getDirectiveValue(directiveName) {
const fieldDef = this.context.getFieldDef();
if (!fieldDef?.astNode?.directives) {
return null;
}
const directive = fieldDef.astNode.directives.find(
({ name }) => name.value === directiveName,
);
if (!directive) {
return null;
}
const valueArgument = directive.arguments.find(
(argument) => argument.name.value === 'value',
);
if (!valueArgument) {
const fieldName = fieldDef.name;
const parentTypeName = this.context.getParentType().name;
throw new Error(
`No \`value\` argument defined in \`@${directiveName}\` directive ` +
`on \`${fieldName}\` field on \`${parentTypeName}\`.`,
);
}
return parseFloat(valueArgument.value.value);
}
getCost() {
return this.cost;
}
}