ast.mjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * Contains a range of UTF-8 character offsets and token references that
  3. * identify the region of the source from which the AST derived.
  4. */
  5. export class Location {
  6. /**
  7. * The character offset at which this Node begins.
  8. */
  9. /**
  10. * The character offset at which this Node ends.
  11. */
  12. /**
  13. * The Token at which this Node begins.
  14. */
  15. /**
  16. * The Token at which this Node ends.
  17. */
  18. /**
  19. * The Source document the AST represents.
  20. */
  21. constructor(startToken, endToken, source) {
  22. this.start = startToken.start;
  23. this.end = endToken.end;
  24. this.startToken = startToken;
  25. this.endToken = endToken;
  26. this.source = source;
  27. }
  28. get [Symbol.toStringTag]() {
  29. return 'Location';
  30. }
  31. toJSON() {
  32. return {
  33. start: this.start,
  34. end: this.end,
  35. };
  36. }
  37. }
  38. /**
  39. * Represents a range of characters represented by a lexical token
  40. * within a Source.
  41. */
  42. export class Token {
  43. /**
  44. * The kind of Token.
  45. */
  46. /**
  47. * The character offset at which this Node begins.
  48. */
  49. /**
  50. * The character offset at which this Node ends.
  51. */
  52. /**
  53. * The 1-indexed line number on which this Token appears.
  54. */
  55. /**
  56. * The 1-indexed column number at which this Token begins.
  57. */
  58. /**
  59. * For non-punctuation tokens, represents the interpreted value of the token.
  60. *
  61. * Note: is undefined for punctuation tokens, but typed as string for
  62. * convenience in the parser.
  63. */
  64. /**
  65. * Tokens exist as nodes in a double-linked-list amongst all tokens
  66. * including ignored tokens. <SOF> is always the first node and <EOF>
  67. * the last.
  68. */
  69. constructor(kind, start, end, line, column, value) {
  70. this.kind = kind;
  71. this.start = start;
  72. this.end = end;
  73. this.line = line;
  74. this.column = column; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  75. this.value = value;
  76. this.prev = null;
  77. this.next = null;
  78. }
  79. get [Symbol.toStringTag]() {
  80. return 'Token';
  81. }
  82. toJSON() {
  83. return {
  84. kind: this.kind,
  85. value: this.value,
  86. line: this.line,
  87. column: this.column,
  88. };
  89. }
  90. }
  91. /**
  92. * The list of all possible AST node types.
  93. */
  94. /**
  95. * @internal
  96. */
  97. export const QueryDocumentKeys = {
  98. Name: [],
  99. Document: ['definitions'],
  100. OperationDefinition: [
  101. 'name',
  102. 'variableDefinitions',
  103. 'directives',
  104. 'selectionSet',
  105. ],
  106. VariableDefinition: ['variable', 'type', 'defaultValue', 'directives'],
  107. Variable: ['name'],
  108. SelectionSet: ['selections'],
  109. Field: ['alias', 'name', 'arguments', 'directives', 'selectionSet'],
  110. Argument: ['name', 'value'],
  111. FragmentSpread: ['name', 'directives'],
  112. InlineFragment: ['typeCondition', 'directives', 'selectionSet'],
  113. FragmentDefinition: [
  114. 'name', // Note: fragment variable definitions are deprecated and will removed in v17.0.0
  115. 'variableDefinitions',
  116. 'typeCondition',
  117. 'directives',
  118. 'selectionSet',
  119. ],
  120. IntValue: [],
  121. FloatValue: [],
  122. StringValue: [],
  123. BooleanValue: [],
  124. NullValue: [],
  125. EnumValue: [],
  126. ListValue: ['values'],
  127. ObjectValue: ['fields'],
  128. ObjectField: ['name', 'value'],
  129. Directive: ['name', 'arguments'],
  130. NamedType: ['name'],
  131. ListType: ['type'],
  132. NonNullType: ['type'],
  133. SchemaDefinition: ['description', 'directives', 'operationTypes'],
  134. OperationTypeDefinition: ['type'],
  135. ScalarTypeDefinition: ['description', 'name', 'directives'],
  136. ObjectTypeDefinition: [
  137. 'description',
  138. 'name',
  139. 'interfaces',
  140. 'directives',
  141. 'fields',
  142. ],
  143. FieldDefinition: ['description', 'name', 'arguments', 'type', 'directives'],
  144. InputValueDefinition: [
  145. 'description',
  146. 'name',
  147. 'type',
  148. 'defaultValue',
  149. 'directives',
  150. ],
  151. InterfaceTypeDefinition: [
  152. 'description',
  153. 'name',
  154. 'interfaces',
  155. 'directives',
  156. 'fields',
  157. ],
  158. UnionTypeDefinition: ['description', 'name', 'directives', 'types'],
  159. EnumTypeDefinition: ['description', 'name', 'directives', 'values'],
  160. EnumValueDefinition: ['description', 'name', 'directives'],
  161. InputObjectTypeDefinition: ['description', 'name', 'directives', 'fields'],
  162. DirectiveDefinition: ['description', 'name', 'arguments', 'locations'],
  163. SchemaExtension: ['directives', 'operationTypes'],
  164. ScalarTypeExtension: ['name', 'directives'],
  165. ObjectTypeExtension: ['name', 'interfaces', 'directives', 'fields'],
  166. InterfaceTypeExtension: ['name', 'interfaces', 'directives', 'fields'],
  167. UnionTypeExtension: ['name', 'directives', 'types'],
  168. EnumTypeExtension: ['name', 'directives', 'values'],
  169. InputObjectTypeExtension: ['name', 'directives', 'fields'],
  170. };
  171. const kindValues = new Set(Object.keys(QueryDocumentKeys));
  172. /**
  173. * @internal
  174. */
  175. export function isNode(maybeNode) {
  176. const maybeKind =
  177. maybeNode === null || maybeNode === void 0 ? void 0 : maybeNode.kind;
  178. return typeof maybeKind === 'string' && kindValues.has(maybeKind);
  179. }
  180. /** Name */
  181. var OperationTypeNode;
  182. (function (OperationTypeNode) {
  183. OperationTypeNode['QUERY'] = 'query';
  184. OperationTypeNode['MUTATION'] = 'mutation';
  185. OperationTypeNode['SUBSCRIPTION'] = 'subscription';
  186. })(OperationTypeNode || (OperationTypeNode = {}));
  187. export { OperationTypeNode };