ast.d.ts 15 KB

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