@conduit-client/onestore-graphql-parser 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main/index.js +6 -0
- package/dist/main/index.js.map +1 -0
- package/dist/types/index.d.ts +0 -0
- package/dist/types/v1/__tests__/gql.spec.d.ts +1 -0
- package/dist/types/v1/__tests__/utils.d.ts +4 -0
- package/dist/types/v1/gql.d.ts +47 -0
- package/dist/types/v1/index.d.ts +6 -0
- package/dist/v1/index.js +3176 -0
- package/dist/v1/index.js.map +1 -0
- package/package.json +52 -0
package/dist/v1/index.js
ADDED
|
@@ -0,0 +1,3176 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
* For full license text, see the LICENSE.txt file
|
|
5
|
+
*/
|
|
6
|
+
function devAssert(condition, message) {
|
|
7
|
+
const booleanCondition = Boolean(condition);
|
|
8
|
+
if (!booleanCondition) {
|
|
9
|
+
throw new Error(message);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
const MAX_ARRAY_LENGTH = 10;
|
|
13
|
+
const MAX_RECURSIVE_DEPTH = 2;
|
|
14
|
+
function inspect(value) {
|
|
15
|
+
return formatValue(value, []);
|
|
16
|
+
}
|
|
17
|
+
function formatValue(value, seenValues) {
|
|
18
|
+
switch (typeof value) {
|
|
19
|
+
case "string":
|
|
20
|
+
return JSON.stringify(value);
|
|
21
|
+
case "function":
|
|
22
|
+
return value.name ? `[function ${value.name}]` : "[function]";
|
|
23
|
+
case "object":
|
|
24
|
+
return formatObjectValue(value, seenValues);
|
|
25
|
+
default:
|
|
26
|
+
return String(value);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function formatObjectValue(value, previouslySeenValues) {
|
|
30
|
+
if (value === null) {
|
|
31
|
+
return "null";
|
|
32
|
+
}
|
|
33
|
+
if (previouslySeenValues.includes(value)) {
|
|
34
|
+
return "[Circular]";
|
|
35
|
+
}
|
|
36
|
+
const seenValues = [...previouslySeenValues, value];
|
|
37
|
+
if (isJSONable(value)) {
|
|
38
|
+
const jsonValue = value.toJSON();
|
|
39
|
+
if (jsonValue !== value) {
|
|
40
|
+
return typeof jsonValue === "string" ? jsonValue : formatValue(jsonValue, seenValues);
|
|
41
|
+
}
|
|
42
|
+
} else if (Array.isArray(value)) {
|
|
43
|
+
return formatArray(value, seenValues);
|
|
44
|
+
}
|
|
45
|
+
return formatObject(value, seenValues);
|
|
46
|
+
}
|
|
47
|
+
function isJSONable(value) {
|
|
48
|
+
return typeof value.toJSON === "function";
|
|
49
|
+
}
|
|
50
|
+
function formatObject(object, seenValues) {
|
|
51
|
+
const entries = Object.entries(object);
|
|
52
|
+
if (entries.length === 0) {
|
|
53
|
+
return "{}";
|
|
54
|
+
}
|
|
55
|
+
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
|
|
56
|
+
return "[" + getObjectTag(object) + "]";
|
|
57
|
+
}
|
|
58
|
+
const properties = entries.map(
|
|
59
|
+
([key, value]) => key + ": " + formatValue(value, seenValues)
|
|
60
|
+
);
|
|
61
|
+
return "{ " + properties.join(", ") + " }";
|
|
62
|
+
}
|
|
63
|
+
function formatArray(array, seenValues) {
|
|
64
|
+
if (array.length === 0) {
|
|
65
|
+
return "[]";
|
|
66
|
+
}
|
|
67
|
+
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
|
|
68
|
+
return "[Array]";
|
|
69
|
+
}
|
|
70
|
+
const len = Math.min(MAX_ARRAY_LENGTH, array.length);
|
|
71
|
+
const remaining = array.length - len;
|
|
72
|
+
const items = [];
|
|
73
|
+
for (let i = 0; i < len; ++i) {
|
|
74
|
+
items.push(formatValue(array[i], seenValues));
|
|
75
|
+
}
|
|
76
|
+
if (remaining === 1) {
|
|
77
|
+
items.push("... 1 more item");
|
|
78
|
+
} else if (remaining > 1) {
|
|
79
|
+
items.push(`... ${remaining} more items`);
|
|
80
|
+
}
|
|
81
|
+
return "[" + items.join(", ") + "]";
|
|
82
|
+
}
|
|
83
|
+
function getObjectTag(object) {
|
|
84
|
+
const tag = Object.prototype.toString.call(object).replace(/^\[object /, "").replace(/]$/, "");
|
|
85
|
+
if (tag === "Object" && typeof object.constructor === "function") {
|
|
86
|
+
const name = object.constructor.name;
|
|
87
|
+
if (typeof name === "string" && name !== "") {
|
|
88
|
+
return name;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return tag;
|
|
92
|
+
}
|
|
93
|
+
const isProduction = globalThis.process && // eslint-disable-next-line no-undef
|
|
94
|
+
process.env.NODE_ENV === "production";
|
|
95
|
+
const instanceOf = (
|
|
96
|
+
/* c8 ignore next 6 */
|
|
97
|
+
// FIXME: https://github.com/graphql/graphql-js/issues/2317
|
|
98
|
+
isProduction ? function instanceOf2(value, constructor) {
|
|
99
|
+
return value instanceof constructor;
|
|
100
|
+
} : function instanceOf3(value, constructor) {
|
|
101
|
+
if (value instanceof constructor) {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
if (typeof value === "object" && value !== null) {
|
|
105
|
+
var _value$constructor;
|
|
106
|
+
const className = constructor.prototype[Symbol.toStringTag];
|
|
107
|
+
const valueClassName = (
|
|
108
|
+
// We still need to support constructor's name to detect conflicts with older versions of this library.
|
|
109
|
+
Symbol.toStringTag in value ? value[Symbol.toStringTag] : (_value$constructor = value.constructor) === null || _value$constructor === void 0 ? void 0 : _value$constructor.name
|
|
110
|
+
);
|
|
111
|
+
if (className === valueClassName) {
|
|
112
|
+
const stringifiedValue = inspect(value);
|
|
113
|
+
throw new Error(`Cannot use ${className} "${stringifiedValue}" from another module or realm.
|
|
114
|
+
|
|
115
|
+
Ensure that there is only one instance of "graphql" in the node_modules
|
|
116
|
+
directory. If different versions of "graphql" are the dependencies of other
|
|
117
|
+
relied on modules, use "resolutions" to ensure only one version is installed.
|
|
118
|
+
|
|
119
|
+
https://yarnpkg.com/en/docs/selective-version-resolutions
|
|
120
|
+
|
|
121
|
+
Duplicate "graphql" modules cannot be used at the same time since different
|
|
122
|
+
versions may have different capabilities and behavior. The data from one
|
|
123
|
+
version used in the function from another could produce confusing and
|
|
124
|
+
spurious results.`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
);
|
|
130
|
+
class Source {
|
|
131
|
+
constructor(body, name = "GraphQL request", locationOffset = {
|
|
132
|
+
line: 1,
|
|
133
|
+
column: 1
|
|
134
|
+
}) {
|
|
135
|
+
typeof body === "string" || devAssert(false, `Body must be a string. Received: ${inspect(body)}.`);
|
|
136
|
+
this.body = body;
|
|
137
|
+
this.name = name;
|
|
138
|
+
this.locationOffset = locationOffset;
|
|
139
|
+
this.locationOffset.line > 0 || devAssert(
|
|
140
|
+
false,
|
|
141
|
+
"line in locationOffset is 1-indexed and must be positive."
|
|
142
|
+
);
|
|
143
|
+
this.locationOffset.column > 0 || devAssert(
|
|
144
|
+
false,
|
|
145
|
+
"column in locationOffset is 1-indexed and must be positive."
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
get [Symbol.toStringTag]() {
|
|
149
|
+
return "Source";
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function isSource(source) {
|
|
153
|
+
return instanceOf(source, Source);
|
|
154
|
+
}
|
|
155
|
+
function invariant(condition, message) {
|
|
156
|
+
const booleanCondition = Boolean(condition);
|
|
157
|
+
if (!booleanCondition) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
"Unexpected invariant triggered."
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const LineRegExp = /\r\n|[\n\r]/g;
|
|
164
|
+
function getLocation(source, position) {
|
|
165
|
+
let lastLineStart = 0;
|
|
166
|
+
let line = 1;
|
|
167
|
+
for (const match of source.body.matchAll(LineRegExp)) {
|
|
168
|
+
typeof match.index === "number" || invariant(false);
|
|
169
|
+
if (match.index >= position) {
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
lastLineStart = match.index + match[0].length;
|
|
173
|
+
line += 1;
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
line,
|
|
177
|
+
column: position + 1 - lastLineStart
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
function printLocation(location) {
|
|
181
|
+
return printSourceLocation(
|
|
182
|
+
location.source,
|
|
183
|
+
getLocation(location.source, location.start)
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
function printSourceLocation(source, sourceLocation) {
|
|
187
|
+
const firstLineColumnOffset = source.locationOffset.column - 1;
|
|
188
|
+
const body = "".padStart(firstLineColumnOffset) + source.body;
|
|
189
|
+
const lineIndex = sourceLocation.line - 1;
|
|
190
|
+
const lineOffset = source.locationOffset.line - 1;
|
|
191
|
+
const lineNum = sourceLocation.line + lineOffset;
|
|
192
|
+
const columnOffset = sourceLocation.line === 1 ? firstLineColumnOffset : 0;
|
|
193
|
+
const columnNum = sourceLocation.column + columnOffset;
|
|
194
|
+
const locationStr = `${source.name}:${lineNum}:${columnNum}
|
|
195
|
+
`;
|
|
196
|
+
const lines = body.split(/\r\n|[\n\r]/g);
|
|
197
|
+
const locationLine = lines[lineIndex];
|
|
198
|
+
if (locationLine.length > 120) {
|
|
199
|
+
const subLineIndex = Math.floor(columnNum / 80);
|
|
200
|
+
const subLineColumnNum = columnNum % 80;
|
|
201
|
+
const subLines = [];
|
|
202
|
+
for (let i = 0; i < locationLine.length; i += 80) {
|
|
203
|
+
subLines.push(locationLine.slice(i, i + 80));
|
|
204
|
+
}
|
|
205
|
+
return locationStr + printPrefixedLines([
|
|
206
|
+
[`${lineNum} |`, subLines[0]],
|
|
207
|
+
...subLines.slice(1, subLineIndex + 1).map((subLine) => ["|", subLine]),
|
|
208
|
+
["|", "^".padStart(subLineColumnNum)],
|
|
209
|
+
["|", subLines[subLineIndex + 1]]
|
|
210
|
+
]);
|
|
211
|
+
}
|
|
212
|
+
return locationStr + printPrefixedLines([
|
|
213
|
+
// Lines specified like this: ["prefix", "string"],
|
|
214
|
+
[`${lineNum - 1} |`, lines[lineIndex - 1]],
|
|
215
|
+
[`${lineNum} |`, locationLine],
|
|
216
|
+
["|", "^".padStart(columnNum)],
|
|
217
|
+
[`${lineNum + 1} |`, lines[lineIndex + 1]]
|
|
218
|
+
]);
|
|
219
|
+
}
|
|
220
|
+
function printPrefixedLines(lines) {
|
|
221
|
+
const existingLines = lines.filter(([_, line]) => line !== void 0);
|
|
222
|
+
const padLen = Math.max(...existingLines.map(([prefix]) => prefix.length));
|
|
223
|
+
return existingLines.map(([prefix, line]) => prefix.padStart(padLen) + (line ? " " + line : "")).join("\n");
|
|
224
|
+
}
|
|
225
|
+
var Kind;
|
|
226
|
+
(function(Kind2) {
|
|
227
|
+
Kind2["NAME"] = "Name";
|
|
228
|
+
Kind2["DOCUMENT"] = "Document";
|
|
229
|
+
Kind2["OPERATION_DEFINITION"] = "OperationDefinition";
|
|
230
|
+
Kind2["VARIABLE_DEFINITION"] = "VariableDefinition";
|
|
231
|
+
Kind2["SELECTION_SET"] = "SelectionSet";
|
|
232
|
+
Kind2["FIELD"] = "Field";
|
|
233
|
+
Kind2["ARGUMENT"] = "Argument";
|
|
234
|
+
Kind2["FRAGMENT_SPREAD"] = "FragmentSpread";
|
|
235
|
+
Kind2["INLINE_FRAGMENT"] = "InlineFragment";
|
|
236
|
+
Kind2["FRAGMENT_DEFINITION"] = "FragmentDefinition";
|
|
237
|
+
Kind2["VARIABLE"] = "Variable";
|
|
238
|
+
Kind2["INT"] = "IntValue";
|
|
239
|
+
Kind2["FLOAT"] = "FloatValue";
|
|
240
|
+
Kind2["STRING"] = "StringValue";
|
|
241
|
+
Kind2["BOOLEAN"] = "BooleanValue";
|
|
242
|
+
Kind2["NULL"] = "NullValue";
|
|
243
|
+
Kind2["ENUM"] = "EnumValue";
|
|
244
|
+
Kind2["LIST"] = "ListValue";
|
|
245
|
+
Kind2["OBJECT"] = "ObjectValue";
|
|
246
|
+
Kind2["OBJECT_FIELD"] = "ObjectField";
|
|
247
|
+
Kind2["DIRECTIVE"] = "Directive";
|
|
248
|
+
Kind2["NAMED_TYPE"] = "NamedType";
|
|
249
|
+
Kind2["LIST_TYPE"] = "ListType";
|
|
250
|
+
Kind2["NON_NULL_TYPE"] = "NonNullType";
|
|
251
|
+
Kind2["SCHEMA_DEFINITION"] = "SchemaDefinition";
|
|
252
|
+
Kind2["OPERATION_TYPE_DEFINITION"] = "OperationTypeDefinition";
|
|
253
|
+
Kind2["SCALAR_TYPE_DEFINITION"] = "ScalarTypeDefinition";
|
|
254
|
+
Kind2["OBJECT_TYPE_DEFINITION"] = "ObjectTypeDefinition";
|
|
255
|
+
Kind2["FIELD_DEFINITION"] = "FieldDefinition";
|
|
256
|
+
Kind2["INPUT_VALUE_DEFINITION"] = "InputValueDefinition";
|
|
257
|
+
Kind2["INTERFACE_TYPE_DEFINITION"] = "InterfaceTypeDefinition";
|
|
258
|
+
Kind2["UNION_TYPE_DEFINITION"] = "UnionTypeDefinition";
|
|
259
|
+
Kind2["ENUM_TYPE_DEFINITION"] = "EnumTypeDefinition";
|
|
260
|
+
Kind2["ENUM_VALUE_DEFINITION"] = "EnumValueDefinition";
|
|
261
|
+
Kind2["INPUT_OBJECT_TYPE_DEFINITION"] = "InputObjectTypeDefinition";
|
|
262
|
+
Kind2["DIRECTIVE_DEFINITION"] = "DirectiveDefinition";
|
|
263
|
+
Kind2["SCHEMA_EXTENSION"] = "SchemaExtension";
|
|
264
|
+
Kind2["SCALAR_TYPE_EXTENSION"] = "ScalarTypeExtension";
|
|
265
|
+
Kind2["OBJECT_TYPE_EXTENSION"] = "ObjectTypeExtension";
|
|
266
|
+
Kind2["INTERFACE_TYPE_EXTENSION"] = "InterfaceTypeExtension";
|
|
267
|
+
Kind2["UNION_TYPE_EXTENSION"] = "UnionTypeExtension";
|
|
268
|
+
Kind2["ENUM_TYPE_EXTENSION"] = "EnumTypeExtension";
|
|
269
|
+
Kind2["INPUT_OBJECT_TYPE_EXTENSION"] = "InputObjectTypeExtension";
|
|
270
|
+
})(Kind || (Kind = {}));
|
|
271
|
+
var TokenKind;
|
|
272
|
+
(function(TokenKind2) {
|
|
273
|
+
TokenKind2["SOF"] = "<SOF>";
|
|
274
|
+
TokenKind2["EOF"] = "<EOF>";
|
|
275
|
+
TokenKind2["BANG"] = "!";
|
|
276
|
+
TokenKind2["DOLLAR"] = "$";
|
|
277
|
+
TokenKind2["AMP"] = "&";
|
|
278
|
+
TokenKind2["PAREN_L"] = "(";
|
|
279
|
+
TokenKind2["PAREN_R"] = ")";
|
|
280
|
+
TokenKind2["SPREAD"] = "...";
|
|
281
|
+
TokenKind2["COLON"] = ":";
|
|
282
|
+
TokenKind2["EQUALS"] = "=";
|
|
283
|
+
TokenKind2["AT"] = "@";
|
|
284
|
+
TokenKind2["BRACKET_L"] = "[";
|
|
285
|
+
TokenKind2["BRACKET_R"] = "]";
|
|
286
|
+
TokenKind2["BRACE_L"] = "{";
|
|
287
|
+
TokenKind2["PIPE"] = "|";
|
|
288
|
+
TokenKind2["BRACE_R"] = "}";
|
|
289
|
+
TokenKind2["NAME"] = "Name";
|
|
290
|
+
TokenKind2["INT"] = "Int";
|
|
291
|
+
TokenKind2["FLOAT"] = "Float";
|
|
292
|
+
TokenKind2["STRING"] = "String";
|
|
293
|
+
TokenKind2["BLOCK_STRING"] = "BlockString";
|
|
294
|
+
TokenKind2["COMMENT"] = "Comment";
|
|
295
|
+
})(TokenKind || (TokenKind = {}));
|
|
296
|
+
function isObjectLike(value) {
|
|
297
|
+
return typeof value == "object" && value !== null;
|
|
298
|
+
}
|
|
299
|
+
function toNormalizedOptions(args) {
|
|
300
|
+
const firstArg = args[0];
|
|
301
|
+
if (firstArg == null || "kind" in firstArg || "length" in firstArg) {
|
|
302
|
+
return {
|
|
303
|
+
nodes: firstArg,
|
|
304
|
+
source: args[1],
|
|
305
|
+
positions: args[2],
|
|
306
|
+
path: args[3],
|
|
307
|
+
originalError: args[4],
|
|
308
|
+
extensions: args[5]
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
return firstArg;
|
|
312
|
+
}
|
|
313
|
+
class GraphQLError extends Error {
|
|
314
|
+
/**
|
|
315
|
+
* An array of `{ line, column }` locations within the source GraphQL document
|
|
316
|
+
* which correspond to this error.
|
|
317
|
+
*
|
|
318
|
+
* Errors during validation often contain multiple locations, for example to
|
|
319
|
+
* point out two things with the same name. Errors during execution include a
|
|
320
|
+
* single location, the field which produced the error.
|
|
321
|
+
*
|
|
322
|
+
* Enumerable, and appears in the result of JSON.stringify().
|
|
323
|
+
*/
|
|
324
|
+
/**
|
|
325
|
+
* An array describing the JSON-path into the execution response which
|
|
326
|
+
* corresponds to this error. Only included for errors during execution.
|
|
327
|
+
*
|
|
328
|
+
* Enumerable, and appears in the result of JSON.stringify().
|
|
329
|
+
*/
|
|
330
|
+
/**
|
|
331
|
+
* An array of GraphQL AST Nodes corresponding to this error.
|
|
332
|
+
*/
|
|
333
|
+
/**
|
|
334
|
+
* The source GraphQL document for the first location of this error.
|
|
335
|
+
*
|
|
336
|
+
* Note that if this Error represents more than one node, the source may not
|
|
337
|
+
* represent nodes after the first node.
|
|
338
|
+
*/
|
|
339
|
+
/**
|
|
340
|
+
* An array of character offsets within the source GraphQL document
|
|
341
|
+
* which correspond to this error.
|
|
342
|
+
*/
|
|
343
|
+
/**
|
|
344
|
+
* The original error thrown from a field resolver during execution.
|
|
345
|
+
*/
|
|
346
|
+
/**
|
|
347
|
+
* Extension fields to add to the formatted error.
|
|
348
|
+
*/
|
|
349
|
+
/**
|
|
350
|
+
* @deprecated Please use the `GraphQLErrorOptions` constructor overload instead.
|
|
351
|
+
*/
|
|
352
|
+
constructor(message, ...rawArgs) {
|
|
353
|
+
var _this$nodes, _nodeLocations$, _ref;
|
|
354
|
+
const { nodes, source, positions, path, originalError, extensions } = toNormalizedOptions(rawArgs);
|
|
355
|
+
super(message);
|
|
356
|
+
this.name = "GraphQLError";
|
|
357
|
+
this.path = path !== null && path !== void 0 ? path : void 0;
|
|
358
|
+
this.originalError = originalError !== null && originalError !== void 0 ? originalError : void 0;
|
|
359
|
+
this.nodes = undefinedIfEmpty(
|
|
360
|
+
Array.isArray(nodes) ? nodes : nodes ? [nodes] : void 0
|
|
361
|
+
);
|
|
362
|
+
const nodeLocations = undefinedIfEmpty(
|
|
363
|
+
(_this$nodes = this.nodes) === null || _this$nodes === void 0 ? void 0 : _this$nodes.map((node) => node.loc).filter((loc) => loc != null)
|
|
364
|
+
);
|
|
365
|
+
this.source = source !== null && source !== void 0 ? source : nodeLocations === null || nodeLocations === void 0 ? void 0 : (_nodeLocations$ = nodeLocations[0]) === null || _nodeLocations$ === void 0 ? void 0 : _nodeLocations$.source;
|
|
366
|
+
this.positions = positions !== null && positions !== void 0 ? positions : nodeLocations === null || nodeLocations === void 0 ? void 0 : nodeLocations.map((loc) => loc.start);
|
|
367
|
+
this.locations = positions && source ? positions.map((pos) => getLocation(source, pos)) : nodeLocations === null || nodeLocations === void 0 ? void 0 : nodeLocations.map((loc) => getLocation(loc.source, loc.start));
|
|
368
|
+
const originalExtensions = isObjectLike(
|
|
369
|
+
originalError === null || originalError === void 0 ? void 0 : originalError.extensions
|
|
370
|
+
) ? originalError === null || originalError === void 0 ? void 0 : originalError.extensions : void 0;
|
|
371
|
+
this.extensions = (_ref = extensions !== null && extensions !== void 0 ? extensions : originalExtensions) !== null && _ref !== void 0 ? _ref : /* @__PURE__ */ Object.create(null);
|
|
372
|
+
Object.defineProperties(this, {
|
|
373
|
+
message: {
|
|
374
|
+
writable: true,
|
|
375
|
+
enumerable: true
|
|
376
|
+
},
|
|
377
|
+
name: {
|
|
378
|
+
enumerable: false
|
|
379
|
+
},
|
|
380
|
+
nodes: {
|
|
381
|
+
enumerable: false
|
|
382
|
+
},
|
|
383
|
+
source: {
|
|
384
|
+
enumerable: false
|
|
385
|
+
},
|
|
386
|
+
positions: {
|
|
387
|
+
enumerable: false
|
|
388
|
+
},
|
|
389
|
+
originalError: {
|
|
390
|
+
enumerable: false
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
if (originalError !== null && originalError !== void 0 && originalError.stack) {
|
|
394
|
+
Object.defineProperty(this, "stack", {
|
|
395
|
+
value: originalError.stack,
|
|
396
|
+
writable: true,
|
|
397
|
+
configurable: true
|
|
398
|
+
});
|
|
399
|
+
} else if (Error.captureStackTrace) {
|
|
400
|
+
Error.captureStackTrace(this, GraphQLError);
|
|
401
|
+
} else {
|
|
402
|
+
Object.defineProperty(this, "stack", {
|
|
403
|
+
value: Error().stack,
|
|
404
|
+
writable: true,
|
|
405
|
+
configurable: true
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
get [Symbol.toStringTag]() {
|
|
410
|
+
return "GraphQLError";
|
|
411
|
+
}
|
|
412
|
+
toString() {
|
|
413
|
+
let output = this.message;
|
|
414
|
+
if (this.nodes) {
|
|
415
|
+
for (const node of this.nodes) {
|
|
416
|
+
if (node.loc) {
|
|
417
|
+
output += "\n\n" + printLocation(node.loc);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
} else if (this.source && this.locations) {
|
|
421
|
+
for (const location of this.locations) {
|
|
422
|
+
output += "\n\n" + printSourceLocation(this.source, location);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return output;
|
|
426
|
+
}
|
|
427
|
+
toJSON() {
|
|
428
|
+
const formattedError = {
|
|
429
|
+
message: this.message
|
|
430
|
+
};
|
|
431
|
+
if (this.locations != null) {
|
|
432
|
+
formattedError.locations = this.locations;
|
|
433
|
+
}
|
|
434
|
+
if (this.path != null) {
|
|
435
|
+
formattedError.path = this.path;
|
|
436
|
+
}
|
|
437
|
+
if (this.extensions != null && Object.keys(this.extensions).length > 0) {
|
|
438
|
+
formattedError.extensions = this.extensions;
|
|
439
|
+
}
|
|
440
|
+
return formattedError;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
function undefinedIfEmpty(array) {
|
|
444
|
+
return array === void 0 || array.length === 0 ? void 0 : array;
|
|
445
|
+
}
|
|
446
|
+
function syntaxError(source, position, description) {
|
|
447
|
+
return new GraphQLError(`Syntax Error: ${description}`, {
|
|
448
|
+
source,
|
|
449
|
+
positions: [position]
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
class Location {
|
|
453
|
+
/**
|
|
454
|
+
* The character offset at which this Node begins.
|
|
455
|
+
*/
|
|
456
|
+
/**
|
|
457
|
+
* The character offset at which this Node ends.
|
|
458
|
+
*/
|
|
459
|
+
/**
|
|
460
|
+
* The Token at which this Node begins.
|
|
461
|
+
*/
|
|
462
|
+
/**
|
|
463
|
+
* The Token at which this Node ends.
|
|
464
|
+
*/
|
|
465
|
+
/**
|
|
466
|
+
* The Source document the AST represents.
|
|
467
|
+
*/
|
|
468
|
+
constructor(startToken, endToken, source) {
|
|
469
|
+
this.start = startToken.start;
|
|
470
|
+
this.end = endToken.end;
|
|
471
|
+
this.startToken = startToken;
|
|
472
|
+
this.endToken = endToken;
|
|
473
|
+
this.source = source;
|
|
474
|
+
}
|
|
475
|
+
get [Symbol.toStringTag]() {
|
|
476
|
+
return "Location";
|
|
477
|
+
}
|
|
478
|
+
toJSON() {
|
|
479
|
+
return {
|
|
480
|
+
start: this.start,
|
|
481
|
+
end: this.end
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
class Token {
|
|
486
|
+
/**
|
|
487
|
+
* The kind of Token.
|
|
488
|
+
*/
|
|
489
|
+
/**
|
|
490
|
+
* The character offset at which this Node begins.
|
|
491
|
+
*/
|
|
492
|
+
/**
|
|
493
|
+
* The character offset at which this Node ends.
|
|
494
|
+
*/
|
|
495
|
+
/**
|
|
496
|
+
* The 1-indexed line number on which this Token appears.
|
|
497
|
+
*/
|
|
498
|
+
/**
|
|
499
|
+
* The 1-indexed column number at which this Token begins.
|
|
500
|
+
*/
|
|
501
|
+
/**
|
|
502
|
+
* For non-punctuation tokens, represents the interpreted value of the token.
|
|
503
|
+
*
|
|
504
|
+
* Note: is undefined for punctuation tokens, but typed as string for
|
|
505
|
+
* convenience in the parser.
|
|
506
|
+
*/
|
|
507
|
+
/**
|
|
508
|
+
* Tokens exist as nodes in a double-linked-list amongst all tokens
|
|
509
|
+
* including ignored tokens. <SOF> is always the first node and <EOF>
|
|
510
|
+
* the last.
|
|
511
|
+
*/
|
|
512
|
+
constructor(kind, start, end, line, column, value) {
|
|
513
|
+
this.kind = kind;
|
|
514
|
+
this.start = start;
|
|
515
|
+
this.end = end;
|
|
516
|
+
this.line = line;
|
|
517
|
+
this.column = column;
|
|
518
|
+
this.value = value;
|
|
519
|
+
this.prev = null;
|
|
520
|
+
this.next = null;
|
|
521
|
+
}
|
|
522
|
+
get [Symbol.toStringTag]() {
|
|
523
|
+
return "Token";
|
|
524
|
+
}
|
|
525
|
+
toJSON() {
|
|
526
|
+
return {
|
|
527
|
+
kind: this.kind,
|
|
528
|
+
value: this.value,
|
|
529
|
+
line: this.line,
|
|
530
|
+
column: this.column
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
const QueryDocumentKeys = {
|
|
535
|
+
Name: [],
|
|
536
|
+
Document: ["definitions"],
|
|
537
|
+
OperationDefinition: [
|
|
538
|
+
"name",
|
|
539
|
+
"variableDefinitions",
|
|
540
|
+
"directives",
|
|
541
|
+
"selectionSet"
|
|
542
|
+
],
|
|
543
|
+
VariableDefinition: ["variable", "type", "defaultValue", "directives"],
|
|
544
|
+
Variable: ["name"],
|
|
545
|
+
SelectionSet: ["selections"],
|
|
546
|
+
Field: ["alias", "name", "arguments", "directives", "selectionSet"],
|
|
547
|
+
Argument: ["name", "value"],
|
|
548
|
+
FragmentSpread: ["name", "directives"],
|
|
549
|
+
InlineFragment: ["typeCondition", "directives", "selectionSet"],
|
|
550
|
+
FragmentDefinition: [
|
|
551
|
+
"name",
|
|
552
|
+
// Note: fragment variable definitions are deprecated and will removed in v17.0.0
|
|
553
|
+
"variableDefinitions",
|
|
554
|
+
"typeCondition",
|
|
555
|
+
"directives",
|
|
556
|
+
"selectionSet"
|
|
557
|
+
],
|
|
558
|
+
IntValue: [],
|
|
559
|
+
FloatValue: [],
|
|
560
|
+
StringValue: [],
|
|
561
|
+
BooleanValue: [],
|
|
562
|
+
NullValue: [],
|
|
563
|
+
EnumValue: [],
|
|
564
|
+
ListValue: ["values"],
|
|
565
|
+
ObjectValue: ["fields"],
|
|
566
|
+
ObjectField: ["name", "value"],
|
|
567
|
+
Directive: ["name", "arguments"],
|
|
568
|
+
NamedType: ["name"],
|
|
569
|
+
ListType: ["type"],
|
|
570
|
+
NonNullType: ["type"],
|
|
571
|
+
SchemaDefinition: ["description", "directives", "operationTypes"],
|
|
572
|
+
OperationTypeDefinition: ["type"],
|
|
573
|
+
ScalarTypeDefinition: ["description", "name", "directives"],
|
|
574
|
+
ObjectTypeDefinition: [
|
|
575
|
+
"description",
|
|
576
|
+
"name",
|
|
577
|
+
"interfaces",
|
|
578
|
+
"directives",
|
|
579
|
+
"fields"
|
|
580
|
+
],
|
|
581
|
+
FieldDefinition: ["description", "name", "arguments", "type", "directives"],
|
|
582
|
+
InputValueDefinition: [
|
|
583
|
+
"description",
|
|
584
|
+
"name",
|
|
585
|
+
"type",
|
|
586
|
+
"defaultValue",
|
|
587
|
+
"directives"
|
|
588
|
+
],
|
|
589
|
+
InterfaceTypeDefinition: [
|
|
590
|
+
"description",
|
|
591
|
+
"name",
|
|
592
|
+
"interfaces",
|
|
593
|
+
"directives",
|
|
594
|
+
"fields"
|
|
595
|
+
],
|
|
596
|
+
UnionTypeDefinition: ["description", "name", "directives", "types"],
|
|
597
|
+
EnumTypeDefinition: ["description", "name", "directives", "values"],
|
|
598
|
+
EnumValueDefinition: ["description", "name", "directives"],
|
|
599
|
+
InputObjectTypeDefinition: ["description", "name", "directives", "fields"],
|
|
600
|
+
DirectiveDefinition: ["description", "name", "arguments", "locations"],
|
|
601
|
+
SchemaExtension: ["directives", "operationTypes"],
|
|
602
|
+
ScalarTypeExtension: ["name", "directives"],
|
|
603
|
+
ObjectTypeExtension: ["name", "interfaces", "directives", "fields"],
|
|
604
|
+
InterfaceTypeExtension: ["name", "interfaces", "directives", "fields"],
|
|
605
|
+
UnionTypeExtension: ["name", "directives", "types"],
|
|
606
|
+
EnumTypeExtension: ["name", "directives", "values"],
|
|
607
|
+
InputObjectTypeExtension: ["name", "directives", "fields"]
|
|
608
|
+
};
|
|
609
|
+
const kindValues = new Set(Object.keys(QueryDocumentKeys));
|
|
610
|
+
function isNode(maybeNode) {
|
|
611
|
+
const maybeKind = maybeNode === null || maybeNode === void 0 ? void 0 : maybeNode.kind;
|
|
612
|
+
return typeof maybeKind === "string" && kindValues.has(maybeKind);
|
|
613
|
+
}
|
|
614
|
+
var OperationTypeNode;
|
|
615
|
+
(function(OperationTypeNode2) {
|
|
616
|
+
OperationTypeNode2["QUERY"] = "query";
|
|
617
|
+
OperationTypeNode2["MUTATION"] = "mutation";
|
|
618
|
+
OperationTypeNode2["SUBSCRIPTION"] = "subscription";
|
|
619
|
+
})(OperationTypeNode || (OperationTypeNode = {}));
|
|
620
|
+
function isWhiteSpace(code) {
|
|
621
|
+
return code === 9 || code === 32;
|
|
622
|
+
}
|
|
623
|
+
function isDigit(code) {
|
|
624
|
+
return code >= 48 && code <= 57;
|
|
625
|
+
}
|
|
626
|
+
function isLetter(code) {
|
|
627
|
+
return code >= 97 && code <= 122 || // A-Z
|
|
628
|
+
code >= 65 && code <= 90;
|
|
629
|
+
}
|
|
630
|
+
function isNameStart(code) {
|
|
631
|
+
return isLetter(code) || code === 95;
|
|
632
|
+
}
|
|
633
|
+
function isNameContinue(code) {
|
|
634
|
+
return isLetter(code) || isDigit(code) || code === 95;
|
|
635
|
+
}
|
|
636
|
+
function dedentBlockStringLines(lines) {
|
|
637
|
+
var _firstNonEmptyLine2;
|
|
638
|
+
let commonIndent = Number.MAX_SAFE_INTEGER;
|
|
639
|
+
let firstNonEmptyLine = null;
|
|
640
|
+
let lastNonEmptyLine = -1;
|
|
641
|
+
for (let i = 0; i < lines.length; ++i) {
|
|
642
|
+
var _firstNonEmptyLine;
|
|
643
|
+
const line = lines[i];
|
|
644
|
+
const indent2 = leadingWhitespace(line);
|
|
645
|
+
if (indent2 === line.length) {
|
|
646
|
+
continue;
|
|
647
|
+
}
|
|
648
|
+
firstNonEmptyLine = (_firstNonEmptyLine = firstNonEmptyLine) !== null && _firstNonEmptyLine !== void 0 ? _firstNonEmptyLine : i;
|
|
649
|
+
lastNonEmptyLine = i;
|
|
650
|
+
if (i !== 0 && indent2 < commonIndent) {
|
|
651
|
+
commonIndent = indent2;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
return lines.map((line, i) => i === 0 ? line : line.slice(commonIndent)).slice(
|
|
655
|
+
(_firstNonEmptyLine2 = firstNonEmptyLine) !== null && _firstNonEmptyLine2 !== void 0 ? _firstNonEmptyLine2 : 0,
|
|
656
|
+
lastNonEmptyLine + 1
|
|
657
|
+
);
|
|
658
|
+
}
|
|
659
|
+
function leadingWhitespace(str) {
|
|
660
|
+
let i = 0;
|
|
661
|
+
while (i < str.length && isWhiteSpace(str.charCodeAt(i))) {
|
|
662
|
+
++i;
|
|
663
|
+
}
|
|
664
|
+
return i;
|
|
665
|
+
}
|
|
666
|
+
function printBlockString(value, options) {
|
|
667
|
+
const escapedValue = value.replace(/"""/g, '\\"""');
|
|
668
|
+
const lines = escapedValue.split(/\r\n|[\n\r]/g);
|
|
669
|
+
const isSingleLine = lines.length === 1;
|
|
670
|
+
const forceLeadingNewLine = lines.length > 1 && lines.slice(1).every((line) => line.length === 0 || isWhiteSpace(line.charCodeAt(0)));
|
|
671
|
+
const hasTrailingTripleQuotes = escapedValue.endsWith('\\"""');
|
|
672
|
+
const hasTrailingQuote = value.endsWith('"') && !hasTrailingTripleQuotes;
|
|
673
|
+
const hasTrailingSlash = value.endsWith("\\");
|
|
674
|
+
const forceTrailingNewline = hasTrailingQuote || hasTrailingSlash;
|
|
675
|
+
const printAsMultipleLines = !(options !== null && options !== void 0 && options.minimize) && // add leading and trailing new lines only if it improves readability
|
|
676
|
+
(!isSingleLine || value.length > 70 || forceTrailingNewline || forceLeadingNewLine || hasTrailingTripleQuotes);
|
|
677
|
+
let result = "";
|
|
678
|
+
const skipLeadingNewLine = isSingleLine && isWhiteSpace(value.charCodeAt(0));
|
|
679
|
+
if (printAsMultipleLines && !skipLeadingNewLine || forceLeadingNewLine) {
|
|
680
|
+
result += "\n";
|
|
681
|
+
}
|
|
682
|
+
result += escapedValue;
|
|
683
|
+
if (printAsMultipleLines || forceTrailingNewline) {
|
|
684
|
+
result += "\n";
|
|
685
|
+
}
|
|
686
|
+
return '"""' + result + '"""';
|
|
687
|
+
}
|
|
688
|
+
class Lexer {
|
|
689
|
+
/**
|
|
690
|
+
* The previously focused non-ignored token.
|
|
691
|
+
*/
|
|
692
|
+
/**
|
|
693
|
+
* The currently focused non-ignored token.
|
|
694
|
+
*/
|
|
695
|
+
/**
|
|
696
|
+
* The (1-indexed) line containing the current token.
|
|
697
|
+
*/
|
|
698
|
+
/**
|
|
699
|
+
* The character offset at which the current line begins.
|
|
700
|
+
*/
|
|
701
|
+
constructor(source) {
|
|
702
|
+
const startOfFileToken = new Token(TokenKind.SOF, 0, 0, 0, 0);
|
|
703
|
+
this.source = source;
|
|
704
|
+
this.lastToken = startOfFileToken;
|
|
705
|
+
this.token = startOfFileToken;
|
|
706
|
+
this.line = 1;
|
|
707
|
+
this.lineStart = 0;
|
|
708
|
+
}
|
|
709
|
+
get [Symbol.toStringTag]() {
|
|
710
|
+
return "Lexer";
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* Advances the token stream to the next non-ignored token.
|
|
714
|
+
*/
|
|
715
|
+
advance() {
|
|
716
|
+
this.lastToken = this.token;
|
|
717
|
+
const token = this.token = this.lookahead();
|
|
718
|
+
return token;
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* Looks ahead and returns the next non-ignored token, but does not change
|
|
722
|
+
* the state of Lexer.
|
|
723
|
+
*/
|
|
724
|
+
lookahead() {
|
|
725
|
+
let token = this.token;
|
|
726
|
+
if (token.kind !== TokenKind.EOF) {
|
|
727
|
+
do {
|
|
728
|
+
if (token.next) {
|
|
729
|
+
token = token.next;
|
|
730
|
+
} else {
|
|
731
|
+
const nextToken = readNextToken(this, token.end);
|
|
732
|
+
token.next = nextToken;
|
|
733
|
+
nextToken.prev = token;
|
|
734
|
+
token = nextToken;
|
|
735
|
+
}
|
|
736
|
+
} while (token.kind === TokenKind.COMMENT);
|
|
737
|
+
}
|
|
738
|
+
return token;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
function isPunctuatorTokenKind(kind) {
|
|
742
|
+
return kind === TokenKind.BANG || kind === TokenKind.DOLLAR || kind === TokenKind.AMP || kind === TokenKind.PAREN_L || kind === TokenKind.PAREN_R || kind === TokenKind.SPREAD || kind === TokenKind.COLON || kind === TokenKind.EQUALS || kind === TokenKind.AT || kind === TokenKind.BRACKET_L || kind === TokenKind.BRACKET_R || kind === TokenKind.BRACE_L || kind === TokenKind.PIPE || kind === TokenKind.BRACE_R;
|
|
743
|
+
}
|
|
744
|
+
function isUnicodeScalarValue(code) {
|
|
745
|
+
return code >= 0 && code <= 55295 || code >= 57344 && code <= 1114111;
|
|
746
|
+
}
|
|
747
|
+
function isSupplementaryCodePoint(body, location) {
|
|
748
|
+
return isLeadingSurrogate(body.charCodeAt(location)) && isTrailingSurrogate(body.charCodeAt(location + 1));
|
|
749
|
+
}
|
|
750
|
+
function isLeadingSurrogate(code) {
|
|
751
|
+
return code >= 55296 && code <= 56319;
|
|
752
|
+
}
|
|
753
|
+
function isTrailingSurrogate(code) {
|
|
754
|
+
return code >= 56320 && code <= 57343;
|
|
755
|
+
}
|
|
756
|
+
function printCodePointAt(lexer, location) {
|
|
757
|
+
const code = lexer.source.body.codePointAt(location);
|
|
758
|
+
if (code === void 0) {
|
|
759
|
+
return TokenKind.EOF;
|
|
760
|
+
} else if (code >= 32 && code <= 126) {
|
|
761
|
+
const char = String.fromCodePoint(code);
|
|
762
|
+
return char === '"' ? `'"'` : `"${char}"`;
|
|
763
|
+
}
|
|
764
|
+
return "U+" + code.toString(16).toUpperCase().padStart(4, "0");
|
|
765
|
+
}
|
|
766
|
+
function createToken(lexer, kind, start, end, value) {
|
|
767
|
+
const line = lexer.line;
|
|
768
|
+
const col = 1 + start - lexer.lineStart;
|
|
769
|
+
return new Token(kind, start, end, line, col, value);
|
|
770
|
+
}
|
|
771
|
+
function readNextToken(lexer, start) {
|
|
772
|
+
const body = lexer.source.body;
|
|
773
|
+
const bodyLength = body.length;
|
|
774
|
+
let position = start;
|
|
775
|
+
while (position < bodyLength) {
|
|
776
|
+
const code = body.charCodeAt(position);
|
|
777
|
+
switch (code) {
|
|
778
|
+
// Ignored ::
|
|
779
|
+
// - UnicodeBOM
|
|
780
|
+
// - WhiteSpace
|
|
781
|
+
// - LineTerminator
|
|
782
|
+
// - Comment
|
|
783
|
+
// - Comma
|
|
784
|
+
//
|
|
785
|
+
// UnicodeBOM :: "Byte Order Mark (U+FEFF)"
|
|
786
|
+
//
|
|
787
|
+
// WhiteSpace ::
|
|
788
|
+
// - "Horizontal Tab (U+0009)"
|
|
789
|
+
// - "Space (U+0020)"
|
|
790
|
+
//
|
|
791
|
+
// Comma :: ,
|
|
792
|
+
case 65279:
|
|
793
|
+
// <BOM>
|
|
794
|
+
case 9:
|
|
795
|
+
// \t
|
|
796
|
+
case 32:
|
|
797
|
+
// <space>
|
|
798
|
+
case 44:
|
|
799
|
+
++position;
|
|
800
|
+
continue;
|
|
801
|
+
// LineTerminator ::
|
|
802
|
+
// - "New Line (U+000A)"
|
|
803
|
+
// - "Carriage Return (U+000D)" [lookahead != "New Line (U+000A)"]
|
|
804
|
+
// - "Carriage Return (U+000D)" "New Line (U+000A)"
|
|
805
|
+
case 10:
|
|
806
|
+
++position;
|
|
807
|
+
++lexer.line;
|
|
808
|
+
lexer.lineStart = position;
|
|
809
|
+
continue;
|
|
810
|
+
case 13:
|
|
811
|
+
if (body.charCodeAt(position + 1) === 10) {
|
|
812
|
+
position += 2;
|
|
813
|
+
} else {
|
|
814
|
+
++position;
|
|
815
|
+
}
|
|
816
|
+
++lexer.line;
|
|
817
|
+
lexer.lineStart = position;
|
|
818
|
+
continue;
|
|
819
|
+
// Comment
|
|
820
|
+
case 35:
|
|
821
|
+
return readComment(lexer, position);
|
|
822
|
+
// Token ::
|
|
823
|
+
// - Punctuator
|
|
824
|
+
// - Name
|
|
825
|
+
// - IntValue
|
|
826
|
+
// - FloatValue
|
|
827
|
+
// - StringValue
|
|
828
|
+
//
|
|
829
|
+
// Punctuator :: one of ! $ & ( ) ... : = @ [ ] { | }
|
|
830
|
+
case 33:
|
|
831
|
+
return createToken(lexer, TokenKind.BANG, position, position + 1);
|
|
832
|
+
case 36:
|
|
833
|
+
return createToken(lexer, TokenKind.DOLLAR, position, position + 1);
|
|
834
|
+
case 38:
|
|
835
|
+
return createToken(lexer, TokenKind.AMP, position, position + 1);
|
|
836
|
+
case 40:
|
|
837
|
+
return createToken(lexer, TokenKind.PAREN_L, position, position + 1);
|
|
838
|
+
case 41:
|
|
839
|
+
return createToken(lexer, TokenKind.PAREN_R, position, position + 1);
|
|
840
|
+
case 46:
|
|
841
|
+
if (body.charCodeAt(position + 1) === 46 && body.charCodeAt(position + 2) === 46) {
|
|
842
|
+
return createToken(lexer, TokenKind.SPREAD, position, position + 3);
|
|
843
|
+
}
|
|
844
|
+
break;
|
|
845
|
+
case 58:
|
|
846
|
+
return createToken(lexer, TokenKind.COLON, position, position + 1);
|
|
847
|
+
case 61:
|
|
848
|
+
return createToken(lexer, TokenKind.EQUALS, position, position + 1);
|
|
849
|
+
case 64:
|
|
850
|
+
return createToken(lexer, TokenKind.AT, position, position + 1);
|
|
851
|
+
case 91:
|
|
852
|
+
return createToken(lexer, TokenKind.BRACKET_L, position, position + 1);
|
|
853
|
+
case 93:
|
|
854
|
+
return createToken(lexer, TokenKind.BRACKET_R, position, position + 1);
|
|
855
|
+
case 123:
|
|
856
|
+
return createToken(lexer, TokenKind.BRACE_L, position, position + 1);
|
|
857
|
+
case 124:
|
|
858
|
+
return createToken(lexer, TokenKind.PIPE, position, position + 1);
|
|
859
|
+
case 125:
|
|
860
|
+
return createToken(lexer, TokenKind.BRACE_R, position, position + 1);
|
|
861
|
+
// StringValue
|
|
862
|
+
case 34:
|
|
863
|
+
if (body.charCodeAt(position + 1) === 34 && body.charCodeAt(position + 2) === 34) {
|
|
864
|
+
return readBlockString(lexer, position);
|
|
865
|
+
}
|
|
866
|
+
return readString(lexer, position);
|
|
867
|
+
}
|
|
868
|
+
if (isDigit(code) || code === 45) {
|
|
869
|
+
return readNumber(lexer, position, code);
|
|
870
|
+
}
|
|
871
|
+
if (isNameStart(code)) {
|
|
872
|
+
return readName(lexer, position);
|
|
873
|
+
}
|
|
874
|
+
throw syntaxError(
|
|
875
|
+
lexer.source,
|
|
876
|
+
position,
|
|
877
|
+
code === 39 ? `Unexpected single quote character ('), did you mean to use a double quote (")?` : isUnicodeScalarValue(code) || isSupplementaryCodePoint(body, position) ? `Unexpected character: ${printCodePointAt(lexer, position)}.` : `Invalid character: ${printCodePointAt(lexer, position)}.`
|
|
878
|
+
);
|
|
879
|
+
}
|
|
880
|
+
return createToken(lexer, TokenKind.EOF, bodyLength, bodyLength);
|
|
881
|
+
}
|
|
882
|
+
function readComment(lexer, start) {
|
|
883
|
+
const body = lexer.source.body;
|
|
884
|
+
const bodyLength = body.length;
|
|
885
|
+
let position = start + 1;
|
|
886
|
+
while (position < bodyLength) {
|
|
887
|
+
const code = body.charCodeAt(position);
|
|
888
|
+
if (code === 10 || code === 13) {
|
|
889
|
+
break;
|
|
890
|
+
}
|
|
891
|
+
if (isUnicodeScalarValue(code)) {
|
|
892
|
+
++position;
|
|
893
|
+
} else if (isSupplementaryCodePoint(body, position)) {
|
|
894
|
+
position += 2;
|
|
895
|
+
} else {
|
|
896
|
+
break;
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
return createToken(
|
|
900
|
+
lexer,
|
|
901
|
+
TokenKind.COMMENT,
|
|
902
|
+
start,
|
|
903
|
+
position,
|
|
904
|
+
body.slice(start + 1, position)
|
|
905
|
+
);
|
|
906
|
+
}
|
|
907
|
+
function readNumber(lexer, start, firstCode) {
|
|
908
|
+
const body = lexer.source.body;
|
|
909
|
+
let position = start;
|
|
910
|
+
let code = firstCode;
|
|
911
|
+
let isFloat = false;
|
|
912
|
+
if (code === 45) {
|
|
913
|
+
code = body.charCodeAt(++position);
|
|
914
|
+
}
|
|
915
|
+
if (code === 48) {
|
|
916
|
+
code = body.charCodeAt(++position);
|
|
917
|
+
if (isDigit(code)) {
|
|
918
|
+
throw syntaxError(
|
|
919
|
+
lexer.source,
|
|
920
|
+
position,
|
|
921
|
+
`Invalid number, unexpected digit after 0: ${printCodePointAt(
|
|
922
|
+
lexer,
|
|
923
|
+
position
|
|
924
|
+
)}.`
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
} else {
|
|
928
|
+
position = readDigits(lexer, position, code);
|
|
929
|
+
code = body.charCodeAt(position);
|
|
930
|
+
}
|
|
931
|
+
if (code === 46) {
|
|
932
|
+
isFloat = true;
|
|
933
|
+
code = body.charCodeAt(++position);
|
|
934
|
+
position = readDigits(lexer, position, code);
|
|
935
|
+
code = body.charCodeAt(position);
|
|
936
|
+
}
|
|
937
|
+
if (code === 69 || code === 101) {
|
|
938
|
+
isFloat = true;
|
|
939
|
+
code = body.charCodeAt(++position);
|
|
940
|
+
if (code === 43 || code === 45) {
|
|
941
|
+
code = body.charCodeAt(++position);
|
|
942
|
+
}
|
|
943
|
+
position = readDigits(lexer, position, code);
|
|
944
|
+
code = body.charCodeAt(position);
|
|
945
|
+
}
|
|
946
|
+
if (code === 46 || isNameStart(code)) {
|
|
947
|
+
throw syntaxError(
|
|
948
|
+
lexer.source,
|
|
949
|
+
position,
|
|
950
|
+
`Invalid number, expected digit but got: ${printCodePointAt(
|
|
951
|
+
lexer,
|
|
952
|
+
position
|
|
953
|
+
)}.`
|
|
954
|
+
);
|
|
955
|
+
}
|
|
956
|
+
return createToken(
|
|
957
|
+
lexer,
|
|
958
|
+
isFloat ? TokenKind.FLOAT : TokenKind.INT,
|
|
959
|
+
start,
|
|
960
|
+
position,
|
|
961
|
+
body.slice(start, position)
|
|
962
|
+
);
|
|
963
|
+
}
|
|
964
|
+
function readDigits(lexer, start, firstCode) {
|
|
965
|
+
if (!isDigit(firstCode)) {
|
|
966
|
+
throw syntaxError(
|
|
967
|
+
lexer.source,
|
|
968
|
+
start,
|
|
969
|
+
`Invalid number, expected digit but got: ${printCodePointAt(
|
|
970
|
+
lexer,
|
|
971
|
+
start
|
|
972
|
+
)}.`
|
|
973
|
+
);
|
|
974
|
+
}
|
|
975
|
+
const body = lexer.source.body;
|
|
976
|
+
let position = start + 1;
|
|
977
|
+
while (isDigit(body.charCodeAt(position))) {
|
|
978
|
+
++position;
|
|
979
|
+
}
|
|
980
|
+
return position;
|
|
981
|
+
}
|
|
982
|
+
function readString(lexer, start) {
|
|
983
|
+
const body = lexer.source.body;
|
|
984
|
+
const bodyLength = body.length;
|
|
985
|
+
let position = start + 1;
|
|
986
|
+
let chunkStart = position;
|
|
987
|
+
let value = "";
|
|
988
|
+
while (position < bodyLength) {
|
|
989
|
+
const code = body.charCodeAt(position);
|
|
990
|
+
if (code === 34) {
|
|
991
|
+
value += body.slice(chunkStart, position);
|
|
992
|
+
return createToken(lexer, TokenKind.STRING, start, position + 1, value);
|
|
993
|
+
}
|
|
994
|
+
if (code === 92) {
|
|
995
|
+
value += body.slice(chunkStart, position);
|
|
996
|
+
const escape = body.charCodeAt(position + 1) === 117 ? body.charCodeAt(position + 2) === 123 ? readEscapedUnicodeVariableWidth(lexer, position) : readEscapedUnicodeFixedWidth(lexer, position) : readEscapedCharacter(lexer, position);
|
|
997
|
+
value += escape.value;
|
|
998
|
+
position += escape.size;
|
|
999
|
+
chunkStart = position;
|
|
1000
|
+
continue;
|
|
1001
|
+
}
|
|
1002
|
+
if (code === 10 || code === 13) {
|
|
1003
|
+
break;
|
|
1004
|
+
}
|
|
1005
|
+
if (isUnicodeScalarValue(code)) {
|
|
1006
|
+
++position;
|
|
1007
|
+
} else if (isSupplementaryCodePoint(body, position)) {
|
|
1008
|
+
position += 2;
|
|
1009
|
+
} else {
|
|
1010
|
+
throw syntaxError(
|
|
1011
|
+
lexer.source,
|
|
1012
|
+
position,
|
|
1013
|
+
`Invalid character within String: ${printCodePointAt(
|
|
1014
|
+
lexer,
|
|
1015
|
+
position
|
|
1016
|
+
)}.`
|
|
1017
|
+
);
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
throw syntaxError(lexer.source, position, "Unterminated string.");
|
|
1021
|
+
}
|
|
1022
|
+
function readEscapedUnicodeVariableWidth(lexer, position) {
|
|
1023
|
+
const body = lexer.source.body;
|
|
1024
|
+
let point = 0;
|
|
1025
|
+
let size = 3;
|
|
1026
|
+
while (size < 12) {
|
|
1027
|
+
const code = body.charCodeAt(position + size++);
|
|
1028
|
+
if (code === 125) {
|
|
1029
|
+
if (size < 5 || !isUnicodeScalarValue(point)) {
|
|
1030
|
+
break;
|
|
1031
|
+
}
|
|
1032
|
+
return {
|
|
1033
|
+
value: String.fromCodePoint(point),
|
|
1034
|
+
size
|
|
1035
|
+
};
|
|
1036
|
+
}
|
|
1037
|
+
point = point << 4 | readHexDigit(code);
|
|
1038
|
+
if (point < 0) {
|
|
1039
|
+
break;
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
throw syntaxError(
|
|
1043
|
+
lexer.source,
|
|
1044
|
+
position,
|
|
1045
|
+
`Invalid Unicode escape sequence: "${body.slice(
|
|
1046
|
+
position,
|
|
1047
|
+
position + size
|
|
1048
|
+
)}".`
|
|
1049
|
+
);
|
|
1050
|
+
}
|
|
1051
|
+
function readEscapedUnicodeFixedWidth(lexer, position) {
|
|
1052
|
+
const body = lexer.source.body;
|
|
1053
|
+
const code = read16BitHexCode(body, position + 2);
|
|
1054
|
+
if (isUnicodeScalarValue(code)) {
|
|
1055
|
+
return {
|
|
1056
|
+
value: String.fromCodePoint(code),
|
|
1057
|
+
size: 6
|
|
1058
|
+
};
|
|
1059
|
+
}
|
|
1060
|
+
if (isLeadingSurrogate(code)) {
|
|
1061
|
+
if (body.charCodeAt(position + 6) === 92 && body.charCodeAt(position + 7) === 117) {
|
|
1062
|
+
const trailingCode = read16BitHexCode(body, position + 8);
|
|
1063
|
+
if (isTrailingSurrogate(trailingCode)) {
|
|
1064
|
+
return {
|
|
1065
|
+
value: String.fromCodePoint(code, trailingCode),
|
|
1066
|
+
size: 12
|
|
1067
|
+
};
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
throw syntaxError(
|
|
1072
|
+
lexer.source,
|
|
1073
|
+
position,
|
|
1074
|
+
`Invalid Unicode escape sequence: "${body.slice(position, position + 6)}".`
|
|
1075
|
+
);
|
|
1076
|
+
}
|
|
1077
|
+
function read16BitHexCode(body, position) {
|
|
1078
|
+
return readHexDigit(body.charCodeAt(position)) << 12 | readHexDigit(body.charCodeAt(position + 1)) << 8 | readHexDigit(body.charCodeAt(position + 2)) << 4 | readHexDigit(body.charCodeAt(position + 3));
|
|
1079
|
+
}
|
|
1080
|
+
function readHexDigit(code) {
|
|
1081
|
+
return code >= 48 && code <= 57 ? code - 48 : code >= 65 && code <= 70 ? code - 55 : code >= 97 && code <= 102 ? code - 87 : -1;
|
|
1082
|
+
}
|
|
1083
|
+
function readEscapedCharacter(lexer, position) {
|
|
1084
|
+
const body = lexer.source.body;
|
|
1085
|
+
const code = body.charCodeAt(position + 1);
|
|
1086
|
+
switch (code) {
|
|
1087
|
+
case 34:
|
|
1088
|
+
return {
|
|
1089
|
+
value: '"',
|
|
1090
|
+
size: 2
|
|
1091
|
+
};
|
|
1092
|
+
case 92:
|
|
1093
|
+
return {
|
|
1094
|
+
value: "\\",
|
|
1095
|
+
size: 2
|
|
1096
|
+
};
|
|
1097
|
+
case 47:
|
|
1098
|
+
return {
|
|
1099
|
+
value: "/",
|
|
1100
|
+
size: 2
|
|
1101
|
+
};
|
|
1102
|
+
case 98:
|
|
1103
|
+
return {
|
|
1104
|
+
value: "\b",
|
|
1105
|
+
size: 2
|
|
1106
|
+
};
|
|
1107
|
+
case 102:
|
|
1108
|
+
return {
|
|
1109
|
+
value: "\f",
|
|
1110
|
+
size: 2
|
|
1111
|
+
};
|
|
1112
|
+
case 110:
|
|
1113
|
+
return {
|
|
1114
|
+
value: "\n",
|
|
1115
|
+
size: 2
|
|
1116
|
+
};
|
|
1117
|
+
case 114:
|
|
1118
|
+
return {
|
|
1119
|
+
value: "\r",
|
|
1120
|
+
size: 2
|
|
1121
|
+
};
|
|
1122
|
+
case 116:
|
|
1123
|
+
return {
|
|
1124
|
+
value: " ",
|
|
1125
|
+
size: 2
|
|
1126
|
+
};
|
|
1127
|
+
}
|
|
1128
|
+
throw syntaxError(
|
|
1129
|
+
lexer.source,
|
|
1130
|
+
position,
|
|
1131
|
+
`Invalid character escape sequence: "${body.slice(
|
|
1132
|
+
position,
|
|
1133
|
+
position + 2
|
|
1134
|
+
)}".`
|
|
1135
|
+
);
|
|
1136
|
+
}
|
|
1137
|
+
function readBlockString(lexer, start) {
|
|
1138
|
+
const body = lexer.source.body;
|
|
1139
|
+
const bodyLength = body.length;
|
|
1140
|
+
let lineStart = lexer.lineStart;
|
|
1141
|
+
let position = start + 3;
|
|
1142
|
+
let chunkStart = position;
|
|
1143
|
+
let currentLine = "";
|
|
1144
|
+
const blockLines = [];
|
|
1145
|
+
while (position < bodyLength) {
|
|
1146
|
+
const code = body.charCodeAt(position);
|
|
1147
|
+
if (code === 34 && body.charCodeAt(position + 1) === 34 && body.charCodeAt(position + 2) === 34) {
|
|
1148
|
+
currentLine += body.slice(chunkStart, position);
|
|
1149
|
+
blockLines.push(currentLine);
|
|
1150
|
+
const token = createToken(
|
|
1151
|
+
lexer,
|
|
1152
|
+
TokenKind.BLOCK_STRING,
|
|
1153
|
+
start,
|
|
1154
|
+
position + 3,
|
|
1155
|
+
// Return a string of the lines joined with U+000A.
|
|
1156
|
+
dedentBlockStringLines(blockLines).join("\n")
|
|
1157
|
+
);
|
|
1158
|
+
lexer.line += blockLines.length - 1;
|
|
1159
|
+
lexer.lineStart = lineStart;
|
|
1160
|
+
return token;
|
|
1161
|
+
}
|
|
1162
|
+
if (code === 92 && body.charCodeAt(position + 1) === 34 && body.charCodeAt(position + 2) === 34 && body.charCodeAt(position + 3) === 34) {
|
|
1163
|
+
currentLine += body.slice(chunkStart, position);
|
|
1164
|
+
chunkStart = position + 1;
|
|
1165
|
+
position += 4;
|
|
1166
|
+
continue;
|
|
1167
|
+
}
|
|
1168
|
+
if (code === 10 || code === 13) {
|
|
1169
|
+
currentLine += body.slice(chunkStart, position);
|
|
1170
|
+
blockLines.push(currentLine);
|
|
1171
|
+
if (code === 13 && body.charCodeAt(position + 1) === 10) {
|
|
1172
|
+
position += 2;
|
|
1173
|
+
} else {
|
|
1174
|
+
++position;
|
|
1175
|
+
}
|
|
1176
|
+
currentLine = "";
|
|
1177
|
+
chunkStart = position;
|
|
1178
|
+
lineStart = position;
|
|
1179
|
+
continue;
|
|
1180
|
+
}
|
|
1181
|
+
if (isUnicodeScalarValue(code)) {
|
|
1182
|
+
++position;
|
|
1183
|
+
} else if (isSupplementaryCodePoint(body, position)) {
|
|
1184
|
+
position += 2;
|
|
1185
|
+
} else {
|
|
1186
|
+
throw syntaxError(
|
|
1187
|
+
lexer.source,
|
|
1188
|
+
position,
|
|
1189
|
+
`Invalid character within String: ${printCodePointAt(
|
|
1190
|
+
lexer,
|
|
1191
|
+
position
|
|
1192
|
+
)}.`
|
|
1193
|
+
);
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
throw syntaxError(lexer.source, position, "Unterminated string.");
|
|
1197
|
+
}
|
|
1198
|
+
function readName(lexer, start) {
|
|
1199
|
+
const body = lexer.source.body;
|
|
1200
|
+
const bodyLength = body.length;
|
|
1201
|
+
let position = start + 1;
|
|
1202
|
+
while (position < bodyLength) {
|
|
1203
|
+
const code = body.charCodeAt(position);
|
|
1204
|
+
if (isNameContinue(code)) {
|
|
1205
|
+
++position;
|
|
1206
|
+
} else {
|
|
1207
|
+
break;
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
return createToken(
|
|
1211
|
+
lexer,
|
|
1212
|
+
TokenKind.NAME,
|
|
1213
|
+
start,
|
|
1214
|
+
position,
|
|
1215
|
+
body.slice(start, position)
|
|
1216
|
+
);
|
|
1217
|
+
}
|
|
1218
|
+
var DirectiveLocation;
|
|
1219
|
+
(function(DirectiveLocation2) {
|
|
1220
|
+
DirectiveLocation2["QUERY"] = "QUERY";
|
|
1221
|
+
DirectiveLocation2["MUTATION"] = "MUTATION";
|
|
1222
|
+
DirectiveLocation2["SUBSCRIPTION"] = "SUBSCRIPTION";
|
|
1223
|
+
DirectiveLocation2["FIELD"] = "FIELD";
|
|
1224
|
+
DirectiveLocation2["FRAGMENT_DEFINITION"] = "FRAGMENT_DEFINITION";
|
|
1225
|
+
DirectiveLocation2["FRAGMENT_SPREAD"] = "FRAGMENT_SPREAD";
|
|
1226
|
+
DirectiveLocation2["INLINE_FRAGMENT"] = "INLINE_FRAGMENT";
|
|
1227
|
+
DirectiveLocation2["VARIABLE_DEFINITION"] = "VARIABLE_DEFINITION";
|
|
1228
|
+
DirectiveLocation2["SCHEMA"] = "SCHEMA";
|
|
1229
|
+
DirectiveLocation2["SCALAR"] = "SCALAR";
|
|
1230
|
+
DirectiveLocation2["OBJECT"] = "OBJECT";
|
|
1231
|
+
DirectiveLocation2["FIELD_DEFINITION"] = "FIELD_DEFINITION";
|
|
1232
|
+
DirectiveLocation2["ARGUMENT_DEFINITION"] = "ARGUMENT_DEFINITION";
|
|
1233
|
+
DirectiveLocation2["INTERFACE"] = "INTERFACE";
|
|
1234
|
+
DirectiveLocation2["UNION"] = "UNION";
|
|
1235
|
+
DirectiveLocation2["ENUM"] = "ENUM";
|
|
1236
|
+
DirectiveLocation2["ENUM_VALUE"] = "ENUM_VALUE";
|
|
1237
|
+
DirectiveLocation2["INPUT_OBJECT"] = "INPUT_OBJECT";
|
|
1238
|
+
DirectiveLocation2["INPUT_FIELD_DEFINITION"] = "INPUT_FIELD_DEFINITION";
|
|
1239
|
+
})(DirectiveLocation || (DirectiveLocation = {}));
|
|
1240
|
+
function parse(source, options) {
|
|
1241
|
+
const parser = new Parser(source, options);
|
|
1242
|
+
const document = parser.parseDocument();
|
|
1243
|
+
Object.defineProperty(document, "tokenCount", {
|
|
1244
|
+
enumerable: false,
|
|
1245
|
+
value: parser.tokenCount
|
|
1246
|
+
});
|
|
1247
|
+
return document;
|
|
1248
|
+
}
|
|
1249
|
+
class Parser {
|
|
1250
|
+
constructor(source, options = {}) {
|
|
1251
|
+
const sourceObj = isSource(source) ? source : new Source(source);
|
|
1252
|
+
this._lexer = new Lexer(sourceObj);
|
|
1253
|
+
this._options = options;
|
|
1254
|
+
this._tokenCounter = 0;
|
|
1255
|
+
}
|
|
1256
|
+
get tokenCount() {
|
|
1257
|
+
return this._tokenCounter;
|
|
1258
|
+
}
|
|
1259
|
+
/**
|
|
1260
|
+
* Converts a name lex token into a name parse node.
|
|
1261
|
+
*/
|
|
1262
|
+
parseName() {
|
|
1263
|
+
const token = this.expectToken(TokenKind.NAME);
|
|
1264
|
+
return this.node(token, {
|
|
1265
|
+
kind: Kind.NAME,
|
|
1266
|
+
value: token.value
|
|
1267
|
+
});
|
|
1268
|
+
}
|
|
1269
|
+
// Implements the parsing rules in the Document section.
|
|
1270
|
+
/**
|
|
1271
|
+
* Document : Definition+
|
|
1272
|
+
*/
|
|
1273
|
+
parseDocument() {
|
|
1274
|
+
return this.node(this._lexer.token, {
|
|
1275
|
+
kind: Kind.DOCUMENT,
|
|
1276
|
+
definitions: this.many(
|
|
1277
|
+
TokenKind.SOF,
|
|
1278
|
+
this.parseDefinition,
|
|
1279
|
+
TokenKind.EOF
|
|
1280
|
+
)
|
|
1281
|
+
});
|
|
1282
|
+
}
|
|
1283
|
+
/**
|
|
1284
|
+
* Definition :
|
|
1285
|
+
* - ExecutableDefinition
|
|
1286
|
+
* - TypeSystemDefinition
|
|
1287
|
+
* - TypeSystemExtension
|
|
1288
|
+
*
|
|
1289
|
+
* ExecutableDefinition :
|
|
1290
|
+
* - OperationDefinition
|
|
1291
|
+
* - FragmentDefinition
|
|
1292
|
+
*
|
|
1293
|
+
* TypeSystemDefinition :
|
|
1294
|
+
* - SchemaDefinition
|
|
1295
|
+
* - TypeDefinition
|
|
1296
|
+
* - DirectiveDefinition
|
|
1297
|
+
*
|
|
1298
|
+
* TypeDefinition :
|
|
1299
|
+
* - ScalarTypeDefinition
|
|
1300
|
+
* - ObjectTypeDefinition
|
|
1301
|
+
* - InterfaceTypeDefinition
|
|
1302
|
+
* - UnionTypeDefinition
|
|
1303
|
+
* - EnumTypeDefinition
|
|
1304
|
+
* - InputObjectTypeDefinition
|
|
1305
|
+
*/
|
|
1306
|
+
parseDefinition() {
|
|
1307
|
+
if (this.peek(TokenKind.BRACE_L)) {
|
|
1308
|
+
return this.parseOperationDefinition();
|
|
1309
|
+
}
|
|
1310
|
+
const hasDescription = this.peekDescription();
|
|
1311
|
+
const keywordToken = hasDescription ? this._lexer.lookahead() : this._lexer.token;
|
|
1312
|
+
if (keywordToken.kind === TokenKind.NAME) {
|
|
1313
|
+
switch (keywordToken.value) {
|
|
1314
|
+
case "schema":
|
|
1315
|
+
return this.parseSchemaDefinition();
|
|
1316
|
+
case "scalar":
|
|
1317
|
+
return this.parseScalarTypeDefinition();
|
|
1318
|
+
case "type":
|
|
1319
|
+
return this.parseObjectTypeDefinition();
|
|
1320
|
+
case "interface":
|
|
1321
|
+
return this.parseInterfaceTypeDefinition();
|
|
1322
|
+
case "union":
|
|
1323
|
+
return this.parseUnionTypeDefinition();
|
|
1324
|
+
case "enum":
|
|
1325
|
+
return this.parseEnumTypeDefinition();
|
|
1326
|
+
case "input":
|
|
1327
|
+
return this.parseInputObjectTypeDefinition();
|
|
1328
|
+
case "directive":
|
|
1329
|
+
return this.parseDirectiveDefinition();
|
|
1330
|
+
}
|
|
1331
|
+
if (hasDescription) {
|
|
1332
|
+
throw syntaxError(
|
|
1333
|
+
this._lexer.source,
|
|
1334
|
+
this._lexer.token.start,
|
|
1335
|
+
"Unexpected description, descriptions are supported only on type definitions."
|
|
1336
|
+
);
|
|
1337
|
+
}
|
|
1338
|
+
switch (keywordToken.value) {
|
|
1339
|
+
case "query":
|
|
1340
|
+
case "mutation":
|
|
1341
|
+
case "subscription":
|
|
1342
|
+
return this.parseOperationDefinition();
|
|
1343
|
+
case "fragment":
|
|
1344
|
+
return this.parseFragmentDefinition();
|
|
1345
|
+
case "extend":
|
|
1346
|
+
return this.parseTypeSystemExtension();
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
throw this.unexpected(keywordToken);
|
|
1350
|
+
}
|
|
1351
|
+
// Implements the parsing rules in the Operations section.
|
|
1352
|
+
/**
|
|
1353
|
+
* OperationDefinition :
|
|
1354
|
+
* - SelectionSet
|
|
1355
|
+
* - OperationType Name? VariableDefinitions? Directives? SelectionSet
|
|
1356
|
+
*/
|
|
1357
|
+
parseOperationDefinition() {
|
|
1358
|
+
const start = this._lexer.token;
|
|
1359
|
+
if (this.peek(TokenKind.BRACE_L)) {
|
|
1360
|
+
return this.node(start, {
|
|
1361
|
+
kind: Kind.OPERATION_DEFINITION,
|
|
1362
|
+
operation: OperationTypeNode.QUERY,
|
|
1363
|
+
name: void 0,
|
|
1364
|
+
variableDefinitions: [],
|
|
1365
|
+
directives: [],
|
|
1366
|
+
selectionSet: this.parseSelectionSet()
|
|
1367
|
+
});
|
|
1368
|
+
}
|
|
1369
|
+
const operation = this.parseOperationType();
|
|
1370
|
+
let name;
|
|
1371
|
+
if (this.peek(TokenKind.NAME)) {
|
|
1372
|
+
name = this.parseName();
|
|
1373
|
+
}
|
|
1374
|
+
return this.node(start, {
|
|
1375
|
+
kind: Kind.OPERATION_DEFINITION,
|
|
1376
|
+
operation,
|
|
1377
|
+
name,
|
|
1378
|
+
variableDefinitions: this.parseVariableDefinitions(),
|
|
1379
|
+
directives: this.parseDirectives(false),
|
|
1380
|
+
selectionSet: this.parseSelectionSet()
|
|
1381
|
+
});
|
|
1382
|
+
}
|
|
1383
|
+
/**
|
|
1384
|
+
* OperationType : one of query mutation subscription
|
|
1385
|
+
*/
|
|
1386
|
+
parseOperationType() {
|
|
1387
|
+
const operationToken = this.expectToken(TokenKind.NAME);
|
|
1388
|
+
switch (operationToken.value) {
|
|
1389
|
+
case "query":
|
|
1390
|
+
return OperationTypeNode.QUERY;
|
|
1391
|
+
case "mutation":
|
|
1392
|
+
return OperationTypeNode.MUTATION;
|
|
1393
|
+
case "subscription":
|
|
1394
|
+
return OperationTypeNode.SUBSCRIPTION;
|
|
1395
|
+
}
|
|
1396
|
+
throw this.unexpected(operationToken);
|
|
1397
|
+
}
|
|
1398
|
+
/**
|
|
1399
|
+
* VariableDefinitions : ( VariableDefinition+ )
|
|
1400
|
+
*/
|
|
1401
|
+
parseVariableDefinitions() {
|
|
1402
|
+
return this.optionalMany(
|
|
1403
|
+
TokenKind.PAREN_L,
|
|
1404
|
+
this.parseVariableDefinition,
|
|
1405
|
+
TokenKind.PAREN_R
|
|
1406
|
+
);
|
|
1407
|
+
}
|
|
1408
|
+
/**
|
|
1409
|
+
* VariableDefinition : Variable : Type DefaultValue? Directives[Const]?
|
|
1410
|
+
*/
|
|
1411
|
+
parseVariableDefinition() {
|
|
1412
|
+
return this.node(this._lexer.token, {
|
|
1413
|
+
kind: Kind.VARIABLE_DEFINITION,
|
|
1414
|
+
variable: this.parseVariable(),
|
|
1415
|
+
type: (this.expectToken(TokenKind.COLON), this.parseTypeReference()),
|
|
1416
|
+
defaultValue: this.expectOptionalToken(TokenKind.EQUALS) ? this.parseConstValueLiteral() : void 0,
|
|
1417
|
+
directives: this.parseConstDirectives()
|
|
1418
|
+
});
|
|
1419
|
+
}
|
|
1420
|
+
/**
|
|
1421
|
+
* Variable : $ Name
|
|
1422
|
+
*/
|
|
1423
|
+
parseVariable() {
|
|
1424
|
+
const start = this._lexer.token;
|
|
1425
|
+
this.expectToken(TokenKind.DOLLAR);
|
|
1426
|
+
return this.node(start, {
|
|
1427
|
+
kind: Kind.VARIABLE,
|
|
1428
|
+
name: this.parseName()
|
|
1429
|
+
});
|
|
1430
|
+
}
|
|
1431
|
+
/**
|
|
1432
|
+
* ```
|
|
1433
|
+
* SelectionSet : { Selection+ }
|
|
1434
|
+
* ```
|
|
1435
|
+
*/
|
|
1436
|
+
parseSelectionSet() {
|
|
1437
|
+
return this.node(this._lexer.token, {
|
|
1438
|
+
kind: Kind.SELECTION_SET,
|
|
1439
|
+
selections: this.many(
|
|
1440
|
+
TokenKind.BRACE_L,
|
|
1441
|
+
this.parseSelection,
|
|
1442
|
+
TokenKind.BRACE_R
|
|
1443
|
+
)
|
|
1444
|
+
});
|
|
1445
|
+
}
|
|
1446
|
+
/**
|
|
1447
|
+
* Selection :
|
|
1448
|
+
* - Field
|
|
1449
|
+
* - FragmentSpread
|
|
1450
|
+
* - InlineFragment
|
|
1451
|
+
*/
|
|
1452
|
+
parseSelection() {
|
|
1453
|
+
return this.peek(TokenKind.SPREAD) ? this.parseFragment() : this.parseField();
|
|
1454
|
+
}
|
|
1455
|
+
/**
|
|
1456
|
+
* Field : Alias? Name Arguments? Directives? SelectionSet?
|
|
1457
|
+
*
|
|
1458
|
+
* Alias : Name :
|
|
1459
|
+
*/
|
|
1460
|
+
parseField() {
|
|
1461
|
+
const start = this._lexer.token;
|
|
1462
|
+
const nameOrAlias = this.parseName();
|
|
1463
|
+
let alias;
|
|
1464
|
+
let name;
|
|
1465
|
+
if (this.expectOptionalToken(TokenKind.COLON)) {
|
|
1466
|
+
alias = nameOrAlias;
|
|
1467
|
+
name = this.parseName();
|
|
1468
|
+
} else {
|
|
1469
|
+
name = nameOrAlias;
|
|
1470
|
+
}
|
|
1471
|
+
return this.node(start, {
|
|
1472
|
+
kind: Kind.FIELD,
|
|
1473
|
+
alias,
|
|
1474
|
+
name,
|
|
1475
|
+
arguments: this.parseArguments(false),
|
|
1476
|
+
directives: this.parseDirectives(false),
|
|
1477
|
+
selectionSet: this.peek(TokenKind.BRACE_L) ? this.parseSelectionSet() : void 0
|
|
1478
|
+
});
|
|
1479
|
+
}
|
|
1480
|
+
/**
|
|
1481
|
+
* Arguments[Const] : ( Argument[?Const]+ )
|
|
1482
|
+
*/
|
|
1483
|
+
parseArguments(isConst) {
|
|
1484
|
+
const item = isConst ? this.parseConstArgument : this.parseArgument;
|
|
1485
|
+
return this.optionalMany(TokenKind.PAREN_L, item, TokenKind.PAREN_R);
|
|
1486
|
+
}
|
|
1487
|
+
/**
|
|
1488
|
+
* Argument[Const] : Name : Value[?Const]
|
|
1489
|
+
*/
|
|
1490
|
+
parseArgument(isConst = false) {
|
|
1491
|
+
const start = this._lexer.token;
|
|
1492
|
+
const name = this.parseName();
|
|
1493
|
+
this.expectToken(TokenKind.COLON);
|
|
1494
|
+
return this.node(start, {
|
|
1495
|
+
kind: Kind.ARGUMENT,
|
|
1496
|
+
name,
|
|
1497
|
+
value: this.parseValueLiteral(isConst)
|
|
1498
|
+
});
|
|
1499
|
+
}
|
|
1500
|
+
parseConstArgument() {
|
|
1501
|
+
return this.parseArgument(true);
|
|
1502
|
+
}
|
|
1503
|
+
// Implements the parsing rules in the Fragments section.
|
|
1504
|
+
/**
|
|
1505
|
+
* Corresponds to both FragmentSpread and InlineFragment in the spec.
|
|
1506
|
+
*
|
|
1507
|
+
* FragmentSpread : ... FragmentName Directives?
|
|
1508
|
+
*
|
|
1509
|
+
* InlineFragment : ... TypeCondition? Directives? SelectionSet
|
|
1510
|
+
*/
|
|
1511
|
+
parseFragment() {
|
|
1512
|
+
const start = this._lexer.token;
|
|
1513
|
+
this.expectToken(TokenKind.SPREAD);
|
|
1514
|
+
const hasTypeCondition = this.expectOptionalKeyword("on");
|
|
1515
|
+
if (!hasTypeCondition && this.peek(TokenKind.NAME)) {
|
|
1516
|
+
return this.node(start, {
|
|
1517
|
+
kind: Kind.FRAGMENT_SPREAD,
|
|
1518
|
+
name: this.parseFragmentName(),
|
|
1519
|
+
directives: this.parseDirectives(false)
|
|
1520
|
+
});
|
|
1521
|
+
}
|
|
1522
|
+
return this.node(start, {
|
|
1523
|
+
kind: Kind.INLINE_FRAGMENT,
|
|
1524
|
+
typeCondition: hasTypeCondition ? this.parseNamedType() : void 0,
|
|
1525
|
+
directives: this.parseDirectives(false),
|
|
1526
|
+
selectionSet: this.parseSelectionSet()
|
|
1527
|
+
});
|
|
1528
|
+
}
|
|
1529
|
+
/**
|
|
1530
|
+
* FragmentDefinition :
|
|
1531
|
+
* - fragment FragmentName on TypeCondition Directives? SelectionSet
|
|
1532
|
+
*
|
|
1533
|
+
* TypeCondition : NamedType
|
|
1534
|
+
*/
|
|
1535
|
+
parseFragmentDefinition() {
|
|
1536
|
+
const start = this._lexer.token;
|
|
1537
|
+
this.expectKeyword("fragment");
|
|
1538
|
+
if (this._options.allowLegacyFragmentVariables === true) {
|
|
1539
|
+
return this.node(start, {
|
|
1540
|
+
kind: Kind.FRAGMENT_DEFINITION,
|
|
1541
|
+
name: this.parseFragmentName(),
|
|
1542
|
+
variableDefinitions: this.parseVariableDefinitions(),
|
|
1543
|
+
typeCondition: (this.expectKeyword("on"), this.parseNamedType()),
|
|
1544
|
+
directives: this.parseDirectives(false),
|
|
1545
|
+
selectionSet: this.parseSelectionSet()
|
|
1546
|
+
});
|
|
1547
|
+
}
|
|
1548
|
+
return this.node(start, {
|
|
1549
|
+
kind: Kind.FRAGMENT_DEFINITION,
|
|
1550
|
+
name: this.parseFragmentName(),
|
|
1551
|
+
typeCondition: (this.expectKeyword("on"), this.parseNamedType()),
|
|
1552
|
+
directives: this.parseDirectives(false),
|
|
1553
|
+
selectionSet: this.parseSelectionSet()
|
|
1554
|
+
});
|
|
1555
|
+
}
|
|
1556
|
+
/**
|
|
1557
|
+
* FragmentName : Name but not `on`
|
|
1558
|
+
*/
|
|
1559
|
+
parseFragmentName() {
|
|
1560
|
+
if (this._lexer.token.value === "on") {
|
|
1561
|
+
throw this.unexpected();
|
|
1562
|
+
}
|
|
1563
|
+
return this.parseName();
|
|
1564
|
+
}
|
|
1565
|
+
// Implements the parsing rules in the Values section.
|
|
1566
|
+
/**
|
|
1567
|
+
* Value[Const] :
|
|
1568
|
+
* - [~Const] Variable
|
|
1569
|
+
* - IntValue
|
|
1570
|
+
* - FloatValue
|
|
1571
|
+
* - StringValue
|
|
1572
|
+
* - BooleanValue
|
|
1573
|
+
* - NullValue
|
|
1574
|
+
* - EnumValue
|
|
1575
|
+
* - ListValue[?Const]
|
|
1576
|
+
* - ObjectValue[?Const]
|
|
1577
|
+
*
|
|
1578
|
+
* BooleanValue : one of `true` `false`
|
|
1579
|
+
*
|
|
1580
|
+
* NullValue : `null`
|
|
1581
|
+
*
|
|
1582
|
+
* EnumValue : Name but not `true`, `false` or `null`
|
|
1583
|
+
*/
|
|
1584
|
+
parseValueLiteral(isConst) {
|
|
1585
|
+
const token = this._lexer.token;
|
|
1586
|
+
switch (token.kind) {
|
|
1587
|
+
case TokenKind.BRACKET_L:
|
|
1588
|
+
return this.parseList(isConst);
|
|
1589
|
+
case TokenKind.BRACE_L:
|
|
1590
|
+
return this.parseObject(isConst);
|
|
1591
|
+
case TokenKind.INT:
|
|
1592
|
+
this.advanceLexer();
|
|
1593
|
+
return this.node(token, {
|
|
1594
|
+
kind: Kind.INT,
|
|
1595
|
+
value: token.value
|
|
1596
|
+
});
|
|
1597
|
+
case TokenKind.FLOAT:
|
|
1598
|
+
this.advanceLexer();
|
|
1599
|
+
return this.node(token, {
|
|
1600
|
+
kind: Kind.FLOAT,
|
|
1601
|
+
value: token.value
|
|
1602
|
+
});
|
|
1603
|
+
case TokenKind.STRING:
|
|
1604
|
+
case TokenKind.BLOCK_STRING:
|
|
1605
|
+
return this.parseStringLiteral();
|
|
1606
|
+
case TokenKind.NAME:
|
|
1607
|
+
this.advanceLexer();
|
|
1608
|
+
switch (token.value) {
|
|
1609
|
+
case "true":
|
|
1610
|
+
return this.node(token, {
|
|
1611
|
+
kind: Kind.BOOLEAN,
|
|
1612
|
+
value: true
|
|
1613
|
+
});
|
|
1614
|
+
case "false":
|
|
1615
|
+
return this.node(token, {
|
|
1616
|
+
kind: Kind.BOOLEAN,
|
|
1617
|
+
value: false
|
|
1618
|
+
});
|
|
1619
|
+
case "null":
|
|
1620
|
+
return this.node(token, {
|
|
1621
|
+
kind: Kind.NULL
|
|
1622
|
+
});
|
|
1623
|
+
default:
|
|
1624
|
+
return this.node(token, {
|
|
1625
|
+
kind: Kind.ENUM,
|
|
1626
|
+
value: token.value
|
|
1627
|
+
});
|
|
1628
|
+
}
|
|
1629
|
+
case TokenKind.DOLLAR:
|
|
1630
|
+
if (isConst) {
|
|
1631
|
+
this.expectToken(TokenKind.DOLLAR);
|
|
1632
|
+
if (this._lexer.token.kind === TokenKind.NAME) {
|
|
1633
|
+
const varName = this._lexer.token.value;
|
|
1634
|
+
throw syntaxError(
|
|
1635
|
+
this._lexer.source,
|
|
1636
|
+
token.start,
|
|
1637
|
+
`Unexpected variable "$${varName}" in constant value.`
|
|
1638
|
+
);
|
|
1639
|
+
} else {
|
|
1640
|
+
throw this.unexpected(token);
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1643
|
+
return this.parseVariable();
|
|
1644
|
+
default:
|
|
1645
|
+
throw this.unexpected();
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
parseConstValueLiteral() {
|
|
1649
|
+
return this.parseValueLiteral(true);
|
|
1650
|
+
}
|
|
1651
|
+
parseStringLiteral() {
|
|
1652
|
+
const token = this._lexer.token;
|
|
1653
|
+
this.advanceLexer();
|
|
1654
|
+
return this.node(token, {
|
|
1655
|
+
kind: Kind.STRING,
|
|
1656
|
+
value: token.value,
|
|
1657
|
+
block: token.kind === TokenKind.BLOCK_STRING
|
|
1658
|
+
});
|
|
1659
|
+
}
|
|
1660
|
+
/**
|
|
1661
|
+
* ListValue[Const] :
|
|
1662
|
+
* - [ ]
|
|
1663
|
+
* - [ Value[?Const]+ ]
|
|
1664
|
+
*/
|
|
1665
|
+
parseList(isConst) {
|
|
1666
|
+
const item = () => this.parseValueLiteral(isConst);
|
|
1667
|
+
return this.node(this._lexer.token, {
|
|
1668
|
+
kind: Kind.LIST,
|
|
1669
|
+
values: this.any(TokenKind.BRACKET_L, item, TokenKind.BRACKET_R)
|
|
1670
|
+
});
|
|
1671
|
+
}
|
|
1672
|
+
/**
|
|
1673
|
+
* ```
|
|
1674
|
+
* ObjectValue[Const] :
|
|
1675
|
+
* - { }
|
|
1676
|
+
* - { ObjectField[?Const]+ }
|
|
1677
|
+
* ```
|
|
1678
|
+
*/
|
|
1679
|
+
parseObject(isConst) {
|
|
1680
|
+
const item = () => this.parseObjectField(isConst);
|
|
1681
|
+
return this.node(this._lexer.token, {
|
|
1682
|
+
kind: Kind.OBJECT,
|
|
1683
|
+
fields: this.any(TokenKind.BRACE_L, item, TokenKind.BRACE_R)
|
|
1684
|
+
});
|
|
1685
|
+
}
|
|
1686
|
+
/**
|
|
1687
|
+
* ObjectField[Const] : Name : Value[?Const]
|
|
1688
|
+
*/
|
|
1689
|
+
parseObjectField(isConst) {
|
|
1690
|
+
const start = this._lexer.token;
|
|
1691
|
+
const name = this.parseName();
|
|
1692
|
+
this.expectToken(TokenKind.COLON);
|
|
1693
|
+
return this.node(start, {
|
|
1694
|
+
kind: Kind.OBJECT_FIELD,
|
|
1695
|
+
name,
|
|
1696
|
+
value: this.parseValueLiteral(isConst)
|
|
1697
|
+
});
|
|
1698
|
+
}
|
|
1699
|
+
// Implements the parsing rules in the Directives section.
|
|
1700
|
+
/**
|
|
1701
|
+
* Directives[Const] : Directive[?Const]+
|
|
1702
|
+
*/
|
|
1703
|
+
parseDirectives(isConst) {
|
|
1704
|
+
const directives = [];
|
|
1705
|
+
while (this.peek(TokenKind.AT)) {
|
|
1706
|
+
directives.push(this.parseDirective(isConst));
|
|
1707
|
+
}
|
|
1708
|
+
return directives;
|
|
1709
|
+
}
|
|
1710
|
+
parseConstDirectives() {
|
|
1711
|
+
return this.parseDirectives(true);
|
|
1712
|
+
}
|
|
1713
|
+
/**
|
|
1714
|
+
* ```
|
|
1715
|
+
* Directive[Const] : @ Name Arguments[?Const]?
|
|
1716
|
+
* ```
|
|
1717
|
+
*/
|
|
1718
|
+
parseDirective(isConst) {
|
|
1719
|
+
const start = this._lexer.token;
|
|
1720
|
+
this.expectToken(TokenKind.AT);
|
|
1721
|
+
return this.node(start, {
|
|
1722
|
+
kind: Kind.DIRECTIVE,
|
|
1723
|
+
name: this.parseName(),
|
|
1724
|
+
arguments: this.parseArguments(isConst)
|
|
1725
|
+
});
|
|
1726
|
+
}
|
|
1727
|
+
// Implements the parsing rules in the Types section.
|
|
1728
|
+
/**
|
|
1729
|
+
* Type :
|
|
1730
|
+
* - NamedType
|
|
1731
|
+
* - ListType
|
|
1732
|
+
* - NonNullType
|
|
1733
|
+
*/
|
|
1734
|
+
parseTypeReference() {
|
|
1735
|
+
const start = this._lexer.token;
|
|
1736
|
+
let type;
|
|
1737
|
+
if (this.expectOptionalToken(TokenKind.BRACKET_L)) {
|
|
1738
|
+
const innerType = this.parseTypeReference();
|
|
1739
|
+
this.expectToken(TokenKind.BRACKET_R);
|
|
1740
|
+
type = this.node(start, {
|
|
1741
|
+
kind: Kind.LIST_TYPE,
|
|
1742
|
+
type: innerType
|
|
1743
|
+
});
|
|
1744
|
+
} else {
|
|
1745
|
+
type = this.parseNamedType();
|
|
1746
|
+
}
|
|
1747
|
+
if (this.expectOptionalToken(TokenKind.BANG)) {
|
|
1748
|
+
return this.node(start, {
|
|
1749
|
+
kind: Kind.NON_NULL_TYPE,
|
|
1750
|
+
type
|
|
1751
|
+
});
|
|
1752
|
+
}
|
|
1753
|
+
return type;
|
|
1754
|
+
}
|
|
1755
|
+
/**
|
|
1756
|
+
* NamedType : Name
|
|
1757
|
+
*/
|
|
1758
|
+
parseNamedType() {
|
|
1759
|
+
return this.node(this._lexer.token, {
|
|
1760
|
+
kind: Kind.NAMED_TYPE,
|
|
1761
|
+
name: this.parseName()
|
|
1762
|
+
});
|
|
1763
|
+
}
|
|
1764
|
+
// Implements the parsing rules in the Type Definition section.
|
|
1765
|
+
peekDescription() {
|
|
1766
|
+
return this.peek(TokenKind.STRING) || this.peek(TokenKind.BLOCK_STRING);
|
|
1767
|
+
}
|
|
1768
|
+
/**
|
|
1769
|
+
* Description : StringValue
|
|
1770
|
+
*/
|
|
1771
|
+
parseDescription() {
|
|
1772
|
+
if (this.peekDescription()) {
|
|
1773
|
+
return this.parseStringLiteral();
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
/**
|
|
1777
|
+
* ```
|
|
1778
|
+
* SchemaDefinition : Description? schema Directives[Const]? { OperationTypeDefinition+ }
|
|
1779
|
+
* ```
|
|
1780
|
+
*/
|
|
1781
|
+
parseSchemaDefinition() {
|
|
1782
|
+
const start = this._lexer.token;
|
|
1783
|
+
const description = this.parseDescription();
|
|
1784
|
+
this.expectKeyword("schema");
|
|
1785
|
+
const directives = this.parseConstDirectives();
|
|
1786
|
+
const operationTypes = this.many(
|
|
1787
|
+
TokenKind.BRACE_L,
|
|
1788
|
+
this.parseOperationTypeDefinition,
|
|
1789
|
+
TokenKind.BRACE_R
|
|
1790
|
+
);
|
|
1791
|
+
return this.node(start, {
|
|
1792
|
+
kind: Kind.SCHEMA_DEFINITION,
|
|
1793
|
+
description,
|
|
1794
|
+
directives,
|
|
1795
|
+
operationTypes
|
|
1796
|
+
});
|
|
1797
|
+
}
|
|
1798
|
+
/**
|
|
1799
|
+
* OperationTypeDefinition : OperationType : NamedType
|
|
1800
|
+
*/
|
|
1801
|
+
parseOperationTypeDefinition() {
|
|
1802
|
+
const start = this._lexer.token;
|
|
1803
|
+
const operation = this.parseOperationType();
|
|
1804
|
+
this.expectToken(TokenKind.COLON);
|
|
1805
|
+
const type = this.parseNamedType();
|
|
1806
|
+
return this.node(start, {
|
|
1807
|
+
kind: Kind.OPERATION_TYPE_DEFINITION,
|
|
1808
|
+
operation,
|
|
1809
|
+
type
|
|
1810
|
+
});
|
|
1811
|
+
}
|
|
1812
|
+
/**
|
|
1813
|
+
* ScalarTypeDefinition : Description? scalar Name Directives[Const]?
|
|
1814
|
+
*/
|
|
1815
|
+
parseScalarTypeDefinition() {
|
|
1816
|
+
const start = this._lexer.token;
|
|
1817
|
+
const description = this.parseDescription();
|
|
1818
|
+
this.expectKeyword("scalar");
|
|
1819
|
+
const name = this.parseName();
|
|
1820
|
+
const directives = this.parseConstDirectives();
|
|
1821
|
+
return this.node(start, {
|
|
1822
|
+
kind: Kind.SCALAR_TYPE_DEFINITION,
|
|
1823
|
+
description,
|
|
1824
|
+
name,
|
|
1825
|
+
directives
|
|
1826
|
+
});
|
|
1827
|
+
}
|
|
1828
|
+
/**
|
|
1829
|
+
* ObjectTypeDefinition :
|
|
1830
|
+
* Description?
|
|
1831
|
+
* type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition?
|
|
1832
|
+
*/
|
|
1833
|
+
parseObjectTypeDefinition() {
|
|
1834
|
+
const start = this._lexer.token;
|
|
1835
|
+
const description = this.parseDescription();
|
|
1836
|
+
this.expectKeyword("type");
|
|
1837
|
+
const name = this.parseName();
|
|
1838
|
+
const interfaces = this.parseImplementsInterfaces();
|
|
1839
|
+
const directives = this.parseConstDirectives();
|
|
1840
|
+
const fields = this.parseFieldsDefinition();
|
|
1841
|
+
return this.node(start, {
|
|
1842
|
+
kind: Kind.OBJECT_TYPE_DEFINITION,
|
|
1843
|
+
description,
|
|
1844
|
+
name,
|
|
1845
|
+
interfaces,
|
|
1846
|
+
directives,
|
|
1847
|
+
fields
|
|
1848
|
+
});
|
|
1849
|
+
}
|
|
1850
|
+
/**
|
|
1851
|
+
* ImplementsInterfaces :
|
|
1852
|
+
* - implements `&`? NamedType
|
|
1853
|
+
* - ImplementsInterfaces & NamedType
|
|
1854
|
+
*/
|
|
1855
|
+
parseImplementsInterfaces() {
|
|
1856
|
+
return this.expectOptionalKeyword("implements") ? this.delimitedMany(TokenKind.AMP, this.parseNamedType) : [];
|
|
1857
|
+
}
|
|
1858
|
+
/**
|
|
1859
|
+
* ```
|
|
1860
|
+
* FieldsDefinition : { FieldDefinition+ }
|
|
1861
|
+
* ```
|
|
1862
|
+
*/
|
|
1863
|
+
parseFieldsDefinition() {
|
|
1864
|
+
return this.optionalMany(
|
|
1865
|
+
TokenKind.BRACE_L,
|
|
1866
|
+
this.parseFieldDefinition,
|
|
1867
|
+
TokenKind.BRACE_R
|
|
1868
|
+
);
|
|
1869
|
+
}
|
|
1870
|
+
/**
|
|
1871
|
+
* FieldDefinition :
|
|
1872
|
+
* - Description? Name ArgumentsDefinition? : Type Directives[Const]?
|
|
1873
|
+
*/
|
|
1874
|
+
parseFieldDefinition() {
|
|
1875
|
+
const start = this._lexer.token;
|
|
1876
|
+
const description = this.parseDescription();
|
|
1877
|
+
const name = this.parseName();
|
|
1878
|
+
const args = this.parseArgumentDefs();
|
|
1879
|
+
this.expectToken(TokenKind.COLON);
|
|
1880
|
+
const type = this.parseTypeReference();
|
|
1881
|
+
const directives = this.parseConstDirectives();
|
|
1882
|
+
return this.node(start, {
|
|
1883
|
+
kind: Kind.FIELD_DEFINITION,
|
|
1884
|
+
description,
|
|
1885
|
+
name,
|
|
1886
|
+
arguments: args,
|
|
1887
|
+
type,
|
|
1888
|
+
directives
|
|
1889
|
+
});
|
|
1890
|
+
}
|
|
1891
|
+
/**
|
|
1892
|
+
* ArgumentsDefinition : ( InputValueDefinition+ )
|
|
1893
|
+
*/
|
|
1894
|
+
parseArgumentDefs() {
|
|
1895
|
+
return this.optionalMany(
|
|
1896
|
+
TokenKind.PAREN_L,
|
|
1897
|
+
this.parseInputValueDef,
|
|
1898
|
+
TokenKind.PAREN_R
|
|
1899
|
+
);
|
|
1900
|
+
}
|
|
1901
|
+
/**
|
|
1902
|
+
* InputValueDefinition :
|
|
1903
|
+
* - Description? Name : Type DefaultValue? Directives[Const]?
|
|
1904
|
+
*/
|
|
1905
|
+
parseInputValueDef() {
|
|
1906
|
+
const start = this._lexer.token;
|
|
1907
|
+
const description = this.parseDescription();
|
|
1908
|
+
const name = this.parseName();
|
|
1909
|
+
this.expectToken(TokenKind.COLON);
|
|
1910
|
+
const type = this.parseTypeReference();
|
|
1911
|
+
let defaultValue;
|
|
1912
|
+
if (this.expectOptionalToken(TokenKind.EQUALS)) {
|
|
1913
|
+
defaultValue = this.parseConstValueLiteral();
|
|
1914
|
+
}
|
|
1915
|
+
const directives = this.parseConstDirectives();
|
|
1916
|
+
return this.node(start, {
|
|
1917
|
+
kind: Kind.INPUT_VALUE_DEFINITION,
|
|
1918
|
+
description,
|
|
1919
|
+
name,
|
|
1920
|
+
type,
|
|
1921
|
+
defaultValue,
|
|
1922
|
+
directives
|
|
1923
|
+
});
|
|
1924
|
+
}
|
|
1925
|
+
/**
|
|
1926
|
+
* InterfaceTypeDefinition :
|
|
1927
|
+
* - Description? interface Name Directives[Const]? FieldsDefinition?
|
|
1928
|
+
*/
|
|
1929
|
+
parseInterfaceTypeDefinition() {
|
|
1930
|
+
const start = this._lexer.token;
|
|
1931
|
+
const description = this.parseDescription();
|
|
1932
|
+
this.expectKeyword("interface");
|
|
1933
|
+
const name = this.parseName();
|
|
1934
|
+
const interfaces = this.parseImplementsInterfaces();
|
|
1935
|
+
const directives = this.parseConstDirectives();
|
|
1936
|
+
const fields = this.parseFieldsDefinition();
|
|
1937
|
+
return this.node(start, {
|
|
1938
|
+
kind: Kind.INTERFACE_TYPE_DEFINITION,
|
|
1939
|
+
description,
|
|
1940
|
+
name,
|
|
1941
|
+
interfaces,
|
|
1942
|
+
directives,
|
|
1943
|
+
fields
|
|
1944
|
+
});
|
|
1945
|
+
}
|
|
1946
|
+
/**
|
|
1947
|
+
* UnionTypeDefinition :
|
|
1948
|
+
* - Description? union Name Directives[Const]? UnionMemberTypes?
|
|
1949
|
+
*/
|
|
1950
|
+
parseUnionTypeDefinition() {
|
|
1951
|
+
const start = this._lexer.token;
|
|
1952
|
+
const description = this.parseDescription();
|
|
1953
|
+
this.expectKeyword("union");
|
|
1954
|
+
const name = this.parseName();
|
|
1955
|
+
const directives = this.parseConstDirectives();
|
|
1956
|
+
const types = this.parseUnionMemberTypes();
|
|
1957
|
+
return this.node(start, {
|
|
1958
|
+
kind: Kind.UNION_TYPE_DEFINITION,
|
|
1959
|
+
description,
|
|
1960
|
+
name,
|
|
1961
|
+
directives,
|
|
1962
|
+
types
|
|
1963
|
+
});
|
|
1964
|
+
}
|
|
1965
|
+
/**
|
|
1966
|
+
* UnionMemberTypes :
|
|
1967
|
+
* - = `|`? NamedType
|
|
1968
|
+
* - UnionMemberTypes | NamedType
|
|
1969
|
+
*/
|
|
1970
|
+
parseUnionMemberTypes() {
|
|
1971
|
+
return this.expectOptionalToken(TokenKind.EQUALS) ? this.delimitedMany(TokenKind.PIPE, this.parseNamedType) : [];
|
|
1972
|
+
}
|
|
1973
|
+
/**
|
|
1974
|
+
* EnumTypeDefinition :
|
|
1975
|
+
* - Description? enum Name Directives[Const]? EnumValuesDefinition?
|
|
1976
|
+
*/
|
|
1977
|
+
parseEnumTypeDefinition() {
|
|
1978
|
+
const start = this._lexer.token;
|
|
1979
|
+
const description = this.parseDescription();
|
|
1980
|
+
this.expectKeyword("enum");
|
|
1981
|
+
const name = this.parseName();
|
|
1982
|
+
const directives = this.parseConstDirectives();
|
|
1983
|
+
const values = this.parseEnumValuesDefinition();
|
|
1984
|
+
return this.node(start, {
|
|
1985
|
+
kind: Kind.ENUM_TYPE_DEFINITION,
|
|
1986
|
+
description,
|
|
1987
|
+
name,
|
|
1988
|
+
directives,
|
|
1989
|
+
values
|
|
1990
|
+
});
|
|
1991
|
+
}
|
|
1992
|
+
/**
|
|
1993
|
+
* ```
|
|
1994
|
+
* EnumValuesDefinition : { EnumValueDefinition+ }
|
|
1995
|
+
* ```
|
|
1996
|
+
*/
|
|
1997
|
+
parseEnumValuesDefinition() {
|
|
1998
|
+
return this.optionalMany(
|
|
1999
|
+
TokenKind.BRACE_L,
|
|
2000
|
+
this.parseEnumValueDefinition,
|
|
2001
|
+
TokenKind.BRACE_R
|
|
2002
|
+
);
|
|
2003
|
+
}
|
|
2004
|
+
/**
|
|
2005
|
+
* EnumValueDefinition : Description? EnumValue Directives[Const]?
|
|
2006
|
+
*/
|
|
2007
|
+
parseEnumValueDefinition() {
|
|
2008
|
+
const start = this._lexer.token;
|
|
2009
|
+
const description = this.parseDescription();
|
|
2010
|
+
const name = this.parseEnumValueName();
|
|
2011
|
+
const directives = this.parseConstDirectives();
|
|
2012
|
+
return this.node(start, {
|
|
2013
|
+
kind: Kind.ENUM_VALUE_DEFINITION,
|
|
2014
|
+
description,
|
|
2015
|
+
name,
|
|
2016
|
+
directives
|
|
2017
|
+
});
|
|
2018
|
+
}
|
|
2019
|
+
/**
|
|
2020
|
+
* EnumValue : Name but not `true`, `false` or `null`
|
|
2021
|
+
*/
|
|
2022
|
+
parseEnumValueName() {
|
|
2023
|
+
if (this._lexer.token.value === "true" || this._lexer.token.value === "false" || this._lexer.token.value === "null") {
|
|
2024
|
+
throw syntaxError(
|
|
2025
|
+
this._lexer.source,
|
|
2026
|
+
this._lexer.token.start,
|
|
2027
|
+
`${getTokenDesc(
|
|
2028
|
+
this._lexer.token
|
|
2029
|
+
)} is reserved and cannot be used for an enum value.`
|
|
2030
|
+
);
|
|
2031
|
+
}
|
|
2032
|
+
return this.parseName();
|
|
2033
|
+
}
|
|
2034
|
+
/**
|
|
2035
|
+
* InputObjectTypeDefinition :
|
|
2036
|
+
* - Description? input Name Directives[Const]? InputFieldsDefinition?
|
|
2037
|
+
*/
|
|
2038
|
+
parseInputObjectTypeDefinition() {
|
|
2039
|
+
const start = this._lexer.token;
|
|
2040
|
+
const description = this.parseDescription();
|
|
2041
|
+
this.expectKeyword("input");
|
|
2042
|
+
const name = this.parseName();
|
|
2043
|
+
const directives = this.parseConstDirectives();
|
|
2044
|
+
const fields = this.parseInputFieldsDefinition();
|
|
2045
|
+
return this.node(start, {
|
|
2046
|
+
kind: Kind.INPUT_OBJECT_TYPE_DEFINITION,
|
|
2047
|
+
description,
|
|
2048
|
+
name,
|
|
2049
|
+
directives,
|
|
2050
|
+
fields
|
|
2051
|
+
});
|
|
2052
|
+
}
|
|
2053
|
+
/**
|
|
2054
|
+
* ```
|
|
2055
|
+
* InputFieldsDefinition : { InputValueDefinition+ }
|
|
2056
|
+
* ```
|
|
2057
|
+
*/
|
|
2058
|
+
parseInputFieldsDefinition() {
|
|
2059
|
+
return this.optionalMany(
|
|
2060
|
+
TokenKind.BRACE_L,
|
|
2061
|
+
this.parseInputValueDef,
|
|
2062
|
+
TokenKind.BRACE_R
|
|
2063
|
+
);
|
|
2064
|
+
}
|
|
2065
|
+
/**
|
|
2066
|
+
* TypeSystemExtension :
|
|
2067
|
+
* - SchemaExtension
|
|
2068
|
+
* - TypeExtension
|
|
2069
|
+
*
|
|
2070
|
+
* TypeExtension :
|
|
2071
|
+
* - ScalarTypeExtension
|
|
2072
|
+
* - ObjectTypeExtension
|
|
2073
|
+
* - InterfaceTypeExtension
|
|
2074
|
+
* - UnionTypeExtension
|
|
2075
|
+
* - EnumTypeExtension
|
|
2076
|
+
* - InputObjectTypeDefinition
|
|
2077
|
+
*/
|
|
2078
|
+
parseTypeSystemExtension() {
|
|
2079
|
+
const keywordToken = this._lexer.lookahead();
|
|
2080
|
+
if (keywordToken.kind === TokenKind.NAME) {
|
|
2081
|
+
switch (keywordToken.value) {
|
|
2082
|
+
case "schema":
|
|
2083
|
+
return this.parseSchemaExtension();
|
|
2084
|
+
case "scalar":
|
|
2085
|
+
return this.parseScalarTypeExtension();
|
|
2086
|
+
case "type":
|
|
2087
|
+
return this.parseObjectTypeExtension();
|
|
2088
|
+
case "interface":
|
|
2089
|
+
return this.parseInterfaceTypeExtension();
|
|
2090
|
+
case "union":
|
|
2091
|
+
return this.parseUnionTypeExtension();
|
|
2092
|
+
case "enum":
|
|
2093
|
+
return this.parseEnumTypeExtension();
|
|
2094
|
+
case "input":
|
|
2095
|
+
return this.parseInputObjectTypeExtension();
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
throw this.unexpected(keywordToken);
|
|
2099
|
+
}
|
|
2100
|
+
/**
|
|
2101
|
+
* ```
|
|
2102
|
+
* SchemaExtension :
|
|
2103
|
+
* - extend schema Directives[Const]? { OperationTypeDefinition+ }
|
|
2104
|
+
* - extend schema Directives[Const]
|
|
2105
|
+
* ```
|
|
2106
|
+
*/
|
|
2107
|
+
parseSchemaExtension() {
|
|
2108
|
+
const start = this._lexer.token;
|
|
2109
|
+
this.expectKeyword("extend");
|
|
2110
|
+
this.expectKeyword("schema");
|
|
2111
|
+
const directives = this.parseConstDirectives();
|
|
2112
|
+
const operationTypes = this.optionalMany(
|
|
2113
|
+
TokenKind.BRACE_L,
|
|
2114
|
+
this.parseOperationTypeDefinition,
|
|
2115
|
+
TokenKind.BRACE_R
|
|
2116
|
+
);
|
|
2117
|
+
if (directives.length === 0 && operationTypes.length === 0) {
|
|
2118
|
+
throw this.unexpected();
|
|
2119
|
+
}
|
|
2120
|
+
return this.node(start, {
|
|
2121
|
+
kind: Kind.SCHEMA_EXTENSION,
|
|
2122
|
+
directives,
|
|
2123
|
+
operationTypes
|
|
2124
|
+
});
|
|
2125
|
+
}
|
|
2126
|
+
/**
|
|
2127
|
+
* ScalarTypeExtension :
|
|
2128
|
+
* - extend scalar Name Directives[Const]
|
|
2129
|
+
*/
|
|
2130
|
+
parseScalarTypeExtension() {
|
|
2131
|
+
const start = this._lexer.token;
|
|
2132
|
+
this.expectKeyword("extend");
|
|
2133
|
+
this.expectKeyword("scalar");
|
|
2134
|
+
const name = this.parseName();
|
|
2135
|
+
const directives = this.parseConstDirectives();
|
|
2136
|
+
if (directives.length === 0) {
|
|
2137
|
+
throw this.unexpected();
|
|
2138
|
+
}
|
|
2139
|
+
return this.node(start, {
|
|
2140
|
+
kind: Kind.SCALAR_TYPE_EXTENSION,
|
|
2141
|
+
name,
|
|
2142
|
+
directives
|
|
2143
|
+
});
|
|
2144
|
+
}
|
|
2145
|
+
/**
|
|
2146
|
+
* ObjectTypeExtension :
|
|
2147
|
+
* - extend type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
|
|
2148
|
+
* - extend type Name ImplementsInterfaces? Directives[Const]
|
|
2149
|
+
* - extend type Name ImplementsInterfaces
|
|
2150
|
+
*/
|
|
2151
|
+
parseObjectTypeExtension() {
|
|
2152
|
+
const start = this._lexer.token;
|
|
2153
|
+
this.expectKeyword("extend");
|
|
2154
|
+
this.expectKeyword("type");
|
|
2155
|
+
const name = this.parseName();
|
|
2156
|
+
const interfaces = this.parseImplementsInterfaces();
|
|
2157
|
+
const directives = this.parseConstDirectives();
|
|
2158
|
+
const fields = this.parseFieldsDefinition();
|
|
2159
|
+
if (interfaces.length === 0 && directives.length === 0 && fields.length === 0) {
|
|
2160
|
+
throw this.unexpected();
|
|
2161
|
+
}
|
|
2162
|
+
return this.node(start, {
|
|
2163
|
+
kind: Kind.OBJECT_TYPE_EXTENSION,
|
|
2164
|
+
name,
|
|
2165
|
+
interfaces,
|
|
2166
|
+
directives,
|
|
2167
|
+
fields
|
|
2168
|
+
});
|
|
2169
|
+
}
|
|
2170
|
+
/**
|
|
2171
|
+
* InterfaceTypeExtension :
|
|
2172
|
+
* - extend interface Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
|
|
2173
|
+
* - extend interface Name ImplementsInterfaces? Directives[Const]
|
|
2174
|
+
* - extend interface Name ImplementsInterfaces
|
|
2175
|
+
*/
|
|
2176
|
+
parseInterfaceTypeExtension() {
|
|
2177
|
+
const start = this._lexer.token;
|
|
2178
|
+
this.expectKeyword("extend");
|
|
2179
|
+
this.expectKeyword("interface");
|
|
2180
|
+
const name = this.parseName();
|
|
2181
|
+
const interfaces = this.parseImplementsInterfaces();
|
|
2182
|
+
const directives = this.parseConstDirectives();
|
|
2183
|
+
const fields = this.parseFieldsDefinition();
|
|
2184
|
+
if (interfaces.length === 0 && directives.length === 0 && fields.length === 0) {
|
|
2185
|
+
throw this.unexpected();
|
|
2186
|
+
}
|
|
2187
|
+
return this.node(start, {
|
|
2188
|
+
kind: Kind.INTERFACE_TYPE_EXTENSION,
|
|
2189
|
+
name,
|
|
2190
|
+
interfaces,
|
|
2191
|
+
directives,
|
|
2192
|
+
fields
|
|
2193
|
+
});
|
|
2194
|
+
}
|
|
2195
|
+
/**
|
|
2196
|
+
* UnionTypeExtension :
|
|
2197
|
+
* - extend union Name Directives[Const]? UnionMemberTypes
|
|
2198
|
+
* - extend union Name Directives[Const]
|
|
2199
|
+
*/
|
|
2200
|
+
parseUnionTypeExtension() {
|
|
2201
|
+
const start = this._lexer.token;
|
|
2202
|
+
this.expectKeyword("extend");
|
|
2203
|
+
this.expectKeyword("union");
|
|
2204
|
+
const name = this.parseName();
|
|
2205
|
+
const directives = this.parseConstDirectives();
|
|
2206
|
+
const types = this.parseUnionMemberTypes();
|
|
2207
|
+
if (directives.length === 0 && types.length === 0) {
|
|
2208
|
+
throw this.unexpected();
|
|
2209
|
+
}
|
|
2210
|
+
return this.node(start, {
|
|
2211
|
+
kind: Kind.UNION_TYPE_EXTENSION,
|
|
2212
|
+
name,
|
|
2213
|
+
directives,
|
|
2214
|
+
types
|
|
2215
|
+
});
|
|
2216
|
+
}
|
|
2217
|
+
/**
|
|
2218
|
+
* EnumTypeExtension :
|
|
2219
|
+
* - extend enum Name Directives[Const]? EnumValuesDefinition
|
|
2220
|
+
* - extend enum Name Directives[Const]
|
|
2221
|
+
*/
|
|
2222
|
+
parseEnumTypeExtension() {
|
|
2223
|
+
const start = this._lexer.token;
|
|
2224
|
+
this.expectKeyword("extend");
|
|
2225
|
+
this.expectKeyword("enum");
|
|
2226
|
+
const name = this.parseName();
|
|
2227
|
+
const directives = this.parseConstDirectives();
|
|
2228
|
+
const values = this.parseEnumValuesDefinition();
|
|
2229
|
+
if (directives.length === 0 && values.length === 0) {
|
|
2230
|
+
throw this.unexpected();
|
|
2231
|
+
}
|
|
2232
|
+
return this.node(start, {
|
|
2233
|
+
kind: Kind.ENUM_TYPE_EXTENSION,
|
|
2234
|
+
name,
|
|
2235
|
+
directives,
|
|
2236
|
+
values
|
|
2237
|
+
});
|
|
2238
|
+
}
|
|
2239
|
+
/**
|
|
2240
|
+
* InputObjectTypeExtension :
|
|
2241
|
+
* - extend input Name Directives[Const]? InputFieldsDefinition
|
|
2242
|
+
* - extend input Name Directives[Const]
|
|
2243
|
+
*/
|
|
2244
|
+
parseInputObjectTypeExtension() {
|
|
2245
|
+
const start = this._lexer.token;
|
|
2246
|
+
this.expectKeyword("extend");
|
|
2247
|
+
this.expectKeyword("input");
|
|
2248
|
+
const name = this.parseName();
|
|
2249
|
+
const directives = this.parseConstDirectives();
|
|
2250
|
+
const fields = this.parseInputFieldsDefinition();
|
|
2251
|
+
if (directives.length === 0 && fields.length === 0) {
|
|
2252
|
+
throw this.unexpected();
|
|
2253
|
+
}
|
|
2254
|
+
return this.node(start, {
|
|
2255
|
+
kind: Kind.INPUT_OBJECT_TYPE_EXTENSION,
|
|
2256
|
+
name,
|
|
2257
|
+
directives,
|
|
2258
|
+
fields
|
|
2259
|
+
});
|
|
2260
|
+
}
|
|
2261
|
+
/**
|
|
2262
|
+
* ```
|
|
2263
|
+
* DirectiveDefinition :
|
|
2264
|
+
* - Description? directive @ Name ArgumentsDefinition? `repeatable`? on DirectiveLocations
|
|
2265
|
+
* ```
|
|
2266
|
+
*/
|
|
2267
|
+
parseDirectiveDefinition() {
|
|
2268
|
+
const start = this._lexer.token;
|
|
2269
|
+
const description = this.parseDescription();
|
|
2270
|
+
this.expectKeyword("directive");
|
|
2271
|
+
this.expectToken(TokenKind.AT);
|
|
2272
|
+
const name = this.parseName();
|
|
2273
|
+
const args = this.parseArgumentDefs();
|
|
2274
|
+
const repeatable = this.expectOptionalKeyword("repeatable");
|
|
2275
|
+
this.expectKeyword("on");
|
|
2276
|
+
const locations = this.parseDirectiveLocations();
|
|
2277
|
+
return this.node(start, {
|
|
2278
|
+
kind: Kind.DIRECTIVE_DEFINITION,
|
|
2279
|
+
description,
|
|
2280
|
+
name,
|
|
2281
|
+
arguments: args,
|
|
2282
|
+
repeatable,
|
|
2283
|
+
locations
|
|
2284
|
+
});
|
|
2285
|
+
}
|
|
2286
|
+
/**
|
|
2287
|
+
* DirectiveLocations :
|
|
2288
|
+
* - `|`? DirectiveLocation
|
|
2289
|
+
* - DirectiveLocations | DirectiveLocation
|
|
2290
|
+
*/
|
|
2291
|
+
parseDirectiveLocations() {
|
|
2292
|
+
return this.delimitedMany(TokenKind.PIPE, this.parseDirectiveLocation);
|
|
2293
|
+
}
|
|
2294
|
+
/*
|
|
2295
|
+
* DirectiveLocation :
|
|
2296
|
+
* - ExecutableDirectiveLocation
|
|
2297
|
+
* - TypeSystemDirectiveLocation
|
|
2298
|
+
*
|
|
2299
|
+
* ExecutableDirectiveLocation : one of
|
|
2300
|
+
* `QUERY`
|
|
2301
|
+
* `MUTATION`
|
|
2302
|
+
* `SUBSCRIPTION`
|
|
2303
|
+
* `FIELD`
|
|
2304
|
+
* `FRAGMENT_DEFINITION`
|
|
2305
|
+
* `FRAGMENT_SPREAD`
|
|
2306
|
+
* `INLINE_FRAGMENT`
|
|
2307
|
+
*
|
|
2308
|
+
* TypeSystemDirectiveLocation : one of
|
|
2309
|
+
* `SCHEMA`
|
|
2310
|
+
* `SCALAR`
|
|
2311
|
+
* `OBJECT`
|
|
2312
|
+
* `FIELD_DEFINITION`
|
|
2313
|
+
* `ARGUMENT_DEFINITION`
|
|
2314
|
+
* `INTERFACE`
|
|
2315
|
+
* `UNION`
|
|
2316
|
+
* `ENUM`
|
|
2317
|
+
* `ENUM_VALUE`
|
|
2318
|
+
* `INPUT_OBJECT`
|
|
2319
|
+
* `INPUT_FIELD_DEFINITION`
|
|
2320
|
+
*/
|
|
2321
|
+
parseDirectiveLocation() {
|
|
2322
|
+
const start = this._lexer.token;
|
|
2323
|
+
const name = this.parseName();
|
|
2324
|
+
if (Object.prototype.hasOwnProperty.call(DirectiveLocation, name.value)) {
|
|
2325
|
+
return name;
|
|
2326
|
+
}
|
|
2327
|
+
throw this.unexpected(start);
|
|
2328
|
+
}
|
|
2329
|
+
// Core parsing utility functions
|
|
2330
|
+
/**
|
|
2331
|
+
* Returns a node that, if configured to do so, sets a "loc" field as a
|
|
2332
|
+
* location object, used to identify the place in the source that created a
|
|
2333
|
+
* given parsed object.
|
|
2334
|
+
*/
|
|
2335
|
+
node(startToken, node) {
|
|
2336
|
+
if (this._options.noLocation !== true) {
|
|
2337
|
+
node.loc = new Location(
|
|
2338
|
+
startToken,
|
|
2339
|
+
this._lexer.lastToken,
|
|
2340
|
+
this._lexer.source
|
|
2341
|
+
);
|
|
2342
|
+
}
|
|
2343
|
+
return node;
|
|
2344
|
+
}
|
|
2345
|
+
/**
|
|
2346
|
+
* Determines if the next token is of a given kind
|
|
2347
|
+
*/
|
|
2348
|
+
peek(kind) {
|
|
2349
|
+
return this._lexer.token.kind === kind;
|
|
2350
|
+
}
|
|
2351
|
+
/**
|
|
2352
|
+
* If the next token is of the given kind, return that token after advancing the lexer.
|
|
2353
|
+
* Otherwise, do not change the parser state and throw an error.
|
|
2354
|
+
*/
|
|
2355
|
+
expectToken(kind) {
|
|
2356
|
+
const token = this._lexer.token;
|
|
2357
|
+
if (token.kind === kind) {
|
|
2358
|
+
this.advanceLexer();
|
|
2359
|
+
return token;
|
|
2360
|
+
}
|
|
2361
|
+
throw syntaxError(
|
|
2362
|
+
this._lexer.source,
|
|
2363
|
+
token.start,
|
|
2364
|
+
`Expected ${getTokenKindDesc(kind)}, found ${getTokenDesc(token)}.`
|
|
2365
|
+
);
|
|
2366
|
+
}
|
|
2367
|
+
/**
|
|
2368
|
+
* If the next token is of the given kind, return "true" after advancing the lexer.
|
|
2369
|
+
* Otherwise, do not change the parser state and return "false".
|
|
2370
|
+
*/
|
|
2371
|
+
expectOptionalToken(kind) {
|
|
2372
|
+
const token = this._lexer.token;
|
|
2373
|
+
if (token.kind === kind) {
|
|
2374
|
+
this.advanceLexer();
|
|
2375
|
+
return true;
|
|
2376
|
+
}
|
|
2377
|
+
return false;
|
|
2378
|
+
}
|
|
2379
|
+
/**
|
|
2380
|
+
* If the next token is a given keyword, advance the lexer.
|
|
2381
|
+
* Otherwise, do not change the parser state and throw an error.
|
|
2382
|
+
*/
|
|
2383
|
+
expectKeyword(value) {
|
|
2384
|
+
const token = this._lexer.token;
|
|
2385
|
+
if (token.kind === TokenKind.NAME && token.value === value) {
|
|
2386
|
+
this.advanceLexer();
|
|
2387
|
+
} else {
|
|
2388
|
+
throw syntaxError(
|
|
2389
|
+
this._lexer.source,
|
|
2390
|
+
token.start,
|
|
2391
|
+
`Expected "${value}", found ${getTokenDesc(token)}.`
|
|
2392
|
+
);
|
|
2393
|
+
}
|
|
2394
|
+
}
|
|
2395
|
+
/**
|
|
2396
|
+
* If the next token is a given keyword, return "true" after advancing the lexer.
|
|
2397
|
+
* Otherwise, do not change the parser state and return "false".
|
|
2398
|
+
*/
|
|
2399
|
+
expectOptionalKeyword(value) {
|
|
2400
|
+
const token = this._lexer.token;
|
|
2401
|
+
if (token.kind === TokenKind.NAME && token.value === value) {
|
|
2402
|
+
this.advanceLexer();
|
|
2403
|
+
return true;
|
|
2404
|
+
}
|
|
2405
|
+
return false;
|
|
2406
|
+
}
|
|
2407
|
+
/**
|
|
2408
|
+
* Helper function for creating an error when an unexpected lexed token is encountered.
|
|
2409
|
+
*/
|
|
2410
|
+
unexpected(atToken) {
|
|
2411
|
+
const token = atToken !== null && atToken !== void 0 ? atToken : this._lexer.token;
|
|
2412
|
+
return syntaxError(
|
|
2413
|
+
this._lexer.source,
|
|
2414
|
+
token.start,
|
|
2415
|
+
`Unexpected ${getTokenDesc(token)}.`
|
|
2416
|
+
);
|
|
2417
|
+
}
|
|
2418
|
+
/**
|
|
2419
|
+
* Returns a possibly empty list of parse nodes, determined by the parseFn.
|
|
2420
|
+
* This list begins with a lex token of openKind and ends with a lex token of closeKind.
|
|
2421
|
+
* Advances the parser to the next lex token after the closing token.
|
|
2422
|
+
*/
|
|
2423
|
+
any(openKind, parseFn, closeKind) {
|
|
2424
|
+
this.expectToken(openKind);
|
|
2425
|
+
const nodes = [];
|
|
2426
|
+
while (!this.expectOptionalToken(closeKind)) {
|
|
2427
|
+
nodes.push(parseFn.call(this));
|
|
2428
|
+
}
|
|
2429
|
+
return nodes;
|
|
2430
|
+
}
|
|
2431
|
+
/**
|
|
2432
|
+
* Returns a list of parse nodes, determined by the parseFn.
|
|
2433
|
+
* It can be empty only if open token is missing otherwise it will always return non-empty list
|
|
2434
|
+
* that begins with a lex token of openKind and ends with a lex token of closeKind.
|
|
2435
|
+
* Advances the parser to the next lex token after the closing token.
|
|
2436
|
+
*/
|
|
2437
|
+
optionalMany(openKind, parseFn, closeKind) {
|
|
2438
|
+
if (this.expectOptionalToken(openKind)) {
|
|
2439
|
+
const nodes = [];
|
|
2440
|
+
do {
|
|
2441
|
+
nodes.push(parseFn.call(this));
|
|
2442
|
+
} while (!this.expectOptionalToken(closeKind));
|
|
2443
|
+
return nodes;
|
|
2444
|
+
}
|
|
2445
|
+
return [];
|
|
2446
|
+
}
|
|
2447
|
+
/**
|
|
2448
|
+
* Returns a non-empty list of parse nodes, determined by the parseFn.
|
|
2449
|
+
* This list begins with a lex token of openKind and ends with a lex token of closeKind.
|
|
2450
|
+
* Advances the parser to the next lex token after the closing token.
|
|
2451
|
+
*/
|
|
2452
|
+
many(openKind, parseFn, closeKind) {
|
|
2453
|
+
this.expectToken(openKind);
|
|
2454
|
+
const nodes = [];
|
|
2455
|
+
do {
|
|
2456
|
+
nodes.push(parseFn.call(this));
|
|
2457
|
+
} while (!this.expectOptionalToken(closeKind));
|
|
2458
|
+
return nodes;
|
|
2459
|
+
}
|
|
2460
|
+
/**
|
|
2461
|
+
* Returns a non-empty list of parse nodes, determined by the parseFn.
|
|
2462
|
+
* This list may begin with a lex token of delimiterKind followed by items separated by lex tokens of tokenKind.
|
|
2463
|
+
* Advances the parser to the next lex token after last item in the list.
|
|
2464
|
+
*/
|
|
2465
|
+
delimitedMany(delimiterKind, parseFn) {
|
|
2466
|
+
this.expectOptionalToken(delimiterKind);
|
|
2467
|
+
const nodes = [];
|
|
2468
|
+
do {
|
|
2469
|
+
nodes.push(parseFn.call(this));
|
|
2470
|
+
} while (this.expectOptionalToken(delimiterKind));
|
|
2471
|
+
return nodes;
|
|
2472
|
+
}
|
|
2473
|
+
advanceLexer() {
|
|
2474
|
+
const { maxTokens } = this._options;
|
|
2475
|
+
const token = this._lexer.advance();
|
|
2476
|
+
if (token.kind !== TokenKind.EOF) {
|
|
2477
|
+
++this._tokenCounter;
|
|
2478
|
+
if (maxTokens !== void 0 && this._tokenCounter > maxTokens) {
|
|
2479
|
+
throw syntaxError(
|
|
2480
|
+
this._lexer.source,
|
|
2481
|
+
token.start,
|
|
2482
|
+
`Document contains more that ${maxTokens} tokens. Parsing aborted.`
|
|
2483
|
+
);
|
|
2484
|
+
}
|
|
2485
|
+
}
|
|
2486
|
+
}
|
|
2487
|
+
}
|
|
2488
|
+
function getTokenDesc(token) {
|
|
2489
|
+
const value = token.value;
|
|
2490
|
+
return getTokenKindDesc(token.kind) + (value != null ? ` "${value}"` : "");
|
|
2491
|
+
}
|
|
2492
|
+
function getTokenKindDesc(kind) {
|
|
2493
|
+
return isPunctuatorTokenKind(kind) ? `"${kind}"` : kind;
|
|
2494
|
+
}
|
|
2495
|
+
function printString(str) {
|
|
2496
|
+
return `"${str.replace(escapedRegExp, escapedReplacer)}"`;
|
|
2497
|
+
}
|
|
2498
|
+
const escapedRegExp = /[\x00-\x1f\x22\x5c\x7f-\x9f]/g;
|
|
2499
|
+
function escapedReplacer(str) {
|
|
2500
|
+
return escapeSequences[str.charCodeAt(0)];
|
|
2501
|
+
}
|
|
2502
|
+
const escapeSequences = [
|
|
2503
|
+
"\\u0000",
|
|
2504
|
+
"\\u0001",
|
|
2505
|
+
"\\u0002",
|
|
2506
|
+
"\\u0003",
|
|
2507
|
+
"\\u0004",
|
|
2508
|
+
"\\u0005",
|
|
2509
|
+
"\\u0006",
|
|
2510
|
+
"\\u0007",
|
|
2511
|
+
"\\b",
|
|
2512
|
+
"\\t",
|
|
2513
|
+
"\\n",
|
|
2514
|
+
"\\u000B",
|
|
2515
|
+
"\\f",
|
|
2516
|
+
"\\r",
|
|
2517
|
+
"\\u000E",
|
|
2518
|
+
"\\u000F",
|
|
2519
|
+
"\\u0010",
|
|
2520
|
+
"\\u0011",
|
|
2521
|
+
"\\u0012",
|
|
2522
|
+
"\\u0013",
|
|
2523
|
+
"\\u0014",
|
|
2524
|
+
"\\u0015",
|
|
2525
|
+
"\\u0016",
|
|
2526
|
+
"\\u0017",
|
|
2527
|
+
"\\u0018",
|
|
2528
|
+
"\\u0019",
|
|
2529
|
+
"\\u001A",
|
|
2530
|
+
"\\u001B",
|
|
2531
|
+
"\\u001C",
|
|
2532
|
+
"\\u001D",
|
|
2533
|
+
"\\u001E",
|
|
2534
|
+
"\\u001F",
|
|
2535
|
+
"",
|
|
2536
|
+
"",
|
|
2537
|
+
'\\"',
|
|
2538
|
+
"",
|
|
2539
|
+
"",
|
|
2540
|
+
"",
|
|
2541
|
+
"",
|
|
2542
|
+
"",
|
|
2543
|
+
"",
|
|
2544
|
+
"",
|
|
2545
|
+
"",
|
|
2546
|
+
"",
|
|
2547
|
+
"",
|
|
2548
|
+
"",
|
|
2549
|
+
"",
|
|
2550
|
+
"",
|
|
2551
|
+
// 2F
|
|
2552
|
+
"",
|
|
2553
|
+
"",
|
|
2554
|
+
"",
|
|
2555
|
+
"",
|
|
2556
|
+
"",
|
|
2557
|
+
"",
|
|
2558
|
+
"",
|
|
2559
|
+
"",
|
|
2560
|
+
"",
|
|
2561
|
+
"",
|
|
2562
|
+
"",
|
|
2563
|
+
"",
|
|
2564
|
+
"",
|
|
2565
|
+
"",
|
|
2566
|
+
"",
|
|
2567
|
+
"",
|
|
2568
|
+
// 3F
|
|
2569
|
+
"",
|
|
2570
|
+
"",
|
|
2571
|
+
"",
|
|
2572
|
+
"",
|
|
2573
|
+
"",
|
|
2574
|
+
"",
|
|
2575
|
+
"",
|
|
2576
|
+
"",
|
|
2577
|
+
"",
|
|
2578
|
+
"",
|
|
2579
|
+
"",
|
|
2580
|
+
"",
|
|
2581
|
+
"",
|
|
2582
|
+
"",
|
|
2583
|
+
"",
|
|
2584
|
+
"",
|
|
2585
|
+
// 4F
|
|
2586
|
+
"",
|
|
2587
|
+
"",
|
|
2588
|
+
"",
|
|
2589
|
+
"",
|
|
2590
|
+
"",
|
|
2591
|
+
"",
|
|
2592
|
+
"",
|
|
2593
|
+
"",
|
|
2594
|
+
"",
|
|
2595
|
+
"",
|
|
2596
|
+
"",
|
|
2597
|
+
"",
|
|
2598
|
+
"\\\\",
|
|
2599
|
+
"",
|
|
2600
|
+
"",
|
|
2601
|
+
"",
|
|
2602
|
+
// 5F
|
|
2603
|
+
"",
|
|
2604
|
+
"",
|
|
2605
|
+
"",
|
|
2606
|
+
"",
|
|
2607
|
+
"",
|
|
2608
|
+
"",
|
|
2609
|
+
"",
|
|
2610
|
+
"",
|
|
2611
|
+
"",
|
|
2612
|
+
"",
|
|
2613
|
+
"",
|
|
2614
|
+
"",
|
|
2615
|
+
"",
|
|
2616
|
+
"",
|
|
2617
|
+
"",
|
|
2618
|
+
"",
|
|
2619
|
+
// 6F
|
|
2620
|
+
"",
|
|
2621
|
+
"",
|
|
2622
|
+
"",
|
|
2623
|
+
"",
|
|
2624
|
+
"",
|
|
2625
|
+
"",
|
|
2626
|
+
"",
|
|
2627
|
+
"",
|
|
2628
|
+
"",
|
|
2629
|
+
"",
|
|
2630
|
+
"",
|
|
2631
|
+
"",
|
|
2632
|
+
"",
|
|
2633
|
+
"",
|
|
2634
|
+
"",
|
|
2635
|
+
"\\u007F",
|
|
2636
|
+
"\\u0080",
|
|
2637
|
+
"\\u0081",
|
|
2638
|
+
"\\u0082",
|
|
2639
|
+
"\\u0083",
|
|
2640
|
+
"\\u0084",
|
|
2641
|
+
"\\u0085",
|
|
2642
|
+
"\\u0086",
|
|
2643
|
+
"\\u0087",
|
|
2644
|
+
"\\u0088",
|
|
2645
|
+
"\\u0089",
|
|
2646
|
+
"\\u008A",
|
|
2647
|
+
"\\u008B",
|
|
2648
|
+
"\\u008C",
|
|
2649
|
+
"\\u008D",
|
|
2650
|
+
"\\u008E",
|
|
2651
|
+
"\\u008F",
|
|
2652
|
+
"\\u0090",
|
|
2653
|
+
"\\u0091",
|
|
2654
|
+
"\\u0092",
|
|
2655
|
+
"\\u0093",
|
|
2656
|
+
"\\u0094",
|
|
2657
|
+
"\\u0095",
|
|
2658
|
+
"\\u0096",
|
|
2659
|
+
"\\u0097",
|
|
2660
|
+
"\\u0098",
|
|
2661
|
+
"\\u0099",
|
|
2662
|
+
"\\u009A",
|
|
2663
|
+
"\\u009B",
|
|
2664
|
+
"\\u009C",
|
|
2665
|
+
"\\u009D",
|
|
2666
|
+
"\\u009E",
|
|
2667
|
+
"\\u009F"
|
|
2668
|
+
];
|
|
2669
|
+
const BREAK = Object.freeze({});
|
|
2670
|
+
function visit(root, visitor, visitorKeys = QueryDocumentKeys) {
|
|
2671
|
+
const enterLeaveMap = /* @__PURE__ */ new Map();
|
|
2672
|
+
for (const kind of Object.values(Kind)) {
|
|
2673
|
+
enterLeaveMap.set(kind, getEnterLeaveForKind(visitor, kind));
|
|
2674
|
+
}
|
|
2675
|
+
let stack = void 0;
|
|
2676
|
+
let inArray = Array.isArray(root);
|
|
2677
|
+
let keys = [root];
|
|
2678
|
+
let index = -1;
|
|
2679
|
+
let edits = [];
|
|
2680
|
+
let node = root;
|
|
2681
|
+
let key = void 0;
|
|
2682
|
+
let parent = void 0;
|
|
2683
|
+
const path = [];
|
|
2684
|
+
const ancestors = [];
|
|
2685
|
+
do {
|
|
2686
|
+
index++;
|
|
2687
|
+
const isLeaving = index === keys.length;
|
|
2688
|
+
const isEdited = isLeaving && edits.length !== 0;
|
|
2689
|
+
if (isLeaving) {
|
|
2690
|
+
key = ancestors.length === 0 ? void 0 : path[path.length - 1];
|
|
2691
|
+
node = parent;
|
|
2692
|
+
parent = ancestors.pop();
|
|
2693
|
+
if (isEdited) {
|
|
2694
|
+
if (inArray) {
|
|
2695
|
+
node = node.slice();
|
|
2696
|
+
let editOffset = 0;
|
|
2697
|
+
for (const [editKey, editValue] of edits) {
|
|
2698
|
+
const arrayKey = editKey - editOffset;
|
|
2699
|
+
if (editValue === null) {
|
|
2700
|
+
node.splice(arrayKey, 1);
|
|
2701
|
+
editOffset++;
|
|
2702
|
+
} else {
|
|
2703
|
+
node[arrayKey] = editValue;
|
|
2704
|
+
}
|
|
2705
|
+
}
|
|
2706
|
+
} else {
|
|
2707
|
+
node = { ...node };
|
|
2708
|
+
for (const [editKey, editValue] of edits) {
|
|
2709
|
+
node[editKey] = editValue;
|
|
2710
|
+
}
|
|
2711
|
+
}
|
|
2712
|
+
}
|
|
2713
|
+
index = stack.index;
|
|
2714
|
+
keys = stack.keys;
|
|
2715
|
+
edits = stack.edits;
|
|
2716
|
+
inArray = stack.inArray;
|
|
2717
|
+
stack = stack.prev;
|
|
2718
|
+
} else if (parent) {
|
|
2719
|
+
key = inArray ? index : keys[index];
|
|
2720
|
+
node = parent[key];
|
|
2721
|
+
if (node === null || node === void 0) {
|
|
2722
|
+
continue;
|
|
2723
|
+
}
|
|
2724
|
+
path.push(key);
|
|
2725
|
+
}
|
|
2726
|
+
let result;
|
|
2727
|
+
if (!Array.isArray(node)) {
|
|
2728
|
+
var _enterLeaveMap$get, _enterLeaveMap$get2;
|
|
2729
|
+
isNode(node) || devAssert(false, `Invalid AST Node: ${inspect(node)}.`);
|
|
2730
|
+
const visitFn = isLeaving ? (_enterLeaveMap$get = enterLeaveMap.get(node.kind)) === null || _enterLeaveMap$get === void 0 ? void 0 : _enterLeaveMap$get.leave : (_enterLeaveMap$get2 = enterLeaveMap.get(node.kind)) === null || _enterLeaveMap$get2 === void 0 ? void 0 : _enterLeaveMap$get2.enter;
|
|
2731
|
+
result = visitFn === null || visitFn === void 0 ? void 0 : visitFn.call(visitor, node, key, parent, path, ancestors);
|
|
2732
|
+
if (result === BREAK) {
|
|
2733
|
+
break;
|
|
2734
|
+
}
|
|
2735
|
+
if (result === false) {
|
|
2736
|
+
if (!isLeaving) {
|
|
2737
|
+
path.pop();
|
|
2738
|
+
continue;
|
|
2739
|
+
}
|
|
2740
|
+
} else if (result !== void 0) {
|
|
2741
|
+
edits.push([key, result]);
|
|
2742
|
+
if (!isLeaving) {
|
|
2743
|
+
if (isNode(result)) {
|
|
2744
|
+
node = result;
|
|
2745
|
+
} else {
|
|
2746
|
+
path.pop();
|
|
2747
|
+
continue;
|
|
2748
|
+
}
|
|
2749
|
+
}
|
|
2750
|
+
}
|
|
2751
|
+
}
|
|
2752
|
+
if (result === void 0 && isEdited) {
|
|
2753
|
+
edits.push([key, node]);
|
|
2754
|
+
}
|
|
2755
|
+
if (isLeaving) {
|
|
2756
|
+
path.pop();
|
|
2757
|
+
} else {
|
|
2758
|
+
var _node$kind;
|
|
2759
|
+
stack = {
|
|
2760
|
+
inArray,
|
|
2761
|
+
index,
|
|
2762
|
+
keys,
|
|
2763
|
+
edits,
|
|
2764
|
+
prev: stack
|
|
2765
|
+
};
|
|
2766
|
+
inArray = Array.isArray(node);
|
|
2767
|
+
keys = inArray ? node : (_node$kind = visitorKeys[node.kind]) !== null && _node$kind !== void 0 ? _node$kind : [];
|
|
2768
|
+
index = -1;
|
|
2769
|
+
edits = [];
|
|
2770
|
+
if (parent) {
|
|
2771
|
+
ancestors.push(parent);
|
|
2772
|
+
}
|
|
2773
|
+
parent = node;
|
|
2774
|
+
}
|
|
2775
|
+
} while (stack !== void 0);
|
|
2776
|
+
if (edits.length !== 0) {
|
|
2777
|
+
return edits[edits.length - 1][1];
|
|
2778
|
+
}
|
|
2779
|
+
return root;
|
|
2780
|
+
}
|
|
2781
|
+
function getEnterLeaveForKind(visitor, kind) {
|
|
2782
|
+
const kindVisitor = visitor[kind];
|
|
2783
|
+
if (typeof kindVisitor === "object") {
|
|
2784
|
+
return kindVisitor;
|
|
2785
|
+
} else if (typeof kindVisitor === "function") {
|
|
2786
|
+
return {
|
|
2787
|
+
enter: kindVisitor,
|
|
2788
|
+
leave: void 0
|
|
2789
|
+
};
|
|
2790
|
+
}
|
|
2791
|
+
return {
|
|
2792
|
+
enter: visitor.enter,
|
|
2793
|
+
leave: visitor.leave
|
|
2794
|
+
};
|
|
2795
|
+
}
|
|
2796
|
+
function print(ast) {
|
|
2797
|
+
return visit(ast, printDocASTReducer);
|
|
2798
|
+
}
|
|
2799
|
+
const MAX_LINE_LENGTH = 80;
|
|
2800
|
+
const printDocASTReducer = {
|
|
2801
|
+
Name: {
|
|
2802
|
+
leave: (node) => node.value
|
|
2803
|
+
},
|
|
2804
|
+
Variable: {
|
|
2805
|
+
leave: (node) => "$" + node.name
|
|
2806
|
+
},
|
|
2807
|
+
// Document
|
|
2808
|
+
Document: {
|
|
2809
|
+
leave: (node) => join(node.definitions, "\n\n")
|
|
2810
|
+
},
|
|
2811
|
+
OperationDefinition: {
|
|
2812
|
+
leave(node) {
|
|
2813
|
+
const varDefs = wrap("(", join(node.variableDefinitions, ", "), ")");
|
|
2814
|
+
const prefix = join(
|
|
2815
|
+
[
|
|
2816
|
+
node.operation,
|
|
2817
|
+
join([node.name, varDefs]),
|
|
2818
|
+
join(node.directives, " ")
|
|
2819
|
+
],
|
|
2820
|
+
" "
|
|
2821
|
+
);
|
|
2822
|
+
return (prefix === "query" ? "" : prefix + " ") + node.selectionSet;
|
|
2823
|
+
}
|
|
2824
|
+
},
|
|
2825
|
+
VariableDefinition: {
|
|
2826
|
+
leave: ({ variable, type, defaultValue, directives }) => variable + ": " + type + wrap(" = ", defaultValue) + wrap(" ", join(directives, " "))
|
|
2827
|
+
},
|
|
2828
|
+
SelectionSet: {
|
|
2829
|
+
leave: ({ selections }) => block(selections)
|
|
2830
|
+
},
|
|
2831
|
+
Field: {
|
|
2832
|
+
leave({ alias, name, arguments: args, directives, selectionSet }) {
|
|
2833
|
+
const prefix = wrap("", alias, ": ") + name;
|
|
2834
|
+
let argsLine = prefix + wrap("(", join(args, ", "), ")");
|
|
2835
|
+
if (argsLine.length > MAX_LINE_LENGTH) {
|
|
2836
|
+
argsLine = prefix + wrap("(\n", indent(join(args, "\n")), "\n)");
|
|
2837
|
+
}
|
|
2838
|
+
return join([argsLine, join(directives, " "), selectionSet], " ");
|
|
2839
|
+
}
|
|
2840
|
+
},
|
|
2841
|
+
Argument: {
|
|
2842
|
+
leave: ({ name, value }) => name + ": " + value
|
|
2843
|
+
},
|
|
2844
|
+
// Fragments
|
|
2845
|
+
FragmentSpread: {
|
|
2846
|
+
leave: ({ name, directives }) => "..." + name + wrap(" ", join(directives, " "))
|
|
2847
|
+
},
|
|
2848
|
+
InlineFragment: {
|
|
2849
|
+
leave: ({ typeCondition, directives, selectionSet }) => join(
|
|
2850
|
+
[
|
|
2851
|
+
"...",
|
|
2852
|
+
wrap("on ", typeCondition),
|
|
2853
|
+
join(directives, " "),
|
|
2854
|
+
selectionSet
|
|
2855
|
+
],
|
|
2856
|
+
" "
|
|
2857
|
+
)
|
|
2858
|
+
},
|
|
2859
|
+
FragmentDefinition: {
|
|
2860
|
+
leave: ({ name, typeCondition, variableDefinitions, directives, selectionSet }) => (
|
|
2861
|
+
// or removed in the future.
|
|
2862
|
+
`fragment ${name}${wrap("(", join(variableDefinitions, ", "), ")")} on ${typeCondition} ${wrap("", join(directives, " "), " ")}` + selectionSet
|
|
2863
|
+
)
|
|
2864
|
+
},
|
|
2865
|
+
// Value
|
|
2866
|
+
IntValue: {
|
|
2867
|
+
leave: ({ value }) => value
|
|
2868
|
+
},
|
|
2869
|
+
FloatValue: {
|
|
2870
|
+
leave: ({ value }) => value
|
|
2871
|
+
},
|
|
2872
|
+
StringValue: {
|
|
2873
|
+
leave: ({ value, block: isBlockString }) => isBlockString ? printBlockString(value) : printString(value)
|
|
2874
|
+
},
|
|
2875
|
+
BooleanValue: {
|
|
2876
|
+
leave: ({ value }) => value ? "true" : "false"
|
|
2877
|
+
},
|
|
2878
|
+
NullValue: {
|
|
2879
|
+
leave: () => "null"
|
|
2880
|
+
},
|
|
2881
|
+
EnumValue: {
|
|
2882
|
+
leave: ({ value }) => value
|
|
2883
|
+
},
|
|
2884
|
+
ListValue: {
|
|
2885
|
+
leave: ({ values }) => "[" + join(values, ", ") + "]"
|
|
2886
|
+
},
|
|
2887
|
+
ObjectValue: {
|
|
2888
|
+
leave: ({ fields }) => "{" + join(fields, ", ") + "}"
|
|
2889
|
+
},
|
|
2890
|
+
ObjectField: {
|
|
2891
|
+
leave: ({ name, value }) => name + ": " + value
|
|
2892
|
+
},
|
|
2893
|
+
// Directive
|
|
2894
|
+
Directive: {
|
|
2895
|
+
leave: ({ name, arguments: args }) => "@" + name + wrap("(", join(args, ", "), ")")
|
|
2896
|
+
},
|
|
2897
|
+
// Type
|
|
2898
|
+
NamedType: {
|
|
2899
|
+
leave: ({ name }) => name
|
|
2900
|
+
},
|
|
2901
|
+
ListType: {
|
|
2902
|
+
leave: ({ type }) => "[" + type + "]"
|
|
2903
|
+
},
|
|
2904
|
+
NonNullType: {
|
|
2905
|
+
leave: ({ type }) => type + "!"
|
|
2906
|
+
},
|
|
2907
|
+
// Type System Definitions
|
|
2908
|
+
SchemaDefinition: {
|
|
2909
|
+
leave: ({ description, directives, operationTypes }) => wrap("", description, "\n") + join(["schema", join(directives, " "), block(operationTypes)], " ")
|
|
2910
|
+
},
|
|
2911
|
+
OperationTypeDefinition: {
|
|
2912
|
+
leave: ({ operation, type }) => operation + ": " + type
|
|
2913
|
+
},
|
|
2914
|
+
ScalarTypeDefinition: {
|
|
2915
|
+
leave: ({ description, name, directives }) => wrap("", description, "\n") + join(["scalar", name, join(directives, " ")], " ")
|
|
2916
|
+
},
|
|
2917
|
+
ObjectTypeDefinition: {
|
|
2918
|
+
leave: ({ description, name, interfaces, directives, fields }) => wrap("", description, "\n") + join(
|
|
2919
|
+
[
|
|
2920
|
+
"type",
|
|
2921
|
+
name,
|
|
2922
|
+
wrap("implements ", join(interfaces, " & ")),
|
|
2923
|
+
join(directives, " "),
|
|
2924
|
+
block(fields)
|
|
2925
|
+
],
|
|
2926
|
+
" "
|
|
2927
|
+
)
|
|
2928
|
+
},
|
|
2929
|
+
FieldDefinition: {
|
|
2930
|
+
leave: ({ description, name, arguments: args, type, directives }) => wrap("", description, "\n") + name + (hasMultilineItems(args) ? wrap("(\n", indent(join(args, "\n")), "\n)") : wrap("(", join(args, ", "), ")")) + ": " + type + wrap(" ", join(directives, " "))
|
|
2931
|
+
},
|
|
2932
|
+
InputValueDefinition: {
|
|
2933
|
+
leave: ({ description, name, type, defaultValue, directives }) => wrap("", description, "\n") + join(
|
|
2934
|
+
[name + ": " + type, wrap("= ", defaultValue), join(directives, " ")],
|
|
2935
|
+
" "
|
|
2936
|
+
)
|
|
2937
|
+
},
|
|
2938
|
+
InterfaceTypeDefinition: {
|
|
2939
|
+
leave: ({ description, name, interfaces, directives, fields }) => wrap("", description, "\n") + join(
|
|
2940
|
+
[
|
|
2941
|
+
"interface",
|
|
2942
|
+
name,
|
|
2943
|
+
wrap("implements ", join(interfaces, " & ")),
|
|
2944
|
+
join(directives, " "),
|
|
2945
|
+
block(fields)
|
|
2946
|
+
],
|
|
2947
|
+
" "
|
|
2948
|
+
)
|
|
2949
|
+
},
|
|
2950
|
+
UnionTypeDefinition: {
|
|
2951
|
+
leave: ({ description, name, directives, types }) => wrap("", description, "\n") + join(
|
|
2952
|
+
["union", name, join(directives, " "), wrap("= ", join(types, " | "))],
|
|
2953
|
+
" "
|
|
2954
|
+
)
|
|
2955
|
+
},
|
|
2956
|
+
EnumTypeDefinition: {
|
|
2957
|
+
leave: ({ description, name, directives, values }) => wrap("", description, "\n") + join(["enum", name, join(directives, " "), block(values)], " ")
|
|
2958
|
+
},
|
|
2959
|
+
EnumValueDefinition: {
|
|
2960
|
+
leave: ({ description, name, directives }) => wrap("", description, "\n") + join([name, join(directives, " ")], " ")
|
|
2961
|
+
},
|
|
2962
|
+
InputObjectTypeDefinition: {
|
|
2963
|
+
leave: ({ description, name, directives, fields }) => wrap("", description, "\n") + join(["input", name, join(directives, " "), block(fields)], " ")
|
|
2964
|
+
},
|
|
2965
|
+
DirectiveDefinition: {
|
|
2966
|
+
leave: ({ description, name, arguments: args, repeatable, locations }) => wrap("", description, "\n") + "directive @" + name + (hasMultilineItems(args) ? wrap("(\n", indent(join(args, "\n")), "\n)") : wrap("(", join(args, ", "), ")")) + (repeatable ? " repeatable" : "") + " on " + join(locations, " | ")
|
|
2967
|
+
},
|
|
2968
|
+
SchemaExtension: {
|
|
2969
|
+
leave: ({ directives, operationTypes }) => join(
|
|
2970
|
+
["extend schema", join(directives, " "), block(operationTypes)],
|
|
2971
|
+
" "
|
|
2972
|
+
)
|
|
2973
|
+
},
|
|
2974
|
+
ScalarTypeExtension: {
|
|
2975
|
+
leave: ({ name, directives }) => join(["extend scalar", name, join(directives, " ")], " ")
|
|
2976
|
+
},
|
|
2977
|
+
ObjectTypeExtension: {
|
|
2978
|
+
leave: ({ name, interfaces, directives, fields }) => join(
|
|
2979
|
+
[
|
|
2980
|
+
"extend type",
|
|
2981
|
+
name,
|
|
2982
|
+
wrap("implements ", join(interfaces, " & ")),
|
|
2983
|
+
join(directives, " "),
|
|
2984
|
+
block(fields)
|
|
2985
|
+
],
|
|
2986
|
+
" "
|
|
2987
|
+
)
|
|
2988
|
+
},
|
|
2989
|
+
InterfaceTypeExtension: {
|
|
2990
|
+
leave: ({ name, interfaces, directives, fields }) => join(
|
|
2991
|
+
[
|
|
2992
|
+
"extend interface",
|
|
2993
|
+
name,
|
|
2994
|
+
wrap("implements ", join(interfaces, " & ")),
|
|
2995
|
+
join(directives, " "),
|
|
2996
|
+
block(fields)
|
|
2997
|
+
],
|
|
2998
|
+
" "
|
|
2999
|
+
)
|
|
3000
|
+
},
|
|
3001
|
+
UnionTypeExtension: {
|
|
3002
|
+
leave: ({ name, directives, types }) => join(
|
|
3003
|
+
[
|
|
3004
|
+
"extend union",
|
|
3005
|
+
name,
|
|
3006
|
+
join(directives, " "),
|
|
3007
|
+
wrap("= ", join(types, " | "))
|
|
3008
|
+
],
|
|
3009
|
+
" "
|
|
3010
|
+
)
|
|
3011
|
+
},
|
|
3012
|
+
EnumTypeExtension: {
|
|
3013
|
+
leave: ({ name, directives, values }) => join(["extend enum", name, join(directives, " "), block(values)], " ")
|
|
3014
|
+
},
|
|
3015
|
+
InputObjectTypeExtension: {
|
|
3016
|
+
leave: ({ name, directives, fields }) => join(["extend input", name, join(directives, " "), block(fields)], " ")
|
|
3017
|
+
}
|
|
3018
|
+
};
|
|
3019
|
+
function join(maybeArray, separator = "") {
|
|
3020
|
+
var _maybeArray$filter$jo;
|
|
3021
|
+
return (_maybeArray$filter$jo = maybeArray === null || maybeArray === void 0 ? void 0 : maybeArray.filter((x) => x).join(separator)) !== null && _maybeArray$filter$jo !== void 0 ? _maybeArray$filter$jo : "";
|
|
3022
|
+
}
|
|
3023
|
+
function block(array) {
|
|
3024
|
+
return wrap("{\n", indent(join(array, "\n")), "\n}");
|
|
3025
|
+
}
|
|
3026
|
+
function wrap(start, maybeString, end = "") {
|
|
3027
|
+
return maybeString != null && maybeString !== "" ? start + maybeString + end : "";
|
|
3028
|
+
}
|
|
3029
|
+
function indent(str) {
|
|
3030
|
+
return wrap(" ", str.replace(/\n/g, "\n "));
|
|
3031
|
+
}
|
|
3032
|
+
function hasMultilineItems(maybeArray) {
|
|
3033
|
+
var _maybeArray$some;
|
|
3034
|
+
return (_maybeArray$some = maybeArray === null || maybeArray === void 0 ? void 0 : maybeArray.some((str) => str.includes("\n"))) !== null && _maybeArray$some !== void 0 ? _maybeArray$some : false;
|
|
3035
|
+
}
|
|
3036
|
+
function stripIgnoredCharacters(source) {
|
|
3037
|
+
const sourceObj = isSource(source) ? source : new Source(source);
|
|
3038
|
+
const body = sourceObj.body;
|
|
3039
|
+
const lexer = new Lexer(sourceObj);
|
|
3040
|
+
let strippedBody = "";
|
|
3041
|
+
let wasLastAddedTokenNonPunctuator = false;
|
|
3042
|
+
while (lexer.advance().kind !== TokenKind.EOF) {
|
|
3043
|
+
const currentToken = lexer.token;
|
|
3044
|
+
const tokenKind = currentToken.kind;
|
|
3045
|
+
const isNonPunctuator = !isPunctuatorTokenKind(currentToken.kind);
|
|
3046
|
+
if (wasLastAddedTokenNonPunctuator) {
|
|
3047
|
+
if (isNonPunctuator || currentToken.kind === TokenKind.SPREAD) {
|
|
3048
|
+
strippedBody += " ";
|
|
3049
|
+
}
|
|
3050
|
+
}
|
|
3051
|
+
const tokenBody = body.slice(currentToken.start, currentToken.end);
|
|
3052
|
+
if (tokenKind === TokenKind.BLOCK_STRING) {
|
|
3053
|
+
strippedBody += printBlockString(currentToken.value, {
|
|
3054
|
+
minimize: true
|
|
3055
|
+
});
|
|
3056
|
+
} else {
|
|
3057
|
+
strippedBody += tokenBody;
|
|
3058
|
+
}
|
|
3059
|
+
wasLastAddedTokenNonPunctuator = isNonPunctuator;
|
|
3060
|
+
}
|
|
3061
|
+
return strippedBody;
|
|
3062
|
+
}
|
|
3063
|
+
const docMap = /* @__PURE__ */ new Map();
|
|
3064
|
+
const referenceMap = /* @__PURE__ */ new WeakMap();
|
|
3065
|
+
function operationKeyBuilder(inputString) {
|
|
3066
|
+
return stripIgnoredCharacters(inputString);
|
|
3067
|
+
}
|
|
3068
|
+
function parseDocument(inputString) {
|
|
3069
|
+
const operationKey = operationKeyBuilder(inputString);
|
|
3070
|
+
const cachedDoc = docMap.get(operationKey);
|
|
3071
|
+
if (cachedDoc !== void 0) {
|
|
3072
|
+
return cachedDoc;
|
|
3073
|
+
}
|
|
3074
|
+
const parsedDoc = parse(inputString, { noLocation: true });
|
|
3075
|
+
if (!parsedDoc || parsedDoc.kind !== "Document") {
|
|
3076
|
+
if (process.env.NODE_ENV !== "production") {
|
|
3077
|
+
throw new Error("Invalid graphql doc");
|
|
3078
|
+
}
|
|
3079
|
+
return null;
|
|
3080
|
+
}
|
|
3081
|
+
docMap.set(operationKey, parsedDoc);
|
|
3082
|
+
return parsedDoc;
|
|
3083
|
+
}
|
|
3084
|
+
function insertFragments(doc, fragments) {
|
|
3085
|
+
fragments.forEach((fragment) => {
|
|
3086
|
+
doc.definitions.push(fragment);
|
|
3087
|
+
});
|
|
3088
|
+
return doc;
|
|
3089
|
+
}
|
|
3090
|
+
function updateReferenceMapWithKnownKey(doc, key) {
|
|
3091
|
+
referenceMap.set(key, doc);
|
|
3092
|
+
}
|
|
3093
|
+
function updateReferenceMapAndGetKey(doc) {
|
|
3094
|
+
const key = new String();
|
|
3095
|
+
updateReferenceMapWithKnownKey(doc, key);
|
|
3096
|
+
return key;
|
|
3097
|
+
}
|
|
3098
|
+
function processSubstitutions(inputString, substitutions) {
|
|
3099
|
+
let outputString = "";
|
|
3100
|
+
const fragments = [];
|
|
3101
|
+
const subLength = substitutions.length;
|
|
3102
|
+
for (let i = 0; i < subLength; i++) {
|
|
3103
|
+
const substitution = substitutions[i];
|
|
3104
|
+
outputString += inputString[i];
|
|
3105
|
+
if (typeof substitution === "string" || typeof substitution === "number") {
|
|
3106
|
+
outputString += substitution;
|
|
3107
|
+
} else if (typeof substitution === "object") {
|
|
3108
|
+
const doc = referenceMap.get(substitution);
|
|
3109
|
+
if (doc === void 0) {
|
|
3110
|
+
if (process.env.NODE_ENV !== "production") {
|
|
3111
|
+
throw new Error("Invalid substitution fragment");
|
|
3112
|
+
}
|
|
3113
|
+
return null;
|
|
3114
|
+
}
|
|
3115
|
+
for (const def of doc.definitions) {
|
|
3116
|
+
fragments.push(def);
|
|
3117
|
+
}
|
|
3118
|
+
} else {
|
|
3119
|
+
if (process.env.NODE_ENV !== "production") {
|
|
3120
|
+
throw new Error("Unsupported substitution type");
|
|
3121
|
+
}
|
|
3122
|
+
return null;
|
|
3123
|
+
}
|
|
3124
|
+
}
|
|
3125
|
+
return {
|
|
3126
|
+
operationString: outputString + inputString[subLength],
|
|
3127
|
+
fragments
|
|
3128
|
+
};
|
|
3129
|
+
}
|
|
3130
|
+
const astResolver = function(astReference) {
|
|
3131
|
+
return referenceMap.get(astReference);
|
|
3132
|
+
};
|
|
3133
|
+
function gql(literals, ...subs) {
|
|
3134
|
+
let inputString;
|
|
3135
|
+
let inputSubstitutionFragments;
|
|
3136
|
+
if (!literals) {
|
|
3137
|
+
if (process.env.NODE_ENV !== "production") {
|
|
3138
|
+
throw new Error("Invalid query");
|
|
3139
|
+
}
|
|
3140
|
+
return null;
|
|
3141
|
+
} else if (typeof literals === "string") {
|
|
3142
|
+
inputString = literals.trim();
|
|
3143
|
+
inputSubstitutionFragments = [];
|
|
3144
|
+
} else {
|
|
3145
|
+
const sub = processSubstitutions(literals, subs);
|
|
3146
|
+
if (sub === null) {
|
|
3147
|
+
return null;
|
|
3148
|
+
}
|
|
3149
|
+
const { operationString, fragments } = sub;
|
|
3150
|
+
inputString = operationString.trim();
|
|
3151
|
+
inputSubstitutionFragments = fragments;
|
|
3152
|
+
}
|
|
3153
|
+
if (inputString.length === 0) {
|
|
3154
|
+
if (process.env.NODE_ENV !== "production") {
|
|
3155
|
+
throw new Error("Invalid query");
|
|
3156
|
+
}
|
|
3157
|
+
return null;
|
|
3158
|
+
}
|
|
3159
|
+
const document = parseDocument(inputString);
|
|
3160
|
+
if (document === null) {
|
|
3161
|
+
return null;
|
|
3162
|
+
}
|
|
3163
|
+
if (inputSubstitutionFragments.length === 0) {
|
|
3164
|
+
return updateReferenceMapAndGetKey(document);
|
|
3165
|
+
}
|
|
3166
|
+
return updateReferenceMapAndGetKey(insertFragments(document, inputSubstitutionFragments));
|
|
3167
|
+
}
|
|
3168
|
+
export {
|
|
3169
|
+
Kind,
|
|
3170
|
+
astResolver,
|
|
3171
|
+
gql,
|
|
3172
|
+
parse,
|
|
3173
|
+
print,
|
|
3174
|
+
visit
|
|
3175
|
+
};
|
|
3176
|
+
//# sourceMappingURL=index.js.map
|