UNPKG

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