UNPKG

43 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.parse = parse;
7exports.parseValue = parseValue;
8exports.parseType = parseType;
9exports.Parser = void 0;
10
11var _syntaxError = require("../error/syntaxError.js");
12
13var _kinds = require("./kinds.js");
14
15var _ast = require("./ast.js");
16
17var _tokenKind = require("./tokenKind.js");
18
19var _source = require("./source.js");
20
21var _directiveLocation = require("./directiveLocation.js");
22
23var _lexer = require("./lexer.js");
24
25/**
26 * Given a GraphQL source, parses it into a Document.
27 * Throws GraphQLError if a syntax error is encountered.
28 */
29function parse(source, options) {
30 var parser = new Parser(source, options);
31 return parser.parseDocument();
32}
33/**
34 * Given a string containing a GraphQL value (ex. `[42]`), parse the AST for
35 * that value.
36 * Throws GraphQLError if a syntax error is encountered.
37 *
38 * This is useful within tools that operate upon GraphQL Values directly and
39 * in isolation of complete GraphQL documents.
40 *
41 * Consider providing the results to the utility function: valueFromAST().
42 */
43
44
45function parseValue(source, options) {
46 var parser = new Parser(source, options);
47 parser.expectToken(_tokenKind.TokenKind.SOF);
48 var value = parser.parseValueLiteral(false);
49 parser.expectToken(_tokenKind.TokenKind.EOF);
50 return value;
51}
52/**
53 * Given a string containing a GraphQL Type (ex. `[Int!]`), parse the AST for
54 * that type.
55 * Throws GraphQLError if a syntax error is encountered.
56 *
57 * This is useful within tools that operate upon GraphQL Types directly and
58 * in isolation of complete GraphQL documents.
59 *
60 * Consider providing the results to the utility function: typeFromAST().
61 */
62
63
64function parseType(source, options) {
65 var parser = new Parser(source, options);
66 parser.expectToken(_tokenKind.TokenKind.SOF);
67 var type = parser.parseTypeReference();
68 parser.expectToken(_tokenKind.TokenKind.EOF);
69 return type;
70}
71/**
72 * This class is exported only to assist people in implementing their own parsers
73 * without duplicating too much code and should be used only as last resort for cases
74 * such as experimental syntax or if certain features could not be contributed upstream.
75 *
76 * It is still part of the internal API and is versioned, so any changes to it are never
77 * considered breaking changes. If you still need to support multiple versions of the
78 * library, please use the `versionInfo` variable for version detection.
79 *
80 * @internal
81 */
82
83
84var Parser = /*#__PURE__*/function () {
85 function Parser(source, options) {
86 var sourceObj = (0, _source.isSource)(source) ? source : new _source.Source(source);
87 this._lexer = new _lexer.Lexer(sourceObj);
88 this._options = options;
89 }
90 /**
91 * Converts a name lex token into a name parse node.
92 */
93
94
95 var _proto = Parser.prototype;
96
97 _proto.parseName = function parseName() {
98 var token = this.expectToken(_tokenKind.TokenKind.NAME);
99 return {
100 kind: _kinds.Kind.NAME,
101 value: token.value,
102 loc: this.loc(token)
103 };
104 } // Implements the parsing rules in the Document section.
105
106 /**
107 * Document : Definition+
108 */
109 ;
110
111 _proto.parseDocument = function parseDocument() {
112 var start = this._lexer.token;
113 return {
114 kind: _kinds.Kind.DOCUMENT,
115 definitions: this.many(_tokenKind.TokenKind.SOF, this.parseDefinition, _tokenKind.TokenKind.EOF),
116 loc: this.loc(start)
117 };
118 }
119 /**
120 * Definition :
121 * - ExecutableDefinition
122 * - TypeSystemDefinition
123 * - TypeSystemExtension
124 *
125 * ExecutableDefinition :
126 * - OperationDefinition
127 * - FragmentDefinition
128 */
129 ;
130
131 _proto.parseDefinition = function parseDefinition() {
132 if (this.peek(_tokenKind.TokenKind.NAME)) {
133 switch (this._lexer.token.value) {
134 case 'query':
135 case 'mutation':
136 case 'subscription':
137 return this.parseOperationDefinition();
138
139 case 'fragment':
140 return this.parseFragmentDefinition();
141
142 case 'schema':
143 case 'scalar':
144 case 'type':
145 case 'interface':
146 case 'union':
147 case 'enum':
148 case 'input':
149 case 'directive':
150 return this.parseTypeSystemDefinition();
151
152 case 'extend':
153 return this.parseTypeSystemExtension();
154 }
155 } else if (this.peek(_tokenKind.TokenKind.BRACE_L)) {
156 return this.parseOperationDefinition();
157 } else if (this.peekDescription()) {
158 return this.parseTypeSystemDefinition();
159 }
160
161 throw this.unexpected();
162 } // Implements the parsing rules in the Operations section.
163
164 /**
165 * OperationDefinition :
166 * - SelectionSet
167 * - OperationType Name? VariableDefinitions? Directives? SelectionSet
168 */
169 ;
170
171 _proto.parseOperationDefinition = function parseOperationDefinition() {
172 var start = this._lexer.token;
173
174 if (this.peek(_tokenKind.TokenKind.BRACE_L)) {
175 return {
176 kind: _kinds.Kind.OPERATION_DEFINITION,
177 operation: 'query',
178 name: undefined,
179 variableDefinitions: [],
180 directives: [],
181 selectionSet: this.parseSelectionSet(),
182 loc: this.loc(start)
183 };
184 }
185
186 var operation = this.parseOperationType();
187 var name;
188
189 if (this.peek(_tokenKind.TokenKind.NAME)) {
190 name = this.parseName();
191 }
192
193 return {
194 kind: _kinds.Kind.OPERATION_DEFINITION,
195 operation: operation,
196 name: name,
197 variableDefinitions: this.parseVariableDefinitions(),
198 directives: this.parseDirectives(false),
199 selectionSet: this.parseSelectionSet(),
200 loc: this.loc(start)
201 };
202 }
203 /**
204 * OperationType : one of query mutation subscription
205 */
206 ;
207
208 _proto.parseOperationType = function parseOperationType() {
209 var operationToken = this.expectToken(_tokenKind.TokenKind.NAME);
210
211 switch (operationToken.value) {
212 case 'query':
213 return 'query';
214
215 case 'mutation':
216 return 'mutation';
217
218 case 'subscription':
219 return 'subscription';
220 }
221
222 throw this.unexpected(operationToken);
223 }
224 /**
225 * VariableDefinitions : ( VariableDefinition+ )
226 */
227 ;
228
229 _proto.parseVariableDefinitions = function parseVariableDefinitions() {
230 return this.optionalMany(_tokenKind.TokenKind.PAREN_L, this.parseVariableDefinition, _tokenKind.TokenKind.PAREN_R);
231 }
232 /**
233 * VariableDefinition : Variable : Type DefaultValue? Directives[Const]?
234 */
235 ;
236
237 _proto.parseVariableDefinition = function parseVariableDefinition() {
238 var start = this._lexer.token;
239 return {
240 kind: _kinds.Kind.VARIABLE_DEFINITION,
241 variable: this.parseVariable(),
242 type: (this.expectToken(_tokenKind.TokenKind.COLON), this.parseTypeReference()),
243 defaultValue: this.expectOptionalToken(_tokenKind.TokenKind.EQUALS) ? this.parseValueLiteral(true) : undefined,
244 directives: this.parseDirectives(true),
245 loc: this.loc(start)
246 };
247 }
248 /**
249 * Variable : $ Name
250 */
251 ;
252
253 _proto.parseVariable = function parseVariable() {
254 var start = this._lexer.token;
255 this.expectToken(_tokenKind.TokenKind.DOLLAR);
256 return {
257 kind: _kinds.Kind.VARIABLE,
258 name: this.parseName(),
259 loc: this.loc(start)
260 };
261 }
262 /**
263 * SelectionSet : { Selection+ }
264 */
265 ;
266
267 _proto.parseSelectionSet = function parseSelectionSet() {
268 var start = this._lexer.token;
269 return {
270 kind: _kinds.Kind.SELECTION_SET,
271 selections: this.many(_tokenKind.TokenKind.BRACE_L, this.parseSelection, _tokenKind.TokenKind.BRACE_R),
272 loc: this.loc(start)
273 };
274 }
275 /**
276 * Selection :
277 * - Field
278 * - FragmentSpread
279 * - InlineFragment
280 */
281 ;
282
283 _proto.parseSelection = function parseSelection() {
284 return this.peek(_tokenKind.TokenKind.SPREAD) ? this.parseFragment() : this.parseField();
285 }
286 /**
287 * Field : Alias? Name Arguments? Directives? SelectionSet?
288 *
289 * Alias : Name :
290 */
291 ;
292
293 _proto.parseField = function parseField() {
294 var start = this._lexer.token;
295 var nameOrAlias = this.parseName();
296 var alias;
297 var name;
298
299 if (this.expectOptionalToken(_tokenKind.TokenKind.COLON)) {
300 alias = nameOrAlias;
301 name = this.parseName();
302 } else {
303 name = nameOrAlias;
304 }
305
306 return {
307 kind: _kinds.Kind.FIELD,
308 alias: alias,
309 name: name,
310 arguments: this.parseArguments(false),
311 directives: this.parseDirectives(false),
312 selectionSet: this.peek(_tokenKind.TokenKind.BRACE_L) ? this.parseSelectionSet() : undefined,
313 loc: this.loc(start)
314 };
315 }
316 /**
317 * Arguments[Const] : ( Argument[?Const]+ )
318 */
319 ;
320
321 _proto.parseArguments = function parseArguments(isConst) {
322 var item = isConst ? this.parseConstArgument : this.parseArgument;
323 return this.optionalMany(_tokenKind.TokenKind.PAREN_L, item, _tokenKind.TokenKind.PAREN_R);
324 }
325 /**
326 * Argument[Const] : Name : Value[?Const]
327 */
328 ;
329
330 _proto.parseArgument = function parseArgument() {
331 var start = this._lexer.token;
332 var name = this.parseName();
333 this.expectToken(_tokenKind.TokenKind.COLON);
334 return {
335 kind: _kinds.Kind.ARGUMENT,
336 name: name,
337 value: this.parseValueLiteral(false),
338 loc: this.loc(start)
339 };
340 };
341
342 _proto.parseConstArgument = function parseConstArgument() {
343 var start = this._lexer.token;
344 return {
345 kind: _kinds.Kind.ARGUMENT,
346 name: this.parseName(),
347 value: (this.expectToken(_tokenKind.TokenKind.COLON), this.parseValueLiteral(true)),
348 loc: this.loc(start)
349 };
350 } // Implements the parsing rules in the Fragments section.
351
352 /**
353 * Corresponds to both FragmentSpread and InlineFragment in the spec.
354 *
355 * FragmentSpread : ... FragmentName Directives?
356 *
357 * InlineFragment : ... TypeCondition? Directives? SelectionSet
358 */
359 ;
360
361 _proto.parseFragment = function parseFragment() {
362 var start = this._lexer.token;
363 this.expectToken(_tokenKind.TokenKind.SPREAD);
364 var hasTypeCondition = this.expectOptionalKeyword('on');
365
366 if (!hasTypeCondition && this.peek(_tokenKind.TokenKind.NAME)) {
367 return {
368 kind: _kinds.Kind.FRAGMENT_SPREAD,
369 name: this.parseFragmentName(),
370 directives: this.parseDirectives(false),
371 loc: this.loc(start)
372 };
373 }
374
375 return {
376 kind: _kinds.Kind.INLINE_FRAGMENT,
377 typeCondition: hasTypeCondition ? this.parseNamedType() : undefined,
378 directives: this.parseDirectives(false),
379 selectionSet: this.parseSelectionSet(),
380 loc: this.loc(start)
381 };
382 }
383 /**
384 * FragmentDefinition :
385 * - fragment FragmentName on TypeCondition Directives? SelectionSet
386 *
387 * TypeCondition : NamedType
388 */
389 ;
390
391 _proto.parseFragmentDefinition = function parseFragmentDefinition() {
392 var _this$_options;
393
394 var start = this._lexer.token;
395 this.expectKeyword('fragment'); // Experimental support for defining variables within fragments changes
396 // the grammar of FragmentDefinition:
397 // - fragment FragmentName VariableDefinitions? on TypeCondition Directives? SelectionSet
398
399 if (((_this$_options = this._options) === null || _this$_options === void 0 ? void 0 : _this$_options.experimentalFragmentVariables) === true) {
400 return {
401 kind: _kinds.Kind.FRAGMENT_DEFINITION,
402 name: this.parseFragmentName(),
403 variableDefinitions: this.parseVariableDefinitions(),
404 typeCondition: (this.expectKeyword('on'), this.parseNamedType()),
405 directives: this.parseDirectives(false),
406 selectionSet: this.parseSelectionSet(),
407 loc: this.loc(start)
408 };
409 }
410
411 return {
412 kind: _kinds.Kind.FRAGMENT_DEFINITION,
413 name: this.parseFragmentName(),
414 typeCondition: (this.expectKeyword('on'), this.parseNamedType()),
415 directives: this.parseDirectives(false),
416 selectionSet: this.parseSelectionSet(),
417 loc: this.loc(start)
418 };
419 }
420 /**
421 * FragmentName : Name but not `on`
422 */
423 ;
424
425 _proto.parseFragmentName = function parseFragmentName() {
426 if (this._lexer.token.value === 'on') {
427 throw this.unexpected();
428 }
429
430 return this.parseName();
431 } // Implements the parsing rules in the Values section.
432
433 /**
434 * Value[Const] :
435 * - [~Const] Variable
436 * - IntValue
437 * - FloatValue
438 * - StringValue
439 * - BooleanValue
440 * - NullValue
441 * - EnumValue
442 * - ListValue[?Const]
443 * - ObjectValue[?Const]
444 *
445 * BooleanValue : one of `true` `false`
446 *
447 * NullValue : `null`
448 *
449 * EnumValue : Name but not `true`, `false` or `null`
450 */
451 ;
452
453 _proto.parseValueLiteral = function parseValueLiteral(isConst) {
454 var token = this._lexer.token;
455
456 switch (token.kind) {
457 case _tokenKind.TokenKind.BRACKET_L:
458 return this.parseList(isConst);
459
460 case _tokenKind.TokenKind.BRACE_L:
461 return this.parseObject(isConst);
462
463 case _tokenKind.TokenKind.INT:
464 this._lexer.advance();
465
466 return {
467 kind: _kinds.Kind.INT,
468 value: token.value,
469 loc: this.loc(token)
470 };
471
472 case _tokenKind.TokenKind.FLOAT:
473 this._lexer.advance();
474
475 return {
476 kind: _kinds.Kind.FLOAT,
477 value: token.value,
478 loc: this.loc(token)
479 };
480
481 case _tokenKind.TokenKind.STRING:
482 case _tokenKind.TokenKind.BLOCK_STRING:
483 return this.parseStringLiteral();
484
485 case _tokenKind.TokenKind.NAME:
486 this._lexer.advance();
487
488 switch (token.value) {
489 case 'true':
490 return {
491 kind: _kinds.Kind.BOOLEAN,
492 value: true,
493 loc: this.loc(token)
494 };
495
496 case 'false':
497 return {
498 kind: _kinds.Kind.BOOLEAN,
499 value: false,
500 loc: this.loc(token)
501 };
502
503 case 'null':
504 return {
505 kind: _kinds.Kind.NULL,
506 loc: this.loc(token)
507 };
508
509 default:
510 return {
511 kind: _kinds.Kind.ENUM,
512 value: token.value,
513 loc: this.loc(token)
514 };
515 }
516
517 case _tokenKind.TokenKind.DOLLAR:
518 if (!isConst) {
519 return this.parseVariable();
520 }
521
522 break;
523 }
524
525 throw this.unexpected();
526 };
527
528 _proto.parseStringLiteral = function parseStringLiteral() {
529 var token = this._lexer.token;
530
531 this._lexer.advance();
532
533 return {
534 kind: _kinds.Kind.STRING,
535 value: token.value,
536 block: token.kind === _tokenKind.TokenKind.BLOCK_STRING,
537 loc: this.loc(token)
538 };
539 }
540 /**
541 * ListValue[Const] :
542 * - [ ]
543 * - [ Value[?Const]+ ]
544 */
545 ;
546
547 _proto.parseList = function parseList(isConst) {
548 var _this = this;
549
550 var start = this._lexer.token;
551
552 var item = function item() {
553 return _this.parseValueLiteral(isConst);
554 };
555
556 return {
557 kind: _kinds.Kind.LIST,
558 values: this.any(_tokenKind.TokenKind.BRACKET_L, item, _tokenKind.TokenKind.BRACKET_R),
559 loc: this.loc(start)
560 };
561 }
562 /**
563 * ObjectValue[Const] :
564 * - { }
565 * - { ObjectField[?Const]+ }
566 */
567 ;
568
569 _proto.parseObject = function parseObject(isConst) {
570 var _this2 = this;
571
572 var start = this._lexer.token;
573
574 var item = function item() {
575 return _this2.parseObjectField(isConst);
576 };
577
578 return {
579 kind: _kinds.Kind.OBJECT,
580 fields: this.any(_tokenKind.TokenKind.BRACE_L, item, _tokenKind.TokenKind.BRACE_R),
581 loc: this.loc(start)
582 };
583 }
584 /**
585 * ObjectField[Const] : Name : Value[?Const]
586 */
587 ;
588
589 _proto.parseObjectField = function parseObjectField(isConst) {
590 var start = this._lexer.token;
591 var name = this.parseName();
592 this.expectToken(_tokenKind.TokenKind.COLON);
593 return {
594 kind: _kinds.Kind.OBJECT_FIELD,
595 name: name,
596 value: this.parseValueLiteral(isConst),
597 loc: this.loc(start)
598 };
599 } // Implements the parsing rules in the Directives section.
600
601 /**
602 * Directives[Const] : Directive[?Const]+
603 */
604 ;
605
606 _proto.parseDirectives = function parseDirectives(isConst) {
607 var directives = [];
608
609 while (this.peek(_tokenKind.TokenKind.AT)) {
610 directives.push(this.parseDirective(isConst));
611 }
612
613 return directives;
614 }
615 /**
616 * Directive[Const] : @ Name Arguments[?Const]?
617 */
618 ;
619
620 _proto.parseDirective = function parseDirective(isConst) {
621 var start = this._lexer.token;
622 this.expectToken(_tokenKind.TokenKind.AT);
623 return {
624 kind: _kinds.Kind.DIRECTIVE,
625 name: this.parseName(),
626 arguments: this.parseArguments(isConst),
627 loc: this.loc(start)
628 };
629 } // Implements the parsing rules in the Types section.
630
631 /**
632 * Type :
633 * - NamedType
634 * - ListType
635 * - NonNullType
636 */
637 ;
638
639 _proto.parseTypeReference = function parseTypeReference() {
640 var start = this._lexer.token;
641 var type;
642
643 if (this.expectOptionalToken(_tokenKind.TokenKind.BRACKET_L)) {
644 type = this.parseTypeReference();
645 this.expectToken(_tokenKind.TokenKind.BRACKET_R);
646 type = {
647 kind: _kinds.Kind.LIST_TYPE,
648 type: type,
649 loc: this.loc(start)
650 };
651 } else {
652 type = this.parseNamedType();
653 }
654
655 if (this.expectOptionalToken(_tokenKind.TokenKind.BANG)) {
656 return {
657 kind: _kinds.Kind.NON_NULL_TYPE,
658 type: type,
659 loc: this.loc(start)
660 };
661 }
662
663 return type;
664 }
665 /**
666 * NamedType : Name
667 */
668 ;
669
670 _proto.parseNamedType = function parseNamedType() {
671 var start = this._lexer.token;
672 return {
673 kind: _kinds.Kind.NAMED_TYPE,
674 name: this.parseName(),
675 loc: this.loc(start)
676 };
677 } // Implements the parsing rules in the Type Definition section.
678
679 /**
680 * TypeSystemDefinition :
681 * - SchemaDefinition
682 * - TypeDefinition
683 * - DirectiveDefinition
684 *
685 * TypeDefinition :
686 * - ScalarTypeDefinition
687 * - ObjectTypeDefinition
688 * - InterfaceTypeDefinition
689 * - UnionTypeDefinition
690 * - EnumTypeDefinition
691 * - InputObjectTypeDefinition
692 */
693 ;
694
695 _proto.parseTypeSystemDefinition = function parseTypeSystemDefinition() {
696 // Many definitions begin with a description and require a lookahead.
697 var keywordToken = this.peekDescription() ? this._lexer.lookahead() : this._lexer.token;
698
699 if (keywordToken.kind === _tokenKind.TokenKind.NAME) {
700 switch (keywordToken.value) {
701 case 'schema':
702 return this.parseSchemaDefinition();
703
704 case 'scalar':
705 return this.parseScalarTypeDefinition();
706
707 case 'type':
708 return this.parseObjectTypeDefinition();
709
710 case 'interface':
711 return this.parseInterfaceTypeDefinition();
712
713 case 'union':
714 return this.parseUnionTypeDefinition();
715
716 case 'enum':
717 return this.parseEnumTypeDefinition();
718
719 case 'input':
720 return this.parseInputObjectTypeDefinition();
721
722 case 'directive':
723 return this.parseDirectiveDefinition();
724 }
725 }
726
727 throw this.unexpected(keywordToken);
728 };
729
730 _proto.peekDescription = function peekDescription() {
731 return this.peek(_tokenKind.TokenKind.STRING) || this.peek(_tokenKind.TokenKind.BLOCK_STRING);
732 }
733 /**
734 * Description : StringValue
735 */
736 ;
737
738 _proto.parseDescription = function parseDescription() {
739 if (this.peekDescription()) {
740 return this.parseStringLiteral();
741 }
742 }
743 /**
744 * SchemaDefinition : Description? schema Directives[Const]? { OperationTypeDefinition+ }
745 */
746 ;
747
748 _proto.parseSchemaDefinition = function parseSchemaDefinition() {
749 var start = this._lexer.token;
750 var description = this.parseDescription();
751 this.expectKeyword('schema');
752 var directives = this.parseDirectives(true);
753 var operationTypes = this.many(_tokenKind.TokenKind.BRACE_L, this.parseOperationTypeDefinition, _tokenKind.TokenKind.BRACE_R);
754 return {
755 kind: _kinds.Kind.SCHEMA_DEFINITION,
756 description: description,
757 directives: directives,
758 operationTypes: operationTypes,
759 loc: this.loc(start)
760 };
761 }
762 /**
763 * OperationTypeDefinition : OperationType : NamedType
764 */
765 ;
766
767 _proto.parseOperationTypeDefinition = function parseOperationTypeDefinition() {
768 var start = this._lexer.token;
769 var operation = this.parseOperationType();
770 this.expectToken(_tokenKind.TokenKind.COLON);
771 var type = this.parseNamedType();
772 return {
773 kind: _kinds.Kind.OPERATION_TYPE_DEFINITION,
774 operation: operation,
775 type: type,
776 loc: this.loc(start)
777 };
778 }
779 /**
780 * ScalarTypeDefinition : Description? scalar Name Directives[Const]?
781 */
782 ;
783
784 _proto.parseScalarTypeDefinition = function parseScalarTypeDefinition() {
785 var start = this._lexer.token;
786 var description = this.parseDescription();
787 this.expectKeyword('scalar');
788 var name = this.parseName();
789 var directives = this.parseDirectives(true);
790 return {
791 kind: _kinds.Kind.SCALAR_TYPE_DEFINITION,
792 description: description,
793 name: name,
794 directives: directives,
795 loc: this.loc(start)
796 };
797 }
798 /**
799 * ObjectTypeDefinition :
800 * Description?
801 * type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition?
802 */
803 ;
804
805 _proto.parseObjectTypeDefinition = function parseObjectTypeDefinition() {
806 var start = this._lexer.token;
807 var description = this.parseDescription();
808 this.expectKeyword('type');
809 var name = this.parseName();
810 var interfaces = this.parseImplementsInterfaces();
811 var directives = this.parseDirectives(true);
812 var fields = this.parseFieldsDefinition();
813 return {
814 kind: _kinds.Kind.OBJECT_TYPE_DEFINITION,
815 description: description,
816 name: name,
817 interfaces: interfaces,
818 directives: directives,
819 fields: fields,
820 loc: this.loc(start)
821 };
822 }
823 /**
824 * ImplementsInterfaces :
825 * - implements `&`? NamedType
826 * - ImplementsInterfaces & NamedType
827 */
828 ;
829
830 _proto.parseImplementsInterfaces = function parseImplementsInterfaces() {
831 var _this$_options2;
832
833 if (!this.expectOptionalKeyword('implements')) {
834 return [];
835 }
836
837 if (((_this$_options2 = this._options) === null || _this$_options2 === void 0 ? void 0 : _this$_options2.allowLegacySDLImplementsInterfaces) === true) {
838 var types = []; // Optional leading ampersand
839
840 this.expectOptionalToken(_tokenKind.TokenKind.AMP);
841
842 do {
843 types.push(this.parseNamedType());
844 } while (this.expectOptionalToken(_tokenKind.TokenKind.AMP) || this.peek(_tokenKind.TokenKind.NAME));
845
846 return types;
847 }
848
849 return this.delimitedMany(_tokenKind.TokenKind.AMP, this.parseNamedType);
850 }
851 /**
852 * FieldsDefinition : { FieldDefinition+ }
853 */
854 ;
855
856 _proto.parseFieldsDefinition = function parseFieldsDefinition() {
857 var _this$_options3;
858
859 // Legacy support for the SDL?
860 if (((_this$_options3 = this._options) === null || _this$_options3 === void 0 ? void 0 : _this$_options3.allowLegacySDLEmptyFields) === true && this.peek(_tokenKind.TokenKind.BRACE_L) && this._lexer.lookahead().kind === _tokenKind.TokenKind.BRACE_R) {
861 this._lexer.advance();
862
863 this._lexer.advance();
864
865 return [];
866 }
867
868 return this.optionalMany(_tokenKind.TokenKind.BRACE_L, this.parseFieldDefinition, _tokenKind.TokenKind.BRACE_R);
869 }
870 /**
871 * FieldDefinition :
872 * - Description? Name ArgumentsDefinition? : Type Directives[Const]?
873 */
874 ;
875
876 _proto.parseFieldDefinition = function parseFieldDefinition() {
877 var start = this._lexer.token;
878 var description = this.parseDescription();
879 var name = this.parseName();
880 var args = this.parseArgumentDefs();
881 this.expectToken(_tokenKind.TokenKind.COLON);
882 var type = this.parseTypeReference();
883 var directives = this.parseDirectives(true);
884 return {
885 kind: _kinds.Kind.FIELD_DEFINITION,
886 description: description,
887 name: name,
888 arguments: args,
889 type: type,
890 directives: directives,
891 loc: this.loc(start)
892 };
893 }
894 /**
895 * ArgumentsDefinition : ( InputValueDefinition+ )
896 */
897 ;
898
899 _proto.parseArgumentDefs = function parseArgumentDefs() {
900 return this.optionalMany(_tokenKind.TokenKind.PAREN_L, this.parseInputValueDef, _tokenKind.TokenKind.PAREN_R);
901 }
902 /**
903 * InputValueDefinition :
904 * - Description? Name : Type DefaultValue? Directives[Const]?
905 */
906 ;
907
908 _proto.parseInputValueDef = function parseInputValueDef() {
909 var start = this._lexer.token;
910 var description = this.parseDescription();
911 var name = this.parseName();
912 this.expectToken(_tokenKind.TokenKind.COLON);
913 var type = this.parseTypeReference();
914 var defaultValue;
915
916 if (this.expectOptionalToken(_tokenKind.TokenKind.EQUALS)) {
917 defaultValue = this.parseValueLiteral(true);
918 }
919
920 var directives = this.parseDirectives(true);
921 return {
922 kind: _kinds.Kind.INPUT_VALUE_DEFINITION,
923 description: description,
924 name: name,
925 type: type,
926 defaultValue: defaultValue,
927 directives: directives,
928 loc: this.loc(start)
929 };
930 }
931 /**
932 * InterfaceTypeDefinition :
933 * - Description? interface Name Directives[Const]? FieldsDefinition?
934 */
935 ;
936
937 _proto.parseInterfaceTypeDefinition = function parseInterfaceTypeDefinition() {
938 var start = this._lexer.token;
939 var description = this.parseDescription();
940 this.expectKeyword('interface');
941 var name = this.parseName();
942 var interfaces = this.parseImplementsInterfaces();
943 var directives = this.parseDirectives(true);
944 var fields = this.parseFieldsDefinition();
945 return {
946 kind: _kinds.Kind.INTERFACE_TYPE_DEFINITION,
947 description: description,
948 name: name,
949 interfaces: interfaces,
950 directives: directives,
951 fields: fields,
952 loc: this.loc(start)
953 };
954 }
955 /**
956 * UnionTypeDefinition :
957 * - Description? union Name Directives[Const]? UnionMemberTypes?
958 */
959 ;
960
961 _proto.parseUnionTypeDefinition = function parseUnionTypeDefinition() {
962 var start = this._lexer.token;
963 var description = this.parseDescription();
964 this.expectKeyword('union');
965 var name = this.parseName();
966 var directives = this.parseDirectives(true);
967 var types = this.parseUnionMemberTypes();
968 return {
969 kind: _kinds.Kind.UNION_TYPE_DEFINITION,
970 description: description,
971 name: name,
972 directives: directives,
973 types: types,
974 loc: this.loc(start)
975 };
976 }
977 /**
978 * UnionMemberTypes :
979 * - = `|`? NamedType
980 * - UnionMemberTypes | NamedType
981 */
982 ;
983
984 _proto.parseUnionMemberTypes = function parseUnionMemberTypes() {
985 return this.expectOptionalToken(_tokenKind.TokenKind.EQUALS) ? this.delimitedMany(_tokenKind.TokenKind.PIPE, this.parseNamedType) : [];
986 }
987 /**
988 * EnumTypeDefinition :
989 * - Description? enum Name Directives[Const]? EnumValuesDefinition?
990 */
991 ;
992
993 _proto.parseEnumTypeDefinition = function parseEnumTypeDefinition() {
994 var start = this._lexer.token;
995 var description = this.parseDescription();
996 this.expectKeyword('enum');
997 var name = this.parseName();
998 var directives = this.parseDirectives(true);
999 var values = this.parseEnumValuesDefinition();
1000 return {
1001 kind: _kinds.Kind.ENUM_TYPE_DEFINITION,
1002 description: description,
1003 name: name,
1004 directives: directives,
1005 values: values,
1006 loc: this.loc(start)
1007 };
1008 }
1009 /**
1010 * EnumValuesDefinition : { EnumValueDefinition+ }
1011 */
1012 ;
1013
1014 _proto.parseEnumValuesDefinition = function parseEnumValuesDefinition() {
1015 return this.optionalMany(_tokenKind.TokenKind.BRACE_L, this.parseEnumValueDefinition, _tokenKind.TokenKind.BRACE_R);
1016 }
1017 /**
1018 * EnumValueDefinition : Description? EnumValue Directives[Const]?
1019 *
1020 * EnumValue : Name
1021 */
1022 ;
1023
1024 _proto.parseEnumValueDefinition = function parseEnumValueDefinition() {
1025 var start = this._lexer.token;
1026 var description = this.parseDescription();
1027 var name = this.parseName();
1028 var directives = this.parseDirectives(true);
1029 return {
1030 kind: _kinds.Kind.ENUM_VALUE_DEFINITION,
1031 description: description,
1032 name: name,
1033 directives: directives,
1034 loc: this.loc(start)
1035 };
1036 }
1037 /**
1038 * InputObjectTypeDefinition :
1039 * - Description? input Name Directives[Const]? InputFieldsDefinition?
1040 */
1041 ;
1042
1043 _proto.parseInputObjectTypeDefinition = function parseInputObjectTypeDefinition() {
1044 var start = this._lexer.token;
1045 var description = this.parseDescription();
1046 this.expectKeyword('input');
1047 var name = this.parseName();
1048 var directives = this.parseDirectives(true);
1049 var fields = this.parseInputFieldsDefinition();
1050 return {
1051 kind: _kinds.Kind.INPUT_OBJECT_TYPE_DEFINITION,
1052 description: description,
1053 name: name,
1054 directives: directives,
1055 fields: fields,
1056 loc: this.loc(start)
1057 };
1058 }
1059 /**
1060 * InputFieldsDefinition : { InputValueDefinition+ }
1061 */
1062 ;
1063
1064 _proto.parseInputFieldsDefinition = function parseInputFieldsDefinition() {
1065 return this.optionalMany(_tokenKind.TokenKind.BRACE_L, this.parseInputValueDef, _tokenKind.TokenKind.BRACE_R);
1066 }
1067 /**
1068 * TypeSystemExtension :
1069 * - SchemaExtension
1070 * - TypeExtension
1071 *
1072 * TypeExtension :
1073 * - ScalarTypeExtension
1074 * - ObjectTypeExtension
1075 * - InterfaceTypeExtension
1076 * - UnionTypeExtension
1077 * - EnumTypeExtension
1078 * - InputObjectTypeDefinition
1079 */
1080 ;
1081
1082 _proto.parseTypeSystemExtension = function parseTypeSystemExtension() {
1083 var keywordToken = this._lexer.lookahead();
1084
1085 if (keywordToken.kind === _tokenKind.TokenKind.NAME) {
1086 switch (keywordToken.value) {
1087 case 'schema':
1088 return this.parseSchemaExtension();
1089
1090 case 'scalar':
1091 return this.parseScalarTypeExtension();
1092
1093 case 'type':
1094 return this.parseObjectTypeExtension();
1095
1096 case 'interface':
1097 return this.parseInterfaceTypeExtension();
1098
1099 case 'union':
1100 return this.parseUnionTypeExtension();
1101
1102 case 'enum':
1103 return this.parseEnumTypeExtension();
1104
1105 case 'input':
1106 return this.parseInputObjectTypeExtension();
1107 }
1108 }
1109
1110 throw this.unexpected(keywordToken);
1111 }
1112 /**
1113 * SchemaExtension :
1114 * - extend schema Directives[Const]? { OperationTypeDefinition+ }
1115 * - extend schema Directives[Const]
1116 */
1117 ;
1118
1119 _proto.parseSchemaExtension = function parseSchemaExtension() {
1120 var start = this._lexer.token;
1121 this.expectKeyword('extend');
1122 this.expectKeyword('schema');
1123 var directives = this.parseDirectives(true);
1124 var operationTypes = this.optionalMany(_tokenKind.TokenKind.BRACE_L, this.parseOperationTypeDefinition, _tokenKind.TokenKind.BRACE_R);
1125
1126 if (directives.length === 0 && operationTypes.length === 0) {
1127 throw this.unexpected();
1128 }
1129
1130 return {
1131 kind: _kinds.Kind.SCHEMA_EXTENSION,
1132 directives: directives,
1133 operationTypes: operationTypes,
1134 loc: this.loc(start)
1135 };
1136 }
1137 /**
1138 * ScalarTypeExtension :
1139 * - extend scalar Name Directives[Const]
1140 */
1141 ;
1142
1143 _proto.parseScalarTypeExtension = function parseScalarTypeExtension() {
1144 var start = this._lexer.token;
1145 this.expectKeyword('extend');
1146 this.expectKeyword('scalar');
1147 var name = this.parseName();
1148 var directives = this.parseDirectives(true);
1149
1150 if (directives.length === 0) {
1151 throw this.unexpected();
1152 }
1153
1154 return {
1155 kind: _kinds.Kind.SCALAR_TYPE_EXTENSION,
1156 name: name,
1157 directives: directives,
1158 loc: this.loc(start)
1159 };
1160 }
1161 /**
1162 * ObjectTypeExtension :
1163 * - extend type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
1164 * - extend type Name ImplementsInterfaces? Directives[Const]
1165 * - extend type Name ImplementsInterfaces
1166 */
1167 ;
1168
1169 _proto.parseObjectTypeExtension = function parseObjectTypeExtension() {
1170 var start = this._lexer.token;
1171 this.expectKeyword('extend');
1172 this.expectKeyword('type');
1173 var name = this.parseName();
1174 var interfaces = this.parseImplementsInterfaces();
1175 var directives = this.parseDirectives(true);
1176 var fields = this.parseFieldsDefinition();
1177
1178 if (interfaces.length === 0 && directives.length === 0 && fields.length === 0) {
1179 throw this.unexpected();
1180 }
1181
1182 return {
1183 kind: _kinds.Kind.OBJECT_TYPE_EXTENSION,
1184 name: name,
1185 interfaces: interfaces,
1186 directives: directives,
1187 fields: fields,
1188 loc: this.loc(start)
1189 };
1190 }
1191 /**
1192 * InterfaceTypeExtension :
1193 * - extend interface Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
1194 * - extend interface Name ImplementsInterfaces? Directives[Const]
1195 * - extend interface Name ImplementsInterfaces
1196 */
1197 ;
1198
1199 _proto.parseInterfaceTypeExtension = function parseInterfaceTypeExtension() {
1200 var start = this._lexer.token;
1201 this.expectKeyword('extend');
1202 this.expectKeyword('interface');
1203 var name = this.parseName();
1204 var interfaces = this.parseImplementsInterfaces();
1205 var directives = this.parseDirectives(true);
1206 var fields = this.parseFieldsDefinition();
1207
1208 if (interfaces.length === 0 && directives.length === 0 && fields.length === 0) {
1209 throw this.unexpected();
1210 }
1211
1212 return {
1213 kind: _kinds.Kind.INTERFACE_TYPE_EXTENSION,
1214 name: name,
1215 interfaces: interfaces,
1216 directives: directives,
1217 fields: fields,
1218 loc: this.loc(start)
1219 };
1220 }
1221 /**
1222 * UnionTypeExtension :
1223 * - extend union Name Directives[Const]? UnionMemberTypes
1224 * - extend union Name Directives[Const]
1225 */
1226 ;
1227
1228 _proto.parseUnionTypeExtension = function parseUnionTypeExtension() {
1229 var start = this._lexer.token;
1230 this.expectKeyword('extend');
1231 this.expectKeyword('union');
1232 var name = this.parseName();
1233 var directives = this.parseDirectives(true);
1234 var types = this.parseUnionMemberTypes();
1235
1236 if (directives.length === 0 && types.length === 0) {
1237 throw this.unexpected();
1238 }
1239
1240 return {
1241 kind: _kinds.Kind.UNION_TYPE_EXTENSION,
1242 name: name,
1243 directives: directives,
1244 types: types,
1245 loc: this.loc(start)
1246 };
1247 }
1248 /**
1249 * EnumTypeExtension :
1250 * - extend enum Name Directives[Const]? EnumValuesDefinition
1251 * - extend enum Name Directives[Const]
1252 */
1253 ;
1254
1255 _proto.parseEnumTypeExtension = function parseEnumTypeExtension() {
1256 var start = this._lexer.token;
1257 this.expectKeyword('extend');
1258 this.expectKeyword('enum');
1259 var name = this.parseName();
1260 var directives = this.parseDirectives(true);
1261 var values = this.parseEnumValuesDefinition();
1262
1263 if (directives.length === 0 && values.length === 0) {
1264 throw this.unexpected();
1265 }
1266
1267 return {
1268 kind: _kinds.Kind.ENUM_TYPE_EXTENSION,
1269 name: name,
1270 directives: directives,
1271 values: values,
1272 loc: this.loc(start)
1273 };
1274 }
1275 /**
1276 * InputObjectTypeExtension :
1277 * - extend input Name Directives[Const]? InputFieldsDefinition
1278 * - extend input Name Directives[Const]
1279 */
1280 ;
1281
1282 _proto.parseInputObjectTypeExtension = function parseInputObjectTypeExtension() {
1283 var start = this._lexer.token;
1284 this.expectKeyword('extend');
1285 this.expectKeyword('input');
1286 var name = this.parseName();
1287 var directives = this.parseDirectives(true);
1288 var fields = this.parseInputFieldsDefinition();
1289
1290 if (directives.length === 0 && fields.length === 0) {
1291 throw this.unexpected();
1292 }
1293
1294 return {
1295 kind: _kinds.Kind.INPUT_OBJECT_TYPE_EXTENSION,
1296 name: name,
1297 directives: directives,
1298 fields: fields,
1299 loc: this.loc(start)
1300 };
1301 }
1302 /**
1303 * DirectiveDefinition :
1304 * - Description? directive @ Name ArgumentsDefinition? `repeatable`? on DirectiveLocations
1305 */
1306 ;
1307
1308 _proto.parseDirectiveDefinition = function parseDirectiveDefinition() {
1309 var start = this._lexer.token;
1310 var description = this.parseDescription();
1311 this.expectKeyword('directive');
1312 this.expectToken(_tokenKind.TokenKind.AT);
1313 var name = this.parseName();
1314 var args = this.parseArgumentDefs();
1315 var repeatable = this.expectOptionalKeyword('repeatable');
1316 this.expectKeyword('on');
1317 var locations = this.parseDirectiveLocations();
1318 return {
1319 kind: _kinds.Kind.DIRECTIVE_DEFINITION,
1320 description: description,
1321 name: name,
1322 arguments: args,
1323 repeatable: repeatable,
1324 locations: locations,
1325 loc: this.loc(start)
1326 };
1327 }
1328 /**
1329 * DirectiveLocations :
1330 * - `|`? DirectiveLocation
1331 * - DirectiveLocations | DirectiveLocation
1332 */
1333 ;
1334
1335 _proto.parseDirectiveLocations = function parseDirectiveLocations() {
1336 return this.delimitedMany(_tokenKind.TokenKind.PIPE, this.parseDirectiveLocation);
1337 }
1338 /*
1339 * DirectiveLocation :
1340 * - ExecutableDirectiveLocation
1341 * - TypeSystemDirectiveLocation
1342 *
1343 * ExecutableDirectiveLocation : one of
1344 * `QUERY`
1345 * `MUTATION`
1346 * `SUBSCRIPTION`
1347 * `FIELD`
1348 * `FRAGMENT_DEFINITION`
1349 * `FRAGMENT_SPREAD`
1350 * `INLINE_FRAGMENT`
1351 *
1352 * TypeSystemDirectiveLocation : one of
1353 * `SCHEMA`
1354 * `SCALAR`
1355 * `OBJECT`
1356 * `FIELD_DEFINITION`
1357 * `ARGUMENT_DEFINITION`
1358 * `INTERFACE`
1359 * `UNION`
1360 * `ENUM`
1361 * `ENUM_VALUE`
1362 * `INPUT_OBJECT`
1363 * `INPUT_FIELD_DEFINITION`
1364 */
1365 ;
1366
1367 _proto.parseDirectiveLocation = function parseDirectiveLocation() {
1368 var start = this._lexer.token;
1369 var name = this.parseName();
1370
1371 if (_directiveLocation.DirectiveLocation[name.value] !== undefined) {
1372 return name;
1373 }
1374
1375 throw this.unexpected(start);
1376 } // Core parsing utility functions
1377
1378 /**
1379 * Returns a location object, used to identify the place in the source that created a given parsed object.
1380 */
1381 ;
1382
1383 _proto.loc = function loc(startToken) {
1384 var _this$_options4;
1385
1386 if (((_this$_options4 = this._options) === null || _this$_options4 === void 0 ? void 0 : _this$_options4.noLocation) !== true) {
1387 return new _ast.Location(startToken, this._lexer.lastToken, this._lexer.source);
1388 }
1389 }
1390 /**
1391 * Determines if the next token is of a given kind
1392 */
1393 ;
1394
1395 _proto.peek = function peek(kind) {
1396 return this._lexer.token.kind === kind;
1397 }
1398 /**
1399 * If the next token is of the given kind, return that token after advancing the lexer.
1400 * Otherwise, do not change the parser state and throw an error.
1401 */
1402 ;
1403
1404 _proto.expectToken = function expectToken(kind) {
1405 var token = this._lexer.token;
1406
1407 if (token.kind === kind) {
1408 this._lexer.advance();
1409
1410 return token;
1411 }
1412
1413 throw (0, _syntaxError.syntaxError)(this._lexer.source, token.start, "Expected ".concat(getTokenKindDesc(kind), ", found ").concat(getTokenDesc(token), "."));
1414 }
1415 /**
1416 * If the next token is of the given kind, return that token after advancing the lexer.
1417 * Otherwise, do not change the parser state and return undefined.
1418 */
1419 ;
1420
1421 _proto.expectOptionalToken = function expectOptionalToken(kind) {
1422 var token = this._lexer.token;
1423
1424 if (token.kind === kind) {
1425 this._lexer.advance();
1426
1427 return token;
1428 }
1429
1430 return undefined;
1431 }
1432 /**
1433 * If the next token is a given keyword, advance the lexer.
1434 * Otherwise, do not change the parser state and throw an error.
1435 */
1436 ;
1437
1438 _proto.expectKeyword = function expectKeyword(value) {
1439 var token = this._lexer.token;
1440
1441 if (token.kind === _tokenKind.TokenKind.NAME && token.value === value) {
1442 this._lexer.advance();
1443 } else {
1444 throw (0, _syntaxError.syntaxError)(this._lexer.source, token.start, "Expected \"".concat(value, "\", found ").concat(getTokenDesc(token), "."));
1445 }
1446 }
1447 /**
1448 * If the next token is a given keyword, return "true" after advancing the lexer.
1449 * Otherwise, do not change the parser state and return "false".
1450 */
1451 ;
1452
1453 _proto.expectOptionalKeyword = function expectOptionalKeyword(value) {
1454 var token = this._lexer.token;
1455
1456 if (token.kind === _tokenKind.TokenKind.NAME && token.value === value) {
1457 this._lexer.advance();
1458
1459 return true;
1460 }
1461
1462 return false;
1463 }
1464 /**
1465 * Helper function for creating an error when an unexpected lexed token is encountered.
1466 */
1467 ;
1468
1469 _proto.unexpected = function unexpected(atToken) {
1470 var token = atToken !== null && atToken !== void 0 ? atToken : this._lexer.token;
1471 return (0, _syntaxError.syntaxError)(this._lexer.source, token.start, "Unexpected ".concat(getTokenDesc(token), "."));
1472 }
1473 /**
1474 * Returns a possibly empty list of parse nodes, determined by the parseFn.
1475 * This list begins with a lex token of openKind and ends with a lex token of closeKind.
1476 * Advances the parser to the next lex token after the closing token.
1477 */
1478 ;
1479
1480 _proto.any = function any(openKind, parseFn, closeKind) {
1481 this.expectToken(openKind);
1482 var nodes = [];
1483
1484 while (!this.expectOptionalToken(closeKind)) {
1485 nodes.push(parseFn.call(this));
1486 }
1487
1488 return nodes;
1489 }
1490 /**
1491 * Returns a list of parse nodes, determined by the parseFn.
1492 * It can be empty only if open token is missing otherwise it will always return non-empty list
1493 * that begins with a lex token of openKind and ends with a lex token of closeKind.
1494 * Advances the parser to the next lex token after the closing token.
1495 */
1496 ;
1497
1498 _proto.optionalMany = function optionalMany(openKind, parseFn, closeKind) {
1499 if (this.expectOptionalToken(openKind)) {
1500 var nodes = [];
1501
1502 do {
1503 nodes.push(parseFn.call(this));
1504 } while (!this.expectOptionalToken(closeKind));
1505
1506 return nodes;
1507 }
1508
1509 return [];
1510 }
1511 /**
1512 * Returns a non-empty list of parse nodes, determined by the parseFn.
1513 * This list begins with a lex token of openKind and ends with a lex token of closeKind.
1514 * Advances the parser to the next lex token after the closing token.
1515 */
1516 ;
1517
1518 _proto.many = function many(openKind, parseFn, closeKind) {
1519 this.expectToken(openKind);
1520 var nodes = [];
1521
1522 do {
1523 nodes.push(parseFn.call(this));
1524 } while (!this.expectOptionalToken(closeKind));
1525
1526 return nodes;
1527 }
1528 /**
1529 * Returns a non-empty list of parse nodes, determined by the parseFn.
1530 * This list may begin with a lex token of delimiterKind followed by items separated by lex tokens of tokenKind.
1531 * Advances the parser to the next lex token after last item in the list.
1532 */
1533 ;
1534
1535 _proto.delimitedMany = function delimitedMany(delimiterKind, parseFn) {
1536 this.expectOptionalToken(delimiterKind);
1537 var nodes = [];
1538
1539 do {
1540 nodes.push(parseFn.call(this));
1541 } while (this.expectOptionalToken(delimiterKind));
1542
1543 return nodes;
1544 };
1545
1546 return Parser;
1547}();
1548/**
1549 * A helper function to describe a token as a string for debugging.
1550 */
1551
1552
1553exports.Parser = Parser;
1554
1555function getTokenDesc(token) {
1556 var value = token.value;
1557 return getTokenKindDesc(token.kind) + (value != null ? " \"".concat(value, "\"") : '');
1558}
1559/**
1560 * A helper function to describe a token kind as a string for debugging.
1561 */
1562
1563
1564function getTokenKindDesc(kind) {
1565 return (0, _lexer.isPunctuatorTokenKind)(kind) ? "\"".concat(kind, "\"") : kind;
1566}