ast.d.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. import { Source } from './source';
  2. import { TokenKindEnum } from './tokenKind';
  3. /**
  4. * Contains a range of UTF-8 character offsets and token references that
  5. * identify the region of the source from which the AST derived.
  6. */
  7. export class Location {
  8. /**
  9. * The character offset at which this Node begins.
  10. */
  11. readonly start: number;
  12. /**
  13. * The character offset at which this Node ends.
  14. */
  15. readonly end: number;
  16. /**
  17. * The Token at which this Node begins.
  18. */
  19. readonly startToken: Token;
  20. /**
  21. * The Token at which this Node ends.
  22. */
  23. readonly endToken: Token;
  24. /**
  25. * The Source document the AST represents.
  26. */
  27. readonly source: Source;
  28. constructor(startToken: Token, endToken: Token, source: Source);
  29. toJSON(): { start: number; end: number };
  30. }
  31. /**
  32. * Represents a range of characters represented by a lexical token
  33. * within a Source.
  34. */
  35. export class Token {
  36. /**
  37. * The kind of Token.
  38. */
  39. readonly kind: TokenKindEnum;
  40. /**
  41. * The character offset at which this Node begins.
  42. */
  43. readonly start: number;
  44. /**
  45. * The character offset at which this Node ends.
  46. */
  47. readonly end: number;
  48. /**
  49. * The 1-indexed line number on which this Token appears.
  50. */
  51. readonly line: number;
  52. /**
  53. * The 1-indexed column number at which this Token begins.
  54. */
  55. readonly column: number;
  56. /**
  57. * For non-punctuation tokens, represents the interpreted value of the token.
  58. */
  59. readonly value: string | undefined;
  60. /**
  61. * Tokens exist as nodes in a double-linked-list amongst all tokens
  62. * including ignored tokens. <SOF> is always the first node and <EOF>
  63. * the last.
  64. */
  65. readonly prev: Token | null;
  66. readonly next: Token | null;
  67. constructor(
  68. kind: TokenKindEnum,
  69. start: number,
  70. end: number,
  71. line: number,
  72. column: number,
  73. prev: Token | null,
  74. value?: string,
  75. );
  76. toJSON(): {
  77. kind: TokenKindEnum;
  78. value: string | undefined;
  79. line: number;
  80. column: number;
  81. };
  82. }
  83. /**
  84. * @internal
  85. */
  86. export function isNode(maybeNode: any): maybeNode is ASTNode;
  87. /**
  88. * The list of all possible AST node types.
  89. */
  90. export type ASTNode =
  91. | NameNode
  92. | DocumentNode
  93. | OperationDefinitionNode
  94. | VariableDefinitionNode
  95. | VariableNode
  96. | SelectionSetNode
  97. | FieldNode
  98. | ArgumentNode
  99. | FragmentSpreadNode
  100. | InlineFragmentNode
  101. | FragmentDefinitionNode
  102. | IntValueNode
  103. | FloatValueNode
  104. | StringValueNode
  105. | BooleanValueNode
  106. | NullValueNode
  107. | EnumValueNode
  108. | ListValueNode
  109. | ObjectValueNode
  110. | ObjectFieldNode
  111. | DirectiveNode
  112. | NamedTypeNode
  113. | ListTypeNode
  114. | NonNullTypeNode
  115. | SchemaDefinitionNode
  116. | OperationTypeDefinitionNode
  117. | ScalarTypeDefinitionNode
  118. | ObjectTypeDefinitionNode
  119. | FieldDefinitionNode
  120. | InputValueDefinitionNode
  121. | InterfaceTypeDefinitionNode
  122. | UnionTypeDefinitionNode
  123. | EnumTypeDefinitionNode
  124. | EnumValueDefinitionNode
  125. | InputObjectTypeDefinitionNode
  126. | DirectiveDefinitionNode
  127. | SchemaExtensionNode
  128. | ScalarTypeExtensionNode
  129. | ObjectTypeExtensionNode
  130. | InterfaceTypeExtensionNode
  131. | UnionTypeExtensionNode
  132. | EnumTypeExtensionNode
  133. | InputObjectTypeExtensionNode;
  134. /**
  135. * Utility type listing all nodes indexed by their kind.
  136. */
  137. export interface ASTKindToNode {
  138. Name: NameNode;
  139. Document: DocumentNode;
  140. OperationDefinition: OperationDefinitionNode;
  141. VariableDefinition: VariableDefinitionNode;
  142. Variable: VariableNode;
  143. SelectionSet: SelectionSetNode;
  144. Field: FieldNode;
  145. Argument: ArgumentNode;
  146. FragmentSpread: FragmentSpreadNode;
  147. InlineFragment: InlineFragmentNode;
  148. FragmentDefinition: FragmentDefinitionNode;
  149. IntValue: IntValueNode;
  150. FloatValue: FloatValueNode;
  151. StringValue: StringValueNode;
  152. BooleanValue: BooleanValueNode;
  153. NullValue: NullValueNode;
  154. EnumValue: EnumValueNode;
  155. ListValue: ListValueNode;
  156. ObjectValue: ObjectValueNode;
  157. ObjectField: ObjectFieldNode;
  158. Directive: DirectiveNode;
  159. NamedType: NamedTypeNode;
  160. ListType: ListTypeNode;
  161. NonNullType: NonNullTypeNode;
  162. SchemaDefinition: SchemaDefinitionNode;
  163. OperationTypeDefinition: OperationTypeDefinitionNode;
  164. ScalarTypeDefinition: ScalarTypeDefinitionNode;
  165. ObjectTypeDefinition: ObjectTypeDefinitionNode;
  166. FieldDefinition: FieldDefinitionNode;
  167. InputValueDefinition: InputValueDefinitionNode;
  168. InterfaceTypeDefinition: InterfaceTypeDefinitionNode;
  169. UnionTypeDefinition: UnionTypeDefinitionNode;
  170. EnumTypeDefinition: EnumTypeDefinitionNode;
  171. EnumValueDefinition: EnumValueDefinitionNode;
  172. InputObjectTypeDefinition: InputObjectTypeDefinitionNode;
  173. DirectiveDefinition: DirectiveDefinitionNode;
  174. SchemaExtension: SchemaExtensionNode;
  175. ScalarTypeExtension: ScalarTypeExtensionNode;
  176. ObjectTypeExtension: ObjectTypeExtensionNode;
  177. InterfaceTypeExtension: InterfaceTypeExtensionNode;
  178. UnionTypeExtension: UnionTypeExtensionNode;
  179. EnumTypeExtension: EnumTypeExtensionNode;
  180. InputObjectTypeExtension: InputObjectTypeExtensionNode;
  181. }
  182. // Name
  183. export interface NameNode {
  184. readonly kind: 'Name';
  185. readonly loc?: Location;
  186. readonly value: string;
  187. }
  188. // Document
  189. export interface DocumentNode {
  190. readonly kind: 'Document';
  191. readonly loc?: Location;
  192. readonly definitions: ReadonlyArray<DefinitionNode>;
  193. }
  194. export type DefinitionNode =
  195. | ExecutableDefinitionNode
  196. | TypeSystemDefinitionNode
  197. | TypeSystemExtensionNode;
  198. export type ExecutableDefinitionNode =
  199. | OperationDefinitionNode
  200. | FragmentDefinitionNode;
  201. export interface OperationDefinitionNode {
  202. readonly kind: 'OperationDefinition';
  203. readonly loc?: Location;
  204. readonly operation: OperationTypeNode;
  205. readonly name?: NameNode;
  206. readonly variableDefinitions?: ReadonlyArray<VariableDefinitionNode>;
  207. readonly directives?: ReadonlyArray<DirectiveNode>;
  208. readonly selectionSet: SelectionSetNode;
  209. }
  210. export type OperationTypeNode = 'query' | 'mutation' | 'subscription';
  211. export interface VariableDefinitionNode {
  212. readonly kind: 'VariableDefinition';
  213. readonly loc?: Location;
  214. readonly variable: VariableNode;
  215. readonly type: TypeNode;
  216. readonly defaultValue?: ValueNode;
  217. readonly directives?: ReadonlyArray<DirectiveNode>;
  218. }
  219. export interface VariableNode {
  220. readonly kind: 'Variable';
  221. readonly loc?: Location;
  222. readonly name: NameNode;
  223. }
  224. export interface SelectionSetNode {
  225. kind: 'SelectionSet';
  226. loc?: Location;
  227. selections: ReadonlyArray<SelectionNode>;
  228. }
  229. export type SelectionNode = FieldNode | FragmentSpreadNode | InlineFragmentNode;
  230. export interface FieldNode {
  231. readonly kind: 'Field';
  232. readonly loc?: Location;
  233. readonly alias?: NameNode;
  234. readonly name: NameNode;
  235. readonly arguments?: ReadonlyArray<ArgumentNode>;
  236. readonly directives?: ReadonlyArray<DirectiveNode>;
  237. readonly selectionSet?: SelectionSetNode;
  238. }
  239. export interface ArgumentNode {
  240. readonly kind: 'Argument';
  241. readonly loc?: Location;
  242. readonly name: NameNode;
  243. readonly value: ValueNode;
  244. }
  245. // Fragments
  246. export interface FragmentSpreadNode {
  247. readonly kind: 'FragmentSpread';
  248. readonly loc?: Location;
  249. readonly name: NameNode;
  250. readonly directives?: ReadonlyArray<DirectiveNode>;
  251. }
  252. export interface InlineFragmentNode {
  253. readonly kind: 'InlineFragment';
  254. readonly loc?: Location;
  255. readonly typeCondition?: NamedTypeNode;
  256. readonly directives?: ReadonlyArray<DirectiveNode>;
  257. readonly selectionSet: SelectionSetNode;
  258. }
  259. export interface FragmentDefinitionNode {
  260. readonly kind: 'FragmentDefinition';
  261. readonly loc?: Location;
  262. readonly name: NameNode;
  263. // Note: fragment variable definitions are experimental and may be changed
  264. // or removed in the future.
  265. readonly variableDefinitions?: ReadonlyArray<VariableDefinitionNode>;
  266. readonly typeCondition: NamedTypeNode;
  267. readonly directives?: ReadonlyArray<DirectiveNode>;
  268. readonly selectionSet: SelectionSetNode;
  269. }
  270. // Values
  271. export type ValueNode =
  272. | VariableNode
  273. | IntValueNode
  274. | FloatValueNode
  275. | StringValueNode
  276. | BooleanValueNode
  277. | NullValueNode
  278. | EnumValueNode
  279. | ListValueNode
  280. | ObjectValueNode;
  281. export interface IntValueNode {
  282. readonly kind: 'IntValue';
  283. readonly loc?: Location;
  284. readonly value: string;
  285. }
  286. export interface FloatValueNode {
  287. readonly kind: 'FloatValue';
  288. readonly loc?: Location;
  289. readonly value: string;
  290. }
  291. export interface StringValueNode {
  292. readonly kind: 'StringValue';
  293. readonly loc?: Location;
  294. readonly value: string;
  295. readonly block?: boolean;
  296. }
  297. export interface BooleanValueNode {
  298. readonly kind: 'BooleanValue';
  299. readonly loc?: Location;
  300. readonly value: boolean;
  301. }
  302. export interface NullValueNode {
  303. readonly kind: 'NullValue';
  304. readonly loc?: Location;
  305. }
  306. export interface EnumValueNode {
  307. readonly kind: 'EnumValue';
  308. readonly loc?: Location;
  309. readonly value: string;
  310. }
  311. export interface ListValueNode {
  312. readonly kind: 'ListValue';
  313. readonly loc?: Location;
  314. readonly values: ReadonlyArray<ValueNode>;
  315. }
  316. export interface ObjectValueNode {
  317. readonly kind: 'ObjectValue';
  318. readonly loc?: Location;
  319. readonly fields: ReadonlyArray<ObjectFieldNode>;
  320. }
  321. export interface ObjectFieldNode {
  322. readonly kind: 'ObjectField';
  323. readonly loc?: Location;
  324. readonly name: NameNode;
  325. readonly value: ValueNode;
  326. }
  327. // Directives
  328. export interface DirectiveNode {
  329. readonly kind: 'Directive';
  330. readonly loc?: Location;
  331. readonly name: NameNode;
  332. readonly arguments?: ReadonlyArray<ArgumentNode>;
  333. }
  334. // Type Reference
  335. export type TypeNode = NamedTypeNode | ListTypeNode | NonNullTypeNode;
  336. export interface NamedTypeNode {
  337. readonly kind: 'NamedType';
  338. readonly loc?: Location;
  339. readonly name: NameNode;
  340. }
  341. export interface ListTypeNode {
  342. readonly kind: 'ListType';
  343. readonly loc?: Location;
  344. readonly type: TypeNode;
  345. }
  346. export interface NonNullTypeNode {
  347. readonly kind: 'NonNullType';
  348. readonly loc?: Location;
  349. readonly type: NamedTypeNode | ListTypeNode;
  350. }
  351. // Type System Definition
  352. export type TypeSystemDefinitionNode =
  353. | SchemaDefinitionNode
  354. | TypeDefinitionNode
  355. | DirectiveDefinitionNode;
  356. export interface SchemaDefinitionNode {
  357. readonly kind: 'SchemaDefinition';
  358. readonly loc?: Location;
  359. readonly description?: StringValueNode;
  360. readonly directives?: ReadonlyArray<DirectiveNode>;
  361. readonly operationTypes: ReadonlyArray<OperationTypeDefinitionNode>;
  362. }
  363. export interface OperationTypeDefinitionNode {
  364. readonly kind: 'OperationTypeDefinition';
  365. readonly loc?: Location;
  366. readonly operation: OperationTypeNode;
  367. readonly type: NamedTypeNode;
  368. }
  369. // Type Definition
  370. export type TypeDefinitionNode =
  371. | ScalarTypeDefinitionNode
  372. | ObjectTypeDefinitionNode
  373. | InterfaceTypeDefinitionNode
  374. | UnionTypeDefinitionNode
  375. | EnumTypeDefinitionNode
  376. | InputObjectTypeDefinitionNode;
  377. export interface ScalarTypeDefinitionNode {
  378. readonly kind: 'ScalarTypeDefinition';
  379. readonly loc?: Location;
  380. readonly description?: StringValueNode;
  381. readonly name: NameNode;
  382. readonly directives?: ReadonlyArray<DirectiveNode>;
  383. }
  384. export interface ObjectTypeDefinitionNode {
  385. readonly kind: 'ObjectTypeDefinition';
  386. readonly loc?: Location;
  387. readonly description?: StringValueNode;
  388. readonly name: NameNode;
  389. readonly interfaces?: ReadonlyArray<NamedTypeNode>;
  390. readonly directives?: ReadonlyArray<DirectiveNode>;
  391. readonly fields?: ReadonlyArray<FieldDefinitionNode>;
  392. }
  393. export interface FieldDefinitionNode {
  394. readonly kind: 'FieldDefinition';
  395. readonly loc?: Location;
  396. readonly description?: StringValueNode;
  397. readonly name: NameNode;
  398. readonly arguments?: ReadonlyArray<InputValueDefinitionNode>;
  399. readonly type: TypeNode;
  400. readonly directives?: ReadonlyArray<DirectiveNode>;
  401. }
  402. export interface InputValueDefinitionNode {
  403. readonly kind: 'InputValueDefinition';
  404. readonly loc?: Location;
  405. readonly description?: StringValueNode;
  406. readonly name: NameNode;
  407. readonly type: TypeNode;
  408. readonly defaultValue?: ValueNode;
  409. readonly directives?: ReadonlyArray<DirectiveNode>;
  410. }
  411. export interface InterfaceTypeDefinitionNode {
  412. readonly kind: 'InterfaceTypeDefinition';
  413. readonly loc?: Location;
  414. readonly description?: StringValueNode;
  415. readonly name: NameNode;
  416. readonly interfaces?: ReadonlyArray<NamedTypeNode>;
  417. readonly directives?: ReadonlyArray<DirectiveNode>;
  418. readonly fields?: ReadonlyArray<FieldDefinitionNode>;
  419. }
  420. export interface UnionTypeDefinitionNode {
  421. readonly kind: 'UnionTypeDefinition';
  422. readonly loc?: Location;
  423. readonly description?: StringValueNode;
  424. readonly name: NameNode;
  425. readonly directives?: ReadonlyArray<DirectiveNode>;
  426. readonly types?: ReadonlyArray<NamedTypeNode>;
  427. }
  428. export interface EnumTypeDefinitionNode {
  429. readonly kind: 'EnumTypeDefinition';
  430. readonly loc?: Location;
  431. readonly description?: StringValueNode;
  432. readonly name: NameNode;
  433. readonly directives?: ReadonlyArray<DirectiveNode>;
  434. readonly values?: ReadonlyArray<EnumValueDefinitionNode>;
  435. }
  436. export interface EnumValueDefinitionNode {
  437. readonly kind: 'EnumValueDefinition';
  438. readonly loc?: Location;
  439. readonly description?: StringValueNode;
  440. readonly name: NameNode;
  441. readonly directives?: ReadonlyArray<DirectiveNode>;
  442. }
  443. export interface InputObjectTypeDefinitionNode {
  444. readonly kind: 'InputObjectTypeDefinition';
  445. readonly loc?: Location;
  446. readonly description?: StringValueNode;
  447. readonly name: NameNode;
  448. readonly directives?: ReadonlyArray<DirectiveNode>;
  449. readonly fields?: ReadonlyArray<InputValueDefinitionNode>;
  450. }
  451. // Directive Definitions
  452. export interface DirectiveDefinitionNode {
  453. readonly kind: 'DirectiveDefinition';
  454. readonly loc?: Location;
  455. readonly description?: StringValueNode;
  456. readonly name: NameNode;
  457. readonly arguments?: ReadonlyArray<InputValueDefinitionNode>;
  458. readonly repeatable: boolean;
  459. readonly locations: ReadonlyArray<NameNode>;
  460. }
  461. // Type System Extensions
  462. export type TypeSystemExtensionNode = SchemaExtensionNode | TypeExtensionNode;
  463. export interface SchemaExtensionNode {
  464. readonly kind: 'SchemaExtension';
  465. readonly loc?: Location;
  466. readonly directives?: ReadonlyArray<DirectiveNode>;
  467. readonly operationTypes?: ReadonlyArray<OperationTypeDefinitionNode>;
  468. }
  469. // Type Extensions
  470. export type TypeExtensionNode =
  471. | ScalarTypeExtensionNode
  472. | ObjectTypeExtensionNode
  473. | InterfaceTypeExtensionNode
  474. | UnionTypeExtensionNode
  475. | EnumTypeExtensionNode
  476. | InputObjectTypeExtensionNode;
  477. export interface ScalarTypeExtensionNode {
  478. readonly kind: 'ScalarTypeExtension';
  479. readonly loc?: Location;
  480. readonly name: NameNode;
  481. readonly directives?: ReadonlyArray<DirectiveNode>;
  482. }
  483. export interface ObjectTypeExtensionNode {
  484. readonly kind: 'ObjectTypeExtension';
  485. readonly loc?: Location;
  486. readonly name: NameNode;
  487. readonly interfaces?: ReadonlyArray<NamedTypeNode>;
  488. readonly directives?: ReadonlyArray<DirectiveNode>;
  489. readonly fields?: ReadonlyArray<FieldDefinitionNode>;
  490. }
  491. export interface InterfaceTypeExtensionNode {
  492. readonly kind: 'InterfaceTypeExtension';
  493. readonly loc?: Location;
  494. readonly name: NameNode;
  495. readonly interfaces?: ReadonlyArray<NamedTypeNode>;
  496. readonly directives?: ReadonlyArray<DirectiveNode>;
  497. readonly fields?: ReadonlyArray<FieldDefinitionNode>;
  498. }
  499. export interface UnionTypeExtensionNode {
  500. readonly kind: 'UnionTypeExtension';
  501. readonly loc?: Location;
  502. readonly name: NameNode;
  503. readonly directives?: ReadonlyArray<DirectiveNode>;
  504. readonly types?: ReadonlyArray<NamedTypeNode>;
  505. }
  506. export interface EnumTypeExtensionNode {
  507. readonly kind: 'EnumTypeExtension';
  508. readonly loc?: Location;
  509. readonly name: NameNode;
  510. readonly directives?: ReadonlyArray<DirectiveNode>;
  511. readonly values?: ReadonlyArray<EnumValueDefinitionNode>;
  512. }
  513. export interface InputObjectTypeExtensionNode {
  514. readonly kind: 'InputObjectTypeExtension';
  515. readonly loc?: Location;
  516. readonly name: NameNode;
  517. readonly directives?: ReadonlyArray<DirectiveNode>;
  518. readonly fields?: ReadonlyArray<InputValueDefinitionNode>;
  519. }