graphql 15.3.0 → 15.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (337) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +10 -0
  3. package/error/GraphQLError.js +8 -8
  4. package/error/GraphQLError.js.flow +2 -3
  5. package/error/GraphQLError.mjs +2 -2
  6. package/error/formatError.js +1 -1
  7. package/error/formatError.js.flow +0 -1
  8. package/error/index.js +4 -4
  9. package/error/index.js.flow +0 -1
  10. package/error/locatedError.d.ts +2 -2
  11. package/error/locatedError.js +10 -5
  12. package/error/locatedError.js.flow +10 -4
  13. package/error/locatedError.mjs +6 -4
  14. package/error/syntaxError.js +1 -1
  15. package/error/syntaxError.js.flow +0 -1
  16. package/execution/execute.d.ts +0 -15
  17. package/execution/execute.js +104 -104
  18. package/execution/execute.js.flow +119 -133
  19. package/execution/execute.mjs +86 -82
  20. package/execution/index.js +3 -3
  21. package/execution/index.js.flow +0 -1
  22. package/execution/values.js +11 -11
  23. package/execution/values.js.flow +0 -1
  24. package/graphql.js +5 -5
  25. package/graphql.js.flow +0 -1
  26. package/index.d.ts +1 -0
  27. package/index.js +9 -9
  28. package/index.js.flow +0 -1
  29. package/jsutils/ObjMap.js.flow +0 -1
  30. package/jsutils/Path.js.flow +0 -1
  31. package/jsutils/PromiseOrValue.js.flow +0 -1
  32. package/jsutils/defineInspect.js +2 -2
  33. package/jsutils/defineInspect.js.flow +0 -1
  34. package/jsutils/devAssert.js.flow +0 -1
  35. package/jsutils/didYouMean.js.flow +0 -1
  36. package/jsutils/identityFunc.js.flow +0 -1
  37. package/jsutils/inspect.js +1 -2
  38. package/jsutils/inspect.js.flow +10 -8
  39. package/jsutils/inspect.mjs +1 -1
  40. package/jsutils/instanceOf.js +15 -9
  41. package/jsutils/instanceOf.js.flow +13 -7
  42. package/jsutils/instanceOf.mjs +13 -5
  43. package/jsutils/invariant.js.flow +0 -1
  44. package/jsutils/isAsyncIterable.js +13 -0
  45. package/jsutils/isAsyncIterable.js.flow +14 -0
  46. package/jsutils/isAsyncIterable.mjs +10 -0
  47. package/jsutils/isObjectLike.js.flow +0 -1
  48. package/jsutils/isPromise.js.flow +0 -1
  49. package/jsutils/keyMap.js.flow +0 -1
  50. package/jsutils/keyValMap.js.flow +0 -1
  51. package/jsutils/mapValue.js +1 -1
  52. package/jsutils/mapValue.js.flow +0 -1
  53. package/jsutils/memoize3.js +2 -5
  54. package/jsutils/memoize3.js.flow +2 -5
  55. package/jsutils/memoize3.mjs +2 -5
  56. package/jsutils/naturalCompare.js +69 -0
  57. package/jsutils/naturalCompare.js.flow +59 -0
  58. package/jsutils/naturalCompare.mjs +61 -0
  59. package/jsutils/nodejsCustomInspectSymbol.js.flow +0 -1
  60. package/jsutils/printPathArray.js.flow +0 -1
  61. package/jsutils/promiseForObject.js.flow +0 -1
  62. package/jsutils/promiseReduce.js +1 -1
  63. package/jsutils/promiseReduce.js.flow +0 -1
  64. package/jsutils/safeArrayFrom.js +73 -0
  65. package/jsutils/safeArrayFrom.js.flow +59 -0
  66. package/jsutils/safeArrayFrom.mjs +66 -0
  67. package/jsutils/suggestionList.js +5 -1
  68. package/jsutils/suggestionList.js.flow +3 -2
  69. package/jsutils/suggestionList.mjs +3 -1
  70. package/jsutils/toObjMap.js +1 -1
  71. package/jsutils/toObjMap.js.flow +0 -1
  72. package/language/ast.js +1 -1
  73. package/language/ast.js.flow +0 -1
  74. package/language/blockString.d.ts +1 -1
  75. package/language/blockString.js +53 -32
  76. package/language/blockString.js.flow +51 -36
  77. package/language/blockString.mjs +54 -33
  78. package/language/directiveLocation.js.flow +0 -1
  79. package/language/experimentalOnlineParser/grammar.d.ts +1006 -0
  80. package/language/experimentalOnlineParser/grammar.js +987 -0
  81. package/language/experimentalOnlineParser/grammar.js.flow +1000 -0
  82. package/language/experimentalOnlineParser/grammar.mjs +980 -0
  83. package/language/experimentalOnlineParser/index.d.ts +6 -0
  84. package/language/experimentalOnlineParser/index.js +31 -0
  85. package/language/experimentalOnlineParser/index.js.flow +7 -0
  86. package/language/experimentalOnlineParser/index.mjs +1 -0
  87. package/language/experimentalOnlineParser/onlineParser.d.ts +125 -0
  88. package/language/experimentalOnlineParser/onlineParser.js +604 -0
  89. package/language/experimentalOnlineParser/onlineParser.js.flow +723 -0
  90. package/language/experimentalOnlineParser/onlineParser.mjs +587 -0
  91. package/language/index.js +12 -12
  92. package/language/index.js.flow +0 -1
  93. package/language/kinds.js.flow +0 -1
  94. package/language/lexer.js +242 -183
  95. package/language/lexer.js.flow +188 -184
  96. package/language/lexer.mjs +238 -179
  97. package/language/location.js.flow +0 -1
  98. package/language/parser.js +73 -68
  99. package/language/parser.js.flow +62 -57
  100. package/language/parser.mjs +64 -60
  101. package/language/predicates.js +1 -1
  102. package/language/predicates.js.flow +0 -1
  103. package/language/printLocation.js +1 -1
  104. package/language/printLocation.js.flow +0 -1
  105. package/language/printer.js +20 -13
  106. package/language/printer.js.flow +26 -25
  107. package/language/printer.mjs +18 -11
  108. package/language/source.d.ts +7 -0
  109. package/language/source.js +20 -3
  110. package/language/source.js.flow +21 -3
  111. package/language/source.mjs +14 -1
  112. package/language/tokenKind.js.flow +0 -1
  113. package/language/visitor.js +2 -2
  114. package/language/visitor.js.flow +0 -1
  115. package/package.json +2 -3
  116. package/polyfills/arrayFrom.js +2 -2
  117. package/polyfills/arrayFrom.js.flow +1 -2
  118. package/polyfills/arrayFrom.mjs +1 -1
  119. package/polyfills/find.js +1 -1
  120. package/polyfills/find.js.flow +1 -2
  121. package/polyfills/find.mjs +1 -1
  122. package/polyfills/isFinite.js +1 -1
  123. package/polyfills/isFinite.js.flow +1 -2
  124. package/polyfills/isFinite.mjs +1 -1
  125. package/polyfills/isInteger.js +1 -1
  126. package/polyfills/isInteger.js.flow +1 -2
  127. package/polyfills/isInteger.mjs +1 -1
  128. package/polyfills/objectEntries.js +1 -1
  129. package/polyfills/objectEntries.js.flow +1 -2
  130. package/polyfills/objectEntries.mjs +1 -1
  131. package/polyfills/objectValues.js +1 -1
  132. package/polyfills/objectValues.js.flow +1 -2
  133. package/polyfills/objectValues.mjs +1 -1
  134. package/polyfills/symbols.js +3 -5
  135. package/polyfills/symbols.js.flow +9 -6
  136. package/polyfills/symbols.mjs +3 -5
  137. package/subscription/index.js +1 -1
  138. package/subscription/index.js.flow +0 -1
  139. package/subscription/mapAsyncIterator.js +2 -2
  140. package/subscription/mapAsyncIterator.js.flow +8 -9
  141. package/subscription/mapAsyncIterator.mjs +1 -1
  142. package/subscription/subscribe.js +61 -73
  143. package/subscription/subscribe.js.flow +71 -85
  144. package/subscription/subscribe.mjs +53 -65
  145. package/type/definition.d.ts +16 -6
  146. package/type/definition.js +37 -32
  147. package/type/definition.js.flow +93 -63
  148. package/type/definition.mjs +19 -14
  149. package/type/directives.js +17 -16
  150. package/type/directives.js.flow +20 -13
  151. package/type/directives.mjs +6 -5
  152. package/type/index.js +6 -6
  153. package/type/index.js.flow +0 -1
  154. package/type/introspection.js +86 -65
  155. package/type/introspection.js.flow +75 -43
  156. package/type/introspection.mjs +79 -58
  157. package/type/scalars.js +9 -9
  158. package/type/scalars.js.flow +1 -2
  159. package/type/scalars.mjs +1 -1
  160. package/type/schema.js +13 -13
  161. package/type/schema.js.flow +2 -3
  162. package/type/schema.mjs +1 -1
  163. package/type/validate.js +52 -19
  164. package/type/validate.js.flow +54 -8
  165. package/type/validate.mjs +43 -9
  166. package/utilities/TypeInfo.js +7 -7
  167. package/utilities/TypeInfo.js.flow +1 -2
  168. package/utilities/assertValidName.js +2 -2
  169. package/utilities/assertValidName.js.flow +0 -1
  170. package/utilities/astFromValue.js +14 -16
  171. package/utilities/astFromValue.js.flow +6 -7
  172. package/utilities/astFromValue.mjs +6 -7
  173. package/utilities/buildASTSchema.js +17 -27
  174. package/utilities/buildASTSchema.js.flow +5 -21
  175. package/utilities/buildASTSchema.mjs +11 -21
  176. package/utilities/buildClientSchema.js +16 -15
  177. package/utilities/buildClientSchema.js.flow +28 -12
  178. package/utilities/buildClientSchema.mjs +5 -4
  179. package/utilities/coerceInputValue.js +17 -18
  180. package/utilities/coerceInputValue.js.flow +12 -10
  181. package/utilities/coerceInputValue.mjs +7 -7
  182. package/utilities/concatAST.js +9 -8
  183. package/utilities/concatAST.js.flow +8 -8
  184. package/utilities/concatAST.mjs +9 -6
  185. package/utilities/extendSchema.js +26 -19
  186. package/utilities/extendSchema.js.flow +19 -7
  187. package/utilities/extendSchema.mjs +8 -1
  188. package/utilities/findBreakingChanges.js +15 -11
  189. package/utilities/findBreakingChanges.js.flow +6 -3
  190. package/utilities/findBreakingChanges.mjs +5 -2
  191. package/utilities/findDeprecatedUsages.js +2 -2
  192. package/utilities/findDeprecatedUsages.js.flow +0 -1
  193. package/utilities/getIntrospectionQuery.d.ts +6 -0
  194. package/utilities/getIntrospectionQuery.js +8 -2
  195. package/utilities/getIntrospectionQuery.js.flow +17 -5
  196. package/utilities/getIntrospectionQuery.mjs +8 -2
  197. package/utilities/getOperationAST.js +1 -1
  198. package/utilities/getOperationAST.js.flow +0 -1
  199. package/utilities/getOperationRootType.js +1 -1
  200. package/utilities/getOperationRootType.js.flow +0 -1
  201. package/utilities/index.d.ts +3 -0
  202. package/utilities/index.js +22 -22
  203. package/utilities/index.js.flow +0 -1
  204. package/utilities/introspectionFromSchema.js +7 -5
  205. package/utilities/introspectionFromSchema.js.flow +2 -1
  206. package/utilities/introspectionFromSchema.mjs +3 -1
  207. package/utilities/lexicographicSortSchema.js +13 -9
  208. package/utilities/lexicographicSortSchema.js.flow +21 -13
  209. package/utilities/lexicographicSortSchema.mjs +5 -2
  210. package/utilities/printSchema.js +15 -16
  211. package/utilities/printSchema.js.flow +23 -16
  212. package/utilities/printSchema.mjs +5 -6
  213. package/utilities/separateOperations.js +46 -42
  214. package/utilities/separateOperations.js.flow +46 -37
  215. package/utilities/separateOperations.mjs +44 -40
  216. package/utilities/stripIgnoredCharacters.js +6 -16
  217. package/utilities/stripIgnoredCharacters.js.flow +4 -13
  218. package/utilities/stripIgnoredCharacters.mjs +3 -10
  219. package/utilities/typeComparators.js +1 -1
  220. package/utilities/typeComparators.js.flow +0 -1
  221. package/utilities/typeFromAST.js +6 -6
  222. package/utilities/typeFromAST.js.flow +2 -3
  223. package/utilities/typeFromAST.mjs +2 -2
  224. package/utilities/typedQueryDocumentNode.d.ts +20 -0
  225. package/utilities/valueFromAST.js +6 -6
  226. package/utilities/valueFromAST.js.flow +4 -2
  227. package/utilities/valueFromASTUntyped.js +4 -4
  228. package/utilities/valueFromASTUntyped.js.flow +0 -1
  229. package/validation/ValidationContext.js +3 -3
  230. package/validation/ValidationContext.js.flow +3 -4
  231. package/validation/index.js +38 -38
  232. package/validation/index.js.flow +0 -1
  233. package/validation/rules/ExecutableDefinitions.js +1 -1
  234. package/validation/rules/ExecutableDefinitions.js.flow +0 -1
  235. package/validation/rules/ExecutableDefinitionsRule.js +3 -3
  236. package/validation/rules/ExecutableDefinitionsRule.js.flow +0 -1
  237. package/validation/rules/FieldsOnCorrectTypeRule.js +8 -6
  238. package/validation/rules/FieldsOnCorrectTypeRule.js.flow +2 -2
  239. package/validation/rules/FieldsOnCorrectTypeRule.mjs +2 -1
  240. package/validation/rules/FragmentsOnCompositeTypesRule.js +4 -4
  241. package/validation/rules/FragmentsOnCompositeTypesRule.js.flow +0 -1
  242. package/validation/rules/KnownArgumentNamesRule.js +5 -5
  243. package/validation/rules/KnownArgumentNamesRule.js.flow +1 -1
  244. package/validation/rules/KnownDirectivesRule.js +6 -6
  245. package/validation/rules/KnownDirectivesRule.js.flow +4 -3
  246. package/validation/rules/KnownFragmentNamesRule.js +1 -1
  247. package/validation/rules/KnownFragmentNamesRule.js.flow +0 -1
  248. package/validation/rules/KnownTypeNamesRule.js +6 -6
  249. package/validation/rules/KnownTypeNamesRule.js.flow +1 -2
  250. package/validation/rules/LoneAnonymousOperationRule.js +2 -2
  251. package/validation/rules/LoneAnonymousOperationRule.js.flow +0 -1
  252. package/validation/rules/LoneSchemaDefinition.js +1 -1
  253. package/validation/rules/LoneSchemaDefinition.js.flow +0 -1
  254. package/validation/rules/LoneSchemaDefinitionRule.js +1 -1
  255. package/validation/rules/LoneSchemaDefinitionRule.js.flow +0 -1
  256. package/validation/rules/NoFragmentCyclesRule.js +1 -1
  257. package/validation/rules/NoFragmentCyclesRule.js.flow +1 -2
  258. package/validation/rules/NoUndefinedVariablesRule.js +1 -1
  259. package/validation/rules/NoUndefinedVariablesRule.js.flow +0 -1
  260. package/validation/rules/NoUnusedFragmentsRule.js +1 -1
  261. package/validation/rules/NoUnusedFragmentsRule.js.flow +0 -1
  262. package/validation/rules/NoUnusedVariablesRule.js +1 -1
  263. package/validation/rules/NoUnusedVariablesRule.js.flow +0 -1
  264. package/validation/rules/OverlappingFieldsCanBeMergedRule.js +20 -20
  265. package/validation/rules/OverlappingFieldsCanBeMergedRule.js.flow +14 -14
  266. package/validation/rules/OverlappingFieldsCanBeMergedRule.mjs +12 -12
  267. package/validation/rules/PossibleFragmentSpreadsRule.js +5 -5
  268. package/validation/rules/PossibleFragmentSpreadsRule.js.flow +5 -2
  269. package/validation/rules/PossibleTypeExtensions.js +1 -1
  270. package/validation/rules/PossibleTypeExtensions.js.flow +0 -1
  271. package/validation/rules/PossibleTypeExtensionsRule.js +8 -8
  272. package/validation/rules/PossibleTypeExtensionsRule.js.flow +6 -4
  273. package/validation/rules/ProvidedRequiredArgumentsRule.js +7 -7
  274. package/validation/rules/ProvidedRequiredArgumentsRule.js.flow +4 -3
  275. package/validation/rules/ScalarLeafsRule.js +3 -3
  276. package/validation/rules/ScalarLeafsRule.js.flow +0 -1
  277. package/validation/rules/SingleFieldSubscriptionsRule.js +1 -1
  278. package/validation/rules/SingleFieldSubscriptionsRule.js.flow +0 -1
  279. package/validation/rules/UniqueArgumentNamesRule.js +1 -1
  280. package/validation/rules/UniqueArgumentNamesRule.js.flow +0 -1
  281. package/validation/rules/UniqueDirectiveNames.js +1 -1
  282. package/validation/rules/UniqueDirectiveNames.js.flow +0 -1
  283. package/validation/rules/UniqueDirectiveNamesRule.js +2 -2
  284. package/validation/rules/UniqueDirectiveNamesRule.js.flow +0 -1
  285. package/validation/rules/UniqueDirectiveNamesRule.mjs +1 -1
  286. package/validation/rules/UniqueDirectivesPerLocationRule.js +4 -4
  287. package/validation/rules/UniqueDirectivesPerLocationRule.js.flow +0 -1
  288. package/validation/rules/UniqueEnumValueNames.js +1 -1
  289. package/validation/rules/UniqueEnumValueNames.js.flow +0 -1
  290. package/validation/rules/UniqueEnumValueNamesRule.js +2 -2
  291. package/validation/rules/UniqueEnumValueNamesRule.js.flow +7 -2
  292. package/validation/rules/UniqueFieldDefinitionNames.js +1 -1
  293. package/validation/rules/UniqueFieldDefinitionNames.js.flow +0 -1
  294. package/validation/rules/UniqueFieldDefinitionNamesRule.js +3 -3
  295. package/validation/rules/UniqueFieldDefinitionNamesRule.js.flow +13 -4
  296. package/validation/rules/UniqueFieldDefinitionNamesRule.mjs +1 -1
  297. package/validation/rules/UniqueFragmentNamesRule.js +1 -1
  298. package/validation/rules/UniqueFragmentNamesRule.js.flow +0 -1
  299. package/validation/rules/UniqueInputFieldNamesRule.js +1 -1
  300. package/validation/rules/UniqueInputFieldNamesRule.js.flow +0 -1
  301. package/validation/rules/UniqueOperationNamesRule.js +1 -1
  302. package/validation/rules/UniqueOperationNamesRule.js.flow +0 -1
  303. package/validation/rules/UniqueOperationTypes.js +1 -1
  304. package/validation/rules/UniqueOperationTypes.js.flow +0 -1
  305. package/validation/rules/UniqueOperationTypesRule.js +1 -1
  306. package/validation/rules/UniqueOperationTypesRule.js.flow +7 -2
  307. package/validation/rules/UniqueTypeNames.js +1 -1
  308. package/validation/rules/UniqueTypeNames.js.flow +0 -1
  309. package/validation/rules/UniqueTypeNamesRule.js +2 -2
  310. package/validation/rules/UniqueTypeNamesRule.js.flow +0 -1
  311. package/validation/rules/UniqueTypeNamesRule.mjs +1 -1
  312. package/validation/rules/UniqueVariableNamesRule.js +1 -1
  313. package/validation/rules/UniqueVariableNamesRule.js.flow +0 -1
  314. package/validation/rules/ValuesOfCorrectTypeRule.js +8 -8
  315. package/validation/rules/ValuesOfCorrectTypeRule.js.flow +0 -1
  316. package/validation/rules/VariablesAreInputTypesRule.js +4 -4
  317. package/validation/rules/VariablesAreInputTypesRule.js.flow +0 -1
  318. package/validation/rules/VariablesInAllowedPositionRule.js +6 -6
  319. package/validation/rules/VariablesInAllowedPositionRule.js.flow +0 -1
  320. package/validation/rules/custom/NoDeprecatedCustomRule.js +47 -9
  321. package/validation/rules/custom/NoDeprecatedCustomRule.js.flow +56 -13
  322. package/validation/rules/custom/NoDeprecatedCustomRule.mjs +43 -8
  323. package/validation/rules/custom/NoSchemaIntrospectionCustomRule.js +3 -3
  324. package/validation/rules/custom/NoSchemaIntrospectionCustomRule.js.flow +0 -1
  325. package/validation/specifiedRules.js +33 -33
  326. package/validation/specifiedRules.js.flow +0 -1
  327. package/validation/validate.js +7 -7
  328. package/validation/validate.js.flow +4 -5
  329. package/version.js +3 -3
  330. package/version.js.flow +3 -4
  331. package/version.mjs +3 -3
  332. package/jsutils/isCollection.js +0 -47
  333. package/jsutils/isCollection.js.flow +0 -39
  334. package/jsutils/isCollection.mjs +0 -40
  335. package/polyfills/flatMap.js +0 -30
  336. package/polyfills/flatMap.js.flow +0 -28
  337. package/polyfills/flatMap.mjs +0 -23
@@ -0,0 +1,723 @@
1
+ // @flow strict
2
+ import { Lexer } from '../lexer';
3
+ import { Source } from '../source';
4
+
5
+ import GraphQLGrammar from './grammar';
6
+ import type {
7
+ GraphQLGrammarRule,
8
+ GraphQLGrammarRuleName,
9
+ GraphQLGrammarRuleConstraint,
10
+ GraphQLGrammarTokenConstraint,
11
+ GraphQLGrammarOfTypeConstraint,
12
+ GraphQLGrammarListOfTypeConstraint,
13
+ GraphQLGrammarPeekConstraint,
14
+ GraphQLGrammarConstraintsSet,
15
+ } from './grammar';
16
+
17
+ export const TokenKind = {
18
+ NAME: 'Name',
19
+ INT: 'Int',
20
+ FLOAT: 'Float',
21
+ STRING: 'String',
22
+ BLOCK_STRING: 'BlockString',
23
+ COMMENT: 'Comment',
24
+ PUNCTUATION: 'Punctuation',
25
+ EOF: '<EOF>',
26
+ INVALID: 'Invalid',
27
+ };
28
+
29
+ export const RuleKind = {
30
+ TOKEN_CONSTRAINT: 'TokenConstraint',
31
+ OF_TYPE_CONSTRAINT: 'OfTypeConstraint',
32
+ LIST_OF_TYPE_CONSTRAINT: 'ListOfTypeConstraint',
33
+ PEEK_CONSTRAINT: 'PeekConstraint',
34
+ CONSTRAINTS_SET: 'ConstraintsSet',
35
+ CONSTRAINTS_SET_ROOT: 'ConstraintsSetRoot',
36
+ RULE_NAME: 'RuleName',
37
+ INVALID: 'Invalid',
38
+ };
39
+
40
+ interface BaseOnlineParserRule {
41
+ kind: string;
42
+ name?: string;
43
+ depth: number;
44
+ step: number;
45
+ expanded: boolean;
46
+ state: string;
47
+ optional?: boolean;
48
+ eatNextOnFail?: boolean;
49
+ }
50
+ interface TokenOnlineParserRule
51
+ extends BaseOnlineParserRule,
52
+ GraphQLGrammarTokenConstraint {}
53
+ interface OfTypeOnlineParserRule
54
+ extends BaseOnlineParserRule,
55
+ GraphQLGrammarOfTypeConstraint {}
56
+ interface ListOfTypeOnlineParserRule
57
+ extends BaseOnlineParserRule,
58
+ GraphQLGrammarListOfTypeConstraint {}
59
+ interface PeekOnlineParserRule
60
+ extends BaseOnlineParserRule,
61
+ GraphQLGrammarPeekConstraint {
62
+ index: number;
63
+ matched: boolean;
64
+ }
65
+ interface ConstraintsSetOnlineParserRule extends BaseOnlineParserRule {
66
+ constraintsSet: boolean;
67
+ constraints: GraphQLGrammarConstraintsSet;
68
+ }
69
+
70
+ type OnlineParserRule =
71
+ | TokenOnlineParserRule
72
+ | OfTypeOnlineParserRule
73
+ | ListOfTypeOnlineParserRule
74
+ | PeekOnlineParserRule
75
+ | ConstraintsSetOnlineParserRule;
76
+
77
+ export type OnlineParserState = {|
78
+ rules: Array<OnlineParserRule>,
79
+ kind: () => string,
80
+ step: () => number,
81
+ levels: Array<number>,
82
+ indentLevel: number,
83
+ name: string | null,
84
+ type: string | null,
85
+ |};
86
+
87
+ type Token = {|
88
+ kind: string,
89
+ value: string,
90
+ tokenName?: ?string,
91
+ ruleName?: ?string,
92
+ |};
93
+
94
+ type LexerToken = {|
95
+ kind: string,
96
+ value: ?string,
97
+ |};
98
+
99
+ type OnlineParserConfig = {|
100
+ tabSize: number,
101
+ |};
102
+
103
+ type OnlineParserConfigOption = {|
104
+ tabSize: ?number,
105
+ |};
106
+
107
+ export class OnlineParser {
108
+ state: OnlineParserState;
109
+ _lexer: Lexer;
110
+ _config: OnlineParserConfig;
111
+
112
+ constructor(
113
+ source: string,
114
+ state?: OnlineParserState,
115
+ config?: OnlineParserConfigOption,
116
+ ) {
117
+ this.state = state || OnlineParser.startState();
118
+ this._config = {
119
+ tabSize: config?.tabSize ?? 2,
120
+ };
121
+ this._lexer = new Lexer(new Source(source));
122
+ }
123
+
124
+ static startState(): OnlineParserState {
125
+ return {
126
+ rules: [
127
+ // $FlowFixMe[cannot-spread-interface]
128
+ {
129
+ name: 'Document',
130
+ state: 'Document',
131
+ kind: 'ListOfTypeConstraint',
132
+ ...GraphQLGrammar.Document,
133
+ expanded: false,
134
+ depth: 1,
135
+ step: 1,
136
+ },
137
+ ],
138
+ name: null,
139
+ type: null,
140
+ levels: [],
141
+ indentLevel: 0,
142
+ kind(): string {
143
+ return this.rules[this.rules.length - 1]?.state || '';
144
+ },
145
+ step(): number {
146
+ return this.rules[this.rules.length - 1]?.step || 0;
147
+ },
148
+ };
149
+ }
150
+
151
+ static copyState(state: OnlineParserState): OnlineParserState {
152
+ return {
153
+ name: state.name,
154
+ type: state.type,
155
+ rules: JSON.parse(JSON.stringify(state.rules)),
156
+ levels: [...state.levels],
157
+ indentLevel: state.indentLevel,
158
+ kind(): string {
159
+ return this.rules[this.rules.length - 1]?.state || '';
160
+ },
161
+ step(): number {
162
+ return this.rules[this.rules.length - 1]?.step || 0;
163
+ },
164
+ };
165
+ }
166
+
167
+ sol(): boolean {
168
+ return (
169
+ this._lexer.source.locationOffset.line === 1 &&
170
+ this._lexer.source.locationOffset.column === 1
171
+ );
172
+ }
173
+
174
+ parseToken(): Token {
175
+ const rule = (this._getNextRule(): any);
176
+
177
+ if (this.sol()) {
178
+ this.state.indentLevel = Math.floor(
179
+ this.indentation() / this._config.tabSize,
180
+ );
181
+ }
182
+
183
+ if (!rule) {
184
+ return {
185
+ kind: TokenKind.INVALID,
186
+ value: '',
187
+ };
188
+ }
189
+
190
+ let token;
191
+
192
+ if (this._lookAhead().kind === '<EOF>') {
193
+ return {
194
+ kind: TokenKind.EOF,
195
+ value: '',
196
+ ruleName: rule.name,
197
+ };
198
+ }
199
+
200
+ switch (rule.kind) {
201
+ case RuleKind.TOKEN_CONSTRAINT:
202
+ token = this._parseTokenConstraint(rule);
203
+ break;
204
+ case RuleKind.LIST_OF_TYPE_CONSTRAINT:
205
+ token = this._parseListOfTypeConstraint(rule);
206
+ break;
207
+ case RuleKind.OF_TYPE_CONSTRAINT:
208
+ token = this._parseOfTypeConstraint(rule);
209
+ break;
210
+ case RuleKind.PEEK_CONSTRAINT:
211
+ token = this._parsePeekConstraint(rule);
212
+ break;
213
+ case RuleKind.CONSTRAINTS_SET_ROOT:
214
+ token = this._parseConstraintsSetRule(rule);
215
+ break;
216
+ default:
217
+ return {
218
+ kind: TokenKind.INVALID,
219
+ value: '',
220
+ ruleName: rule.name,
221
+ };
222
+ }
223
+
224
+ if (token && token.kind === TokenKind.INVALID) {
225
+ if (rule.optional === true) {
226
+ this.state.rules.pop();
227
+ } else {
228
+ this._rollbackRule();
229
+ }
230
+
231
+ return this.parseToken() || token;
232
+ }
233
+
234
+ return token;
235
+ }
236
+
237
+ indentation(): number {
238
+ const match = this._lexer.source.body.match(/\s*/);
239
+ let indent = 0;
240
+
241
+ if (match && match.length === 0) {
242
+ const whiteSpaces = match[0];
243
+ let pos = 0;
244
+ while (whiteSpaces.length > pos) {
245
+ if (whiteSpaces.charCodeAt(pos) === 9) {
246
+ indent += 2;
247
+ } else {
248
+ indent++;
249
+ }
250
+ pos++;
251
+ }
252
+ }
253
+
254
+ return indent;
255
+ }
256
+
257
+ _parseTokenConstraint(rule: TokenOnlineParserRule): Token {
258
+ rule.expanded = true;
259
+
260
+ const token = this._lookAhead();
261
+
262
+ if (!this._matchToken(token, rule)) {
263
+ return {
264
+ kind: TokenKind.INVALID,
265
+ value: '',
266
+ tokenName: rule.tokenName,
267
+ ruleName: rule.name,
268
+ };
269
+ }
270
+
271
+ this._advanceToken();
272
+ const parserToken = this._transformLexerToken(token, rule);
273
+ this._popMatchedRule(parserToken);
274
+
275
+ return parserToken;
276
+ }
277
+
278
+ _parseListOfTypeConstraint(rule: ListOfTypeOnlineParserRule): Token {
279
+ this._pushRule(
280
+ GraphQLGrammar[rule.listOfType],
281
+ rule.depth + 1,
282
+ rule.listOfType,
283
+ 1,
284
+ rule.state,
285
+ );
286
+
287
+ rule.expanded = true;
288
+
289
+ const token = this.parseToken();
290
+
291
+ return token;
292
+ }
293
+
294
+ _parseOfTypeConstraint(rule: OfTypeOnlineParserRule): Token {
295
+ if (rule.expanded) {
296
+ this._popMatchedRule();
297
+ return this.parseToken();
298
+ }
299
+
300
+ this._pushRule(rule.ofType, rule.depth + 1, rule.tokenName, 1, rule.state);
301
+ rule.expanded = true;
302
+
303
+ const token = this.parseToken();
304
+
305
+ return token;
306
+ }
307
+
308
+ _parsePeekConstraint(rule: PeekOnlineParserRule): Token {
309
+ if (rule.expanded) {
310
+ this._popMatchedRule();
311
+ return this.parseToken();
312
+ }
313
+
314
+ while (!rule.matched && rule.index < rule.peek.length - 1) {
315
+ rule.index++;
316
+ const constraint = rule.peek[rule.index];
317
+
318
+ let { ifCondition } = constraint;
319
+ if (typeof ifCondition === 'string') {
320
+ ifCondition = GraphQLGrammar[ifCondition];
321
+ }
322
+
323
+ let token = this._lookAhead();
324
+ if (ifCondition && this._matchToken(token, ifCondition)) {
325
+ rule.matched = true;
326
+ rule.expanded = true;
327
+ this._pushRule(constraint.expect, rule.depth + 1, '', 1, rule.state);
328
+
329
+ token = this.parseToken();
330
+
331
+ return token;
332
+ }
333
+ }
334
+
335
+ return {
336
+ kind: TokenKind.INVALID,
337
+ value: '',
338
+ ruleName: rule.name,
339
+ };
340
+ }
341
+
342
+ _parseConstraintsSetRule(rule: ConstraintsSetOnlineParserRule): Token {
343
+ if (rule.expanded) {
344
+ this._popMatchedRule();
345
+ return this.parseToken();
346
+ }
347
+
348
+ for (let index = rule.constraints.length - 1; index >= 0; index--) {
349
+ this._pushRule(
350
+ rule.constraints[index],
351
+ rule.depth + 1,
352
+ '',
353
+ index,
354
+ rule.state,
355
+ );
356
+ }
357
+ rule.expanded = true;
358
+
359
+ return this.parseToken();
360
+ }
361
+
362
+ _matchToken(
363
+ token: Token | LexerToken,
364
+ rule: GraphQLGrammarTokenConstraint,
365
+ ): boolean {
366
+ if (typeof token.value === 'string') {
367
+ if (
368
+ (typeof rule.ofValue === 'string' && token.value !== rule.ofValue) ||
369
+ (Array.isArray(rule.oneOf) && !rule.oneOf.includes(token.value)) ||
370
+ (typeof rule.ofValue !== 'string' &&
371
+ !Array.isArray(rule.oneOf) &&
372
+ token.kind !== rule.token)
373
+ ) {
374
+ return false;
375
+ }
376
+
377
+ return this._butNot(token, rule);
378
+ }
379
+
380
+ if (token.kind !== rule.token) {
381
+ return false;
382
+ }
383
+
384
+ return this._butNot(token, rule);
385
+ }
386
+
387
+ _butNot(
388
+ token: Token | LexerToken,
389
+ rule: GraphQLGrammarRuleConstraint,
390
+ ): boolean {
391
+ if (rule.butNot) {
392
+ if (Array.isArray(rule.butNot)) {
393
+ if (
394
+ rule.butNot.reduce(
395
+ (matched, constraint) =>
396
+ matched || this._matchToken(token, constraint),
397
+ false,
398
+ )
399
+ ) {
400
+ return false;
401
+ }
402
+
403
+ return true;
404
+ }
405
+
406
+ return !this._matchToken(token, rule.butNot);
407
+ }
408
+
409
+ return true;
410
+ }
411
+
412
+ _transformLexerToken(lexerToken: LexerToken, rule: any): Token {
413
+ let token;
414
+ const ruleName = rule.name || '';
415
+ const tokenName = rule.tokenName || '';
416
+
417
+ if (lexerToken.kind === '<EOF>' || lexerToken.value !== undefined) {
418
+ token = {
419
+ kind: lexerToken.kind,
420
+ value: lexerToken.value || '',
421
+ tokenName,
422
+ ruleName,
423
+ };
424
+
425
+ if (token.kind === TokenKind.STRING) {
426
+ token.value = `"${token.value}"`;
427
+ } else if (token.kind === TokenKind.BLOCK_STRING) {
428
+ token.value = `"""${token.value}"""`;
429
+ }
430
+ } else {
431
+ token = {
432
+ kind: TokenKind.PUNCTUATION,
433
+ value: lexerToken.kind,
434
+ tokenName,
435
+ ruleName,
436
+ };
437
+
438
+ if (/^[{([]/.test(token.value)) {
439
+ if (this.state.indentLevel !== undefined) {
440
+ this.state.levels = this.state.levels.concat(
441
+ this.state.indentLevel + 1,
442
+ );
443
+ }
444
+ } else if (/^[})\]]/.test(token.value)) {
445
+ this.state.levels.pop();
446
+ }
447
+ }
448
+
449
+ return token;
450
+ }
451
+
452
+ _getNextRule(): OnlineParserRule | null {
453
+ return this.state.rules[this.state.rules.length - 1] || null;
454
+ }
455
+
456
+ _popMatchedRule(token: ?Token) {
457
+ const rule = this.state.rules.pop();
458
+ if (!rule) {
459
+ return;
460
+ }
461
+
462
+ if (token && rule.kind === RuleKind.TOKEN_CONSTRAINT) {
463
+ const constraint = rule;
464
+ if (typeof constraint.definitionName === 'string') {
465
+ this.state.name = token.value || null;
466
+ } else if (typeof constraint.typeName === 'string') {
467
+ this.state.type = token.value || null;
468
+ }
469
+ }
470
+
471
+ const nextRule = this._getNextRule();
472
+ if (!nextRule) {
473
+ return;
474
+ }
475
+
476
+ if (
477
+ nextRule.depth === rule.depth - 1 &&
478
+ nextRule.expanded &&
479
+ nextRule.kind === RuleKind.CONSTRAINTS_SET_ROOT
480
+ ) {
481
+ this.state.rules.pop();
482
+ }
483
+
484
+ if (
485
+ nextRule.depth === rule.depth - 1 &&
486
+ nextRule.expanded &&
487
+ nextRule.kind === RuleKind.LIST_OF_TYPE_CONSTRAINT
488
+ ) {
489
+ nextRule.expanded = false;
490
+ nextRule.optional = true;
491
+ }
492
+ }
493
+
494
+ _rollbackRule() {
495
+ if (!this.state.rules.length) {
496
+ return;
497
+ }
498
+
499
+ const popRule = () => {
500
+ const lastPoppedRule = this.state.rules.pop();
501
+
502
+ if (lastPoppedRule.eatNextOnFail === true) {
503
+ this.state.rules.pop();
504
+ }
505
+ };
506
+
507
+ const poppedRule = this.state.rules.pop();
508
+ if (!poppedRule) {
509
+ return;
510
+ }
511
+
512
+ let popped = 0;
513
+ let nextRule = this._getNextRule();
514
+ while (
515
+ nextRule &&
516
+ (poppedRule.kind !== RuleKind.LIST_OF_TYPE_CONSTRAINT ||
517
+ nextRule.expanded) &&
518
+ nextRule.depth > poppedRule.depth - 1
519
+ ) {
520
+ this.state.rules.pop();
521
+ popped++;
522
+ nextRule = this._getNextRule();
523
+ }
524
+
525
+ if (nextRule && nextRule.expanded) {
526
+ if (nextRule.optional === true) {
527
+ popRule();
528
+ } else {
529
+ if (
530
+ nextRule.kind === RuleKind.LIST_OF_TYPE_CONSTRAINT &&
531
+ popped === 1
532
+ ) {
533
+ this.state.rules.pop();
534
+ return;
535
+ }
536
+ this._rollbackRule();
537
+ }
538
+ }
539
+ }
540
+
541
+ _pushRule(
542
+ baseRule: any,
543
+ depth: number,
544
+ name?: string,
545
+ step?: number,
546
+ state?: string,
547
+ ) {
548
+ this.state.name = null;
549
+ this.state.type = null;
550
+ let rule = baseRule;
551
+
552
+ switch (this._getRuleKind(rule)) {
553
+ case RuleKind.RULE_NAME:
554
+ rule = (rule: GraphQLGrammarRuleName);
555
+ this._pushRule(
556
+ GraphQLGrammar[rule],
557
+ depth,
558
+ (typeof name === 'string' ? name : undefined) || rule,
559
+ step,
560
+ state,
561
+ );
562
+ break;
563
+ case RuleKind.CONSTRAINTS_SET:
564
+ rule = (rule: GraphQLGrammarConstraintsSet);
565
+ this.state.rules.push({
566
+ name: name || '',
567
+ depth,
568
+ expanded: false,
569
+ constraints: rule,
570
+ constraintsSet: true,
571
+ kind: RuleKind.CONSTRAINTS_SET_ROOT,
572
+ state:
573
+ (typeof name === 'string' ? name : undefined) ||
574
+ (typeof state === 'string' ? state : undefined) ||
575
+ this._getNextRule()?.state ||
576
+ '',
577
+ step:
578
+ typeof step === 'number'
579
+ ? step
580
+ : (this._getNextRule()?.step || 0) + 1,
581
+ });
582
+ break;
583
+ case RuleKind.OF_TYPE_CONSTRAINT:
584
+ rule = (rule: GraphQLGrammarOfTypeConstraint);
585
+ this.state.rules.push({
586
+ name: name || '',
587
+ ofType: rule.ofType,
588
+ optional: Boolean(rule.optional),
589
+ butNot: rule.butNot,
590
+ eatNextOnFail: Boolean(rule.eatNextOnFail),
591
+ depth,
592
+ expanded: false,
593
+ kind: RuleKind.OF_TYPE_CONSTRAINT,
594
+ state:
595
+ (typeof rule.tokenName === 'string' ? rule.tokenName : undefined) ||
596
+ (typeof name === 'string' ? name : undefined) ||
597
+ (typeof state === 'string' ? state : undefined) ||
598
+ this._getNextRule()?.state ||
599
+ '',
600
+ step:
601
+ typeof step === 'number'
602
+ ? step
603
+ : (this._getNextRule()?.step || 0) + 1,
604
+ });
605
+ break;
606
+ case RuleKind.LIST_OF_TYPE_CONSTRAINT:
607
+ rule = (rule: GraphQLGrammarListOfTypeConstraint);
608
+ this.state.rules.push({
609
+ listOfType: rule.listOfType,
610
+ optional: Boolean(rule.optional),
611
+ butNot: rule.butNot,
612
+ eatNextOnFail: Boolean(rule.eatNextOnFail),
613
+ name: name || '',
614
+ depth,
615
+ expanded: false,
616
+ kind: RuleKind.LIST_OF_TYPE_CONSTRAINT,
617
+ state:
618
+ (typeof name === 'string' ? name : undefined) ||
619
+ (typeof state === 'string' ? state : undefined) ||
620
+ this._getNextRule()?.state ||
621
+ '',
622
+ step:
623
+ typeof step === 'number'
624
+ ? step
625
+ : (this._getNextRule()?.step || 0) + 1,
626
+ });
627
+ break;
628
+ case RuleKind.TOKEN_CONSTRAINT:
629
+ rule = (rule: GraphQLGrammarTokenConstraint);
630
+ this.state.rules.push({
631
+ token: rule.token,
632
+ ofValue: rule.ofValue,
633
+ oneOf: rule.oneOf,
634
+ definitionName: Boolean(rule.definitionName),
635
+ typeName: Boolean(rule.typeName),
636
+ optional: Boolean(rule.optional),
637
+ butNot: rule.butNot,
638
+ eatNextOnFail: Boolean(rule.eatNextOnFail),
639
+ name: name || '',
640
+ depth,
641
+ expanded: false,
642
+ kind: RuleKind.TOKEN_CONSTRAINT,
643
+ state:
644
+ (typeof rule.tokenName === 'string' ? rule.tokenName : undefined) ||
645
+ (typeof state === 'string' ? state : undefined) ||
646
+ this._getNextRule()?.state ||
647
+ '',
648
+ step:
649
+ typeof step === 'number'
650
+ ? step
651
+ : (this._getNextRule()?.step || 0) + 1,
652
+ });
653
+ break;
654
+ case RuleKind.PEEK_CONSTRAINT:
655
+ rule = (rule: GraphQLGrammarPeekConstraint);
656
+ this.state.rules.push({
657
+ peek: rule.peek,
658
+ optional: Boolean(rule.optional),
659
+ butNot: rule.butNot,
660
+ eatNextOnFail: Boolean(rule.eatNextOnFail),
661
+ name: name || '',
662
+ depth,
663
+ index: -1,
664
+ matched: false,
665
+ expanded: false,
666
+ kind: RuleKind.PEEK_CONSTRAINT,
667
+ state:
668
+ (typeof state === 'string' ? state : undefined) ||
669
+ this._getNextRule()?.state ||
670
+ '',
671
+ step:
672
+ typeof step === 'number'
673
+ ? step
674
+ : (this._getNextRule()?.step || 0) + 1,
675
+ });
676
+ break;
677
+ }
678
+ }
679
+
680
+ _getRuleKind(rule: GraphQLGrammarRule | OnlineParserRule): string {
681
+ if (Array.isArray(rule)) {
682
+ return RuleKind.CONSTRAINTS_SET;
683
+ }
684
+
685
+ if (rule.constraintsSet === true) {
686
+ return RuleKind.CONSTRAINTS_SET_ROOT;
687
+ }
688
+
689
+ if (typeof rule === 'string') {
690
+ return RuleKind.RULE_NAME;
691
+ }
692
+
693
+ if (Object.prototype.hasOwnProperty.call(rule, 'ofType')) {
694
+ return RuleKind.OF_TYPE_CONSTRAINT;
695
+ }
696
+
697
+ if (Object.prototype.hasOwnProperty.call(rule, 'listOfType')) {
698
+ return RuleKind.LIST_OF_TYPE_CONSTRAINT;
699
+ }
700
+
701
+ if (Object.prototype.hasOwnProperty.call(rule, 'peek')) {
702
+ return RuleKind.PEEK_CONSTRAINT;
703
+ }
704
+
705
+ if (Object.prototype.hasOwnProperty.call(rule, 'token')) {
706
+ return RuleKind.TOKEN_CONSTRAINT;
707
+ }
708
+
709
+ return RuleKind.INVALID;
710
+ }
711
+
712
+ _advanceToken(): LexerToken {
713
+ return (this._lexer.advance(): any);
714
+ }
715
+
716
+ _lookAhead(): LexerToken {
717
+ try {
718
+ return (this._lexer.lookahead(): any);
719
+ } catch (err) {
720
+ return { kind: TokenKind.INVALID, value: '' };
721
+ }
722
+ }
723
+ }