UNPKG

17.2 kBTypeScriptView Raw
1import type { Maybe } from '../jsutils/Maybe';
2import type { GraphQLError } from '../error/GraphQLError';
3import type {
4 ArgumentNode,
5 ConstArgumentNode,
6 ConstDirectiveNode,
7 ConstListValueNode,
8 ConstObjectFieldNode,
9 ConstObjectValueNode,
10 ConstValueNode,
11 DefinitionNode,
12 DirectiveDefinitionNode,
13 DirectiveNode,
14 DocumentNode,
15 EnumTypeDefinitionNode,
16 EnumTypeExtensionNode,
17 EnumValueDefinitionNode,
18 FieldDefinitionNode,
19 FieldNode,
20 FragmentDefinitionNode,
21 FragmentSpreadNode,
22 InlineFragmentNode,
23 InputObjectTypeDefinitionNode,
24 InputObjectTypeExtensionNode,
25 InputValueDefinitionNode,
26 InterfaceTypeDefinitionNode,
27 InterfaceTypeExtensionNode,
28 ListValueNode,
29 NamedTypeNode,
30 NameNode,
31 ObjectFieldNode,
32 ObjectTypeDefinitionNode,
33 ObjectTypeExtensionNode,
34 ObjectValueNode,
35 OperationDefinitionNode,
36 OperationTypeDefinitionNode,
37 ScalarTypeDefinitionNode,
38 ScalarTypeExtensionNode,
39 SchemaDefinitionNode,
40 SchemaExtensionNode,
41 SelectionNode,
42 SelectionSetNode,
43 StringValueNode,
44 Token,
45 TypeNode,
46 TypeSystemExtensionNode,
47 UnionTypeDefinitionNode,
48 UnionTypeExtensionNode,
49 ValueNode,
50 VariableDefinitionNode,
51 VariableNode,
52} from './ast';
53import { Location, OperationTypeNode } from './ast';
54import { Lexer } from './lexer';
55import { Source } from './source';
56import { TokenKind } from './tokenKind';
57/**
58 * Configuration options to control parser behavior
59 */
60export interface ParseOptions {
61 /**
62 * By default, the parser creates AST nodes that know the location
63 * in the source that they correspond to. This configuration flag
64 * disables that behavior for performance or testing.
65 */
66 noLocation?: boolean;
67 /**
68 * Parser CPU and memory usage is linear to the number of tokens in a document
69 * however in extreme cases it becomes quadratic due to memory exhaustion.
70 * Parsing happens before validation so even invalid queries can burn lots of
71 * CPU time and memory.
72 * To prevent this you can set a maximum number of tokens allowed within a document.
73 */
74 maxTokens?: number | undefined;
75 /**
76 * @deprecated will be removed in the v17.0.0
77 *
78 * If enabled, the parser will understand and parse variable definitions
79 * contained in a fragment definition. They'll be represented in the
80 * `variableDefinitions` field of the FragmentDefinitionNode.
81 *
82 * The syntax is identical to normal, query-defined variables. For example:
83 *
84 * ```graphql
85 * fragment A($var: Boolean = false) on T {
86 * ...
87 * }
88 * ```
89 */
90 allowLegacyFragmentVariables?: boolean;
91}
92/**
93 * Given a GraphQL source, parses it into a Document.
94 * Throws GraphQLError if a syntax error is encountered.
95 */
96export declare function parse(
97 source: string | Source,
98 options?: ParseOptions | undefined,
99): DocumentNode;
100/**
101 * Given a string containing a GraphQL value (ex. `[42]`), parse the AST for
102 * that value.
103 * Throws GraphQLError if a syntax error is encountered.
104 *
105 * This is useful within tools that operate upon GraphQL Values directly and
106 * in isolation of complete GraphQL documents.
107 *
108 * Consider providing the results to the utility function: valueFromAST().
109 */
110export declare function parseValue(
111 source: string | Source,
112 options?: ParseOptions | undefined,
113): ValueNode;
114/**
115 * Similar to parseValue(), but raises a parse error if it encounters a
116 * variable. The return type will be a constant value.
117 */
118export declare function parseConstValue(
119 source: string | Source,
120 options?: ParseOptions | undefined,
121): ConstValueNode;
122/**
123 * Given a string containing a GraphQL Type (ex. `[Int!]`), parse the AST for
124 * that type.
125 * Throws GraphQLError if a syntax error is encountered.
126 *
127 * This is useful within tools that operate upon GraphQL Types directly and
128 * in isolation of complete GraphQL documents.
129 *
130 * Consider providing the results to the utility function: typeFromAST().
131 */
132export declare function parseType(
133 source: string | Source,
134 options?: ParseOptions | undefined,
135): TypeNode;
136/**
137 * This class is exported only to assist people in implementing their own parsers
138 * without duplicating too much code and should be used only as last resort for cases
139 * such as experimental syntax or if certain features could not be contributed upstream.
140 *
141 * It is still part of the internal API and is versioned, so any changes to it are never
142 * considered breaking changes. If you still need to support multiple versions of the
143 * library, please use the `versionInfo` variable for version detection.
144 *
145 * @internal
146 */
147export declare class Parser {
148 protected _options: ParseOptions;
149 protected _lexer: Lexer;
150 protected _tokenCounter: number;
151 constructor(source: string | Source, options?: ParseOptions);
152 get tokenCount(): number;
153 /**
154 * Converts a name lex token into a name parse node.
155 */
156 parseName(): NameNode;
157 /**
158 * Document : Definition+
159 */
160 parseDocument(): DocumentNode;
161 /**
162 * Definition :
163 * - ExecutableDefinition
164 * - TypeSystemDefinition
165 * - TypeSystemExtension
166 *
167 * ExecutableDefinition :
168 * - OperationDefinition
169 * - FragmentDefinition
170 *
171 * TypeSystemDefinition :
172 * - SchemaDefinition
173 * - TypeDefinition
174 * - DirectiveDefinition
175 *
176 * TypeDefinition :
177 * - ScalarTypeDefinition
178 * - ObjectTypeDefinition
179 * - InterfaceTypeDefinition
180 * - UnionTypeDefinition
181 * - EnumTypeDefinition
182 * - InputObjectTypeDefinition
183 */
184 parseDefinition(): DefinitionNode;
185 /**
186 * OperationDefinition :
187 * - SelectionSet
188 * - OperationType Name? VariableDefinitions? Directives? SelectionSet
189 */
190 parseOperationDefinition(): OperationDefinitionNode;
191 /**
192 * OperationType : one of query mutation subscription
193 */
194 parseOperationType(): OperationTypeNode;
195 /**
196 * VariableDefinitions : ( VariableDefinition+ )
197 */
198 parseVariableDefinitions(): Array<VariableDefinitionNode>;
199 /**
200 * VariableDefinition : Variable : Type DefaultValue? Directives[Const]?
201 */
202 parseVariableDefinition(): VariableDefinitionNode;
203 /**
204 * Variable : $ Name
205 */
206 parseVariable(): VariableNode;
207 /**
208 * ```
209 * SelectionSet : { Selection+ }
210 * ```
211 */
212 parseSelectionSet(): SelectionSetNode;
213 /**
214 * Selection :
215 * - Field
216 * - FragmentSpread
217 * - InlineFragment
218 */
219 parseSelection(): SelectionNode;
220 /**
221 * Field : Alias? Name Arguments? Directives? SelectionSet?
222 *
223 * Alias : Name :
224 */
225 parseField(): FieldNode;
226 /**
227 * Arguments[Const] : ( Argument[?Const]+ )
228 */
229 parseArguments(isConst: true): Array<ConstArgumentNode>;
230 parseArguments(isConst: boolean): Array<ArgumentNode>;
231 /**
232 * Argument[Const] : Name : Value[?Const]
233 */
234 parseArgument(isConst: true): ConstArgumentNode;
235 parseArgument(isConst?: boolean): ArgumentNode;
236 parseConstArgument(): ConstArgumentNode;
237 /**
238 * Corresponds to both FragmentSpread and InlineFragment in the spec.
239 *
240 * FragmentSpread : ... FragmentName Directives?
241 *
242 * InlineFragment : ... TypeCondition? Directives? SelectionSet
243 */
244 parseFragment(): FragmentSpreadNode | InlineFragmentNode;
245 /**
246 * FragmentDefinition :
247 * - fragment FragmentName on TypeCondition Directives? SelectionSet
248 *
249 * TypeCondition : NamedType
250 */
251 parseFragmentDefinition(): FragmentDefinitionNode;
252 /**
253 * FragmentName : Name but not `on`
254 */
255 parseFragmentName(): NameNode;
256 /**
257 * Value[Const] :
258 * - [~Const] Variable
259 * - IntValue
260 * - FloatValue
261 * - StringValue
262 * - BooleanValue
263 * - NullValue
264 * - EnumValue
265 * - ListValue[?Const]
266 * - ObjectValue[?Const]
267 *
268 * BooleanValue : one of `true` `false`
269 *
270 * NullValue : `null`
271 *
272 * EnumValue : Name but not `true`, `false` or `null`
273 */
274 parseValueLiteral(isConst: true): ConstValueNode;
275 parseValueLiteral(isConst: boolean): ValueNode;
276 parseConstValueLiteral(): ConstValueNode;
277 parseStringLiteral(): StringValueNode;
278 /**
279 * ListValue[Const] :
280 * - [ ]
281 * - [ Value[?Const]+ ]
282 */
283 parseList(isConst: true): ConstListValueNode;
284 parseList(isConst: boolean): ListValueNode;
285 /**
286 * ```
287 * ObjectValue[Const] :
288 * - { }
289 * - { ObjectField[?Const]+ }
290 * ```
291 */
292 parseObject(isConst: true): ConstObjectValueNode;
293 parseObject(isConst: boolean): ObjectValueNode;
294 /**
295 * ObjectField[Const] : Name : Value[?Const]
296 */
297 parseObjectField(isConst: true): ConstObjectFieldNode;
298 parseObjectField(isConst: boolean): ObjectFieldNode;
299 /**
300 * Directives[Const] : Directive[?Const]+
301 */
302 parseDirectives(isConst: true): Array<ConstDirectiveNode>;
303 parseDirectives(isConst: boolean): Array<DirectiveNode>;
304 parseConstDirectives(): Array<ConstDirectiveNode>;
305 /**
306 * ```
307 * Directive[Const] : @ Name Arguments[?Const]?
308 * ```
309 */
310 parseDirective(isConst: true): ConstDirectiveNode;
311 parseDirective(isConst: boolean): DirectiveNode;
312 /**
313 * Type :
314 * - NamedType
315 * - ListType
316 * - NonNullType
317 */
318 parseTypeReference(): TypeNode;
319 /**
320 * NamedType : Name
321 */
322 parseNamedType(): NamedTypeNode;
323 peekDescription(): boolean;
324 /**
325 * Description : StringValue
326 */
327 parseDescription(): undefined | StringValueNode;
328 /**
329 * ```
330 * SchemaDefinition : Description? schema Directives[Const]? { OperationTypeDefinition+ }
331 * ```
332 */
333 parseSchemaDefinition(): SchemaDefinitionNode;
334 /**
335 * OperationTypeDefinition : OperationType : NamedType
336 */
337 parseOperationTypeDefinition(): OperationTypeDefinitionNode;
338 /**
339 * ScalarTypeDefinition : Description? scalar Name Directives[Const]?
340 */
341 parseScalarTypeDefinition(): ScalarTypeDefinitionNode;
342 /**
343 * ObjectTypeDefinition :
344 * Description?
345 * type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition?
346 */
347 parseObjectTypeDefinition(): ObjectTypeDefinitionNode;
348 /**
349 * ImplementsInterfaces :
350 * - implements `&`? NamedType
351 * - ImplementsInterfaces & NamedType
352 */
353 parseImplementsInterfaces(): Array<NamedTypeNode>;
354 /**
355 * ```
356 * FieldsDefinition : { FieldDefinition+ }
357 * ```
358 */
359 parseFieldsDefinition(): Array<FieldDefinitionNode>;
360 /**
361 * FieldDefinition :
362 * - Description? Name ArgumentsDefinition? : Type Directives[Const]?
363 */
364 parseFieldDefinition(): FieldDefinitionNode;
365 /**
366 * ArgumentsDefinition : ( InputValueDefinition+ )
367 */
368 parseArgumentDefs(): Array<InputValueDefinitionNode>;
369 /**
370 * InputValueDefinition :
371 * - Description? Name : Type DefaultValue? Directives[Const]?
372 */
373 parseInputValueDef(): InputValueDefinitionNode;
374 /**
375 * InterfaceTypeDefinition :
376 * - Description? interface Name Directives[Const]? FieldsDefinition?
377 */
378 parseInterfaceTypeDefinition(): InterfaceTypeDefinitionNode;
379 /**
380 * UnionTypeDefinition :
381 * - Description? union Name Directives[Const]? UnionMemberTypes?
382 */
383 parseUnionTypeDefinition(): UnionTypeDefinitionNode;
384 /**
385 * UnionMemberTypes :
386 * - = `|`? NamedType
387 * - UnionMemberTypes | NamedType
388 */
389 parseUnionMemberTypes(): Array<NamedTypeNode>;
390 /**
391 * EnumTypeDefinition :
392 * - Description? enum Name Directives[Const]? EnumValuesDefinition?
393 */
394 parseEnumTypeDefinition(): EnumTypeDefinitionNode;
395 /**
396 * ```
397 * EnumValuesDefinition : { EnumValueDefinition+ }
398 * ```
399 */
400 parseEnumValuesDefinition(): Array<EnumValueDefinitionNode>;
401 /**
402 * EnumValueDefinition : Description? EnumValue Directives[Const]?
403 */
404 parseEnumValueDefinition(): EnumValueDefinitionNode;
405 /**
406 * EnumValue : Name but not `true`, `false` or `null`
407 */
408 parseEnumValueName(): NameNode;
409 /**
410 * InputObjectTypeDefinition :
411 * - Description? input Name Directives[Const]? InputFieldsDefinition?
412 */
413 parseInputObjectTypeDefinition(): InputObjectTypeDefinitionNode;
414 /**
415 * ```
416 * InputFieldsDefinition : { InputValueDefinition+ }
417 * ```
418 */
419 parseInputFieldsDefinition(): Array<InputValueDefinitionNode>;
420 /**
421 * TypeSystemExtension :
422 * - SchemaExtension
423 * - TypeExtension
424 *
425 * TypeExtension :
426 * - ScalarTypeExtension
427 * - ObjectTypeExtension
428 * - InterfaceTypeExtension
429 * - UnionTypeExtension
430 * - EnumTypeExtension
431 * - InputObjectTypeDefinition
432 */
433 parseTypeSystemExtension(): TypeSystemExtensionNode;
434 /**
435 * ```
436 * SchemaExtension :
437 * - extend schema Directives[Const]? { OperationTypeDefinition+ }
438 * - extend schema Directives[Const]
439 * ```
440 */
441 parseSchemaExtension(): SchemaExtensionNode;
442 /**
443 * ScalarTypeExtension :
444 * - extend scalar Name Directives[Const]
445 */
446 parseScalarTypeExtension(): ScalarTypeExtensionNode;
447 /**
448 * ObjectTypeExtension :
449 * - extend type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
450 * - extend type Name ImplementsInterfaces? Directives[Const]
451 * - extend type Name ImplementsInterfaces
452 */
453 parseObjectTypeExtension(): ObjectTypeExtensionNode;
454 /**
455 * InterfaceTypeExtension :
456 * - extend interface Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
457 * - extend interface Name ImplementsInterfaces? Directives[Const]
458 * - extend interface Name ImplementsInterfaces
459 */
460 parseInterfaceTypeExtension(): InterfaceTypeExtensionNode;
461 /**
462 * UnionTypeExtension :
463 * - extend union Name Directives[Const]? UnionMemberTypes
464 * - extend union Name Directives[Const]
465 */
466 parseUnionTypeExtension(): UnionTypeExtensionNode;
467 /**
468 * EnumTypeExtension :
469 * - extend enum Name Directives[Const]? EnumValuesDefinition
470 * - extend enum Name Directives[Const]
471 */
472 parseEnumTypeExtension(): EnumTypeExtensionNode;
473 /**
474 * InputObjectTypeExtension :
475 * - extend input Name Directives[Const]? InputFieldsDefinition
476 * - extend input Name Directives[Const]
477 */
478 parseInputObjectTypeExtension(): InputObjectTypeExtensionNode;
479 /**
480 * ```
481 * DirectiveDefinition :
482 * - Description? directive @ Name ArgumentsDefinition? `repeatable`? on DirectiveLocations
483 * ```
484 */
485 parseDirectiveDefinition(): DirectiveDefinitionNode;
486 /**
487 * DirectiveLocations :
488 * - `|`? DirectiveLocation
489 * - DirectiveLocations | DirectiveLocation
490 */
491 parseDirectiveLocations(): Array<NameNode>;
492 parseDirectiveLocation(): NameNode;
493 /**
494 * Returns a node that, if configured to do so, sets a "loc" field as a
495 * location object, used to identify the place in the source that created a
496 * given parsed object.
497 */
498 node<
499 T extends {
500 loc?: Location;
501 },
502 >(startToken: Token, node: T): T;
503 /**
504 * Determines if the next token is of a given kind
505 */
506 peek(kind: TokenKind): boolean;
507 /**
508 * If the next token is of the given kind, return that token after advancing the lexer.
509 * Otherwise, do not change the parser state and throw an error.
510 */
511 expectToken(kind: TokenKind): Token;
512 /**
513 * If the next token is of the given kind, return "true" after advancing the lexer.
514 * Otherwise, do not change the parser state and return "false".
515 */
516 expectOptionalToken(kind: TokenKind): boolean;
517 /**
518 * If the next token is a given keyword, advance the lexer.
519 * Otherwise, do not change the parser state and throw an error.
520 */
521 expectKeyword(value: string): void;
522 /**
523 * If the next token is a given keyword, return "true" after advancing the lexer.
524 * Otherwise, do not change the parser state and return "false".
525 */
526 expectOptionalKeyword(value: string): boolean;
527 /**
528 * Helper function for creating an error when an unexpected lexed token is encountered.
529 */
530 unexpected(atToken?: Maybe<Token>): GraphQLError;
531 /**
532 * Returns a possibly empty list of parse nodes, determined by the parseFn.
533 * This list begins with a lex token of openKind and ends with a lex token of closeKind.
534 * Advances the parser to the next lex token after the closing token.
535 */
536 any<T>(openKind: TokenKind, parseFn: () => T, closeKind: TokenKind): Array<T>;
537 /**
538 * Returns a list of parse nodes, determined by the parseFn.
539 * It can be empty only if open token is missing otherwise it will always return non-empty list
540 * that begins with a lex token of openKind and ends with a lex token of closeKind.
541 * Advances the parser to the next lex token after the closing token.
542 */
543 optionalMany<T>(
544 openKind: TokenKind,
545 parseFn: () => T,
546 closeKind: TokenKind,
547 ): Array<T>;
548 /**
549 * Returns a non-empty list of parse nodes, determined by the parseFn.
550 * This list begins with a lex token of openKind and ends with a lex token of closeKind.
551 * Advances the parser to the next lex token after the closing token.
552 */
553 many<T>(
554 openKind: TokenKind,
555 parseFn: () => T,
556 closeKind: TokenKind,
557 ): Array<T>;
558 /**
559 * Returns a non-empty list of parse nodes, determined by the parseFn.
560 * This list may begin with a lex token of delimiterKind followed by items separated by lex tokens of tokenKind.
561 * Advances the parser to the next lex token after last item in the list.
562 */
563 delimitedMany<T>(delimiterKind: TokenKind, parseFn: () => T): Array<T>;
564 advanceLexer(): void;
565}
566
\No newline at end of file