graphql-jit 0.8.9 → 0.9.0

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 (178) hide show
  1. package/README.md +25 -0
  2. package/dist/cjs/ast.d.ts +89 -0
  3. package/dist/cjs/ast.js +766 -0
  4. package/dist/cjs/ast.js.map +1 -0
  5. package/dist/cjs/compat.d.ts +70 -0
  6. package/dist/cjs/compat.js +144 -0
  7. package/dist/cjs/compat.js.map +1 -0
  8. package/dist/cjs/debug.d.ts +29 -0
  9. package/dist/cjs/debug.js +46 -0
  10. package/dist/cjs/debug.js.map +1 -0
  11. package/dist/cjs/error.d.ts +7 -0
  12. package/dist/cjs/error.js +58 -0
  13. package/dist/cjs/error.js.map +1 -0
  14. package/dist/cjs/execution.d.ts +154 -0
  15. package/dist/cjs/execution.js +1240 -0
  16. package/dist/cjs/execution.js.map +1 -0
  17. package/dist/cjs/generate.d.ts +4 -0
  18. package/dist/cjs/generate.js +13 -0
  19. package/dist/cjs/generate.js.map +1 -0
  20. package/dist/cjs/index.d.ts +3 -0
  21. package/dist/cjs/index.js +10 -0
  22. package/dist/cjs/index.js.map +1 -0
  23. package/dist/cjs/inspect.d.ts +5 -0
  24. package/dist/cjs/inspect.js +110 -0
  25. package/dist/cjs/inspect.js.map +1 -0
  26. package/dist/cjs/json.d.ts +9 -0
  27. package/dist/cjs/json.js +144 -0
  28. package/dist/cjs/json.js.map +1 -0
  29. package/dist/cjs/memoize.d.ts +5 -0
  30. package/dist/cjs/memoize.js +28 -0
  31. package/dist/cjs/memoize.js.map +1 -0
  32. package/dist/cjs/non-null.d.ts +9 -0
  33. package/dist/cjs/non-null.js +205 -0
  34. package/dist/cjs/non-null.js.map +1 -0
  35. package/dist/cjs/package.json +3 -0
  36. package/dist/{resolve-info.d.ts → cjs/resolve-info.d.ts} +10 -12
  37. package/dist/cjs/resolve-info.js +232 -0
  38. package/dist/cjs/resolve-info.js.map +1 -0
  39. package/dist/cjs/runtime.d.ts +14 -0
  40. package/dist/cjs/runtime.js +92 -0
  41. package/dist/cjs/runtime.js.map +1 -0
  42. package/dist/cjs/types.d.ts +1 -0
  43. package/dist/cjs/types.js +3 -0
  44. package/dist/cjs/types.js.map +1 -0
  45. package/dist/cjs/variables.d.ts +20 -0
  46. package/dist/cjs/variables.js +357 -0
  47. package/dist/cjs/variables.js.map +1 -0
  48. package/dist/esm/ast.d.ts +89 -0
  49. package/dist/esm/ast.js +751 -0
  50. package/dist/esm/ast.js.map +1 -0
  51. package/dist/esm/compat.d.ts +70 -0
  52. package/dist/esm/compat.js +102 -0
  53. package/dist/esm/compat.js.map +1 -0
  54. package/dist/esm/debug.d.ts +29 -0
  55. package/dist/esm/debug.js +40 -0
  56. package/dist/esm/debug.js.map +1 -0
  57. package/dist/esm/error.d.ts +7 -0
  58. package/dist/esm/error.js +55 -0
  59. package/dist/esm/error.js.map +1 -0
  60. package/dist/esm/execution.d.ts +154 -0
  61. package/dist/esm/execution.js +1227 -0
  62. package/dist/esm/execution.js.map +1 -0
  63. package/dist/esm/generate.d.ts +4 -0
  64. package/dist/esm/generate.js +10 -0
  65. package/dist/esm/generate.js.map +1 -0
  66. package/dist/esm/index.d.ts +3 -0
  67. package/dist/esm/index.js +3 -0
  68. package/dist/esm/index.js.map +1 -0
  69. package/dist/esm/inspect.d.ts +5 -0
  70. package/dist/esm/inspect.js +106 -0
  71. package/dist/esm/inspect.js.map +1 -0
  72. package/dist/esm/json.d.ts +9 -0
  73. package/dist/esm/json.js +141 -0
  74. package/dist/esm/json.js.map +1 -0
  75. package/dist/esm/memoize.d.ts +5 -0
  76. package/dist/esm/memoize.js +20 -0
  77. package/dist/esm/memoize.js.map +1 -0
  78. package/dist/esm/non-null.d.ts +9 -0
  79. package/dist/esm/non-null.js +199 -0
  80. package/dist/esm/non-null.js.map +1 -0
  81. package/dist/esm/package.json +3 -0
  82. package/dist/{resolve-info.d.mts → esm/resolve-info.d.ts} +10 -12
  83. package/dist/esm/resolve-info.js +224 -0
  84. package/dist/esm/resolve-info.js.map +1 -0
  85. package/dist/esm/runtime.d.ts +14 -0
  86. package/dist/esm/runtime.js +89 -0
  87. package/dist/esm/runtime.js.map +1 -0
  88. package/dist/esm/types.d.ts +1 -0
  89. package/dist/esm/types.js +2 -0
  90. package/dist/esm/types.js.map +1 -0
  91. package/dist/esm/variables.d.ts +20 -0
  92. package/dist/esm/variables.js +350 -0
  93. package/dist/esm/variables.js.map +1 -0
  94. package/package.json +39 -35
  95. package/dist/ast.d.mts +0 -7
  96. package/dist/ast.d.ts +0 -7
  97. package/dist/ast.js +0 -635
  98. package/dist/ast.js.map +0 -1
  99. package/dist/ast.mjs +0 -612
  100. package/dist/ast.mjs.map +0 -1
  101. package/dist/compat.d.mts +0 -7
  102. package/dist/compat.d.ts +0 -7
  103. package/dist/compat.js +0 -113
  104. package/dist/compat.js.map +0 -1
  105. package/dist/compat.mjs +0 -75
  106. package/dist/compat.mjs.map +0 -1
  107. package/dist/error.d.mts +0 -11
  108. package/dist/error.d.ts +0 -11
  109. package/dist/error.js +0 -79
  110. package/dist/error.js.map +0 -1
  111. package/dist/error.mjs +0 -57
  112. package/dist/error.mjs.map +0 -1
  113. package/dist/execution.d.mts +0 -7
  114. package/dist/execution.d.ts +0 -7
  115. package/dist/execution.js +0 -1262
  116. package/dist/execution.js.map +0 -1
  117. package/dist/execution.mjs +0 -1248
  118. package/dist/execution.mjs.map +0 -1
  119. package/dist/generate.d.mts +0 -6
  120. package/dist/generate.d.ts +0 -6
  121. package/dist/generate.js +0 -37
  122. package/dist/generate.js.map +0 -1
  123. package/dist/generate.mjs +0 -13
  124. package/dist/generate.mjs.map +0 -1
  125. package/dist/index-CeluTkd4.d.mts +0 -329
  126. package/dist/index-mKv2zpdA.d.ts +0 -329
  127. package/dist/index.d.mts +0 -7
  128. package/dist/index.d.ts +0 -7
  129. package/dist/index.js +0 -36
  130. package/dist/index.js.map +0 -1
  131. package/dist/index.mjs +0 -15
  132. package/dist/index.mjs.map +0 -1
  133. package/dist/inspect.d.mts +0 -7
  134. package/dist/inspect.d.ts +0 -7
  135. package/dist/inspect.js +0 -121
  136. package/dist/inspect.js.map +0 -1
  137. package/dist/inspect.mjs +0 -97
  138. package/dist/inspect.mjs.map +0 -1
  139. package/dist/json.d.mts +0 -18
  140. package/dist/json.d.ts +0 -18
  141. package/dist/json.js +0 -181
  142. package/dist/json.js.map +0 -1
  143. package/dist/json.mjs +0 -164
  144. package/dist/json.mjs.map +0 -1
  145. package/dist/memoize.d.mts +0 -6
  146. package/dist/memoize.d.ts +0 -6
  147. package/dist/memoize.js +0 -67
  148. package/dist/memoize.js.map +0 -1
  149. package/dist/memoize.mjs +0 -31
  150. package/dist/memoize.mjs.map +0 -1
  151. package/dist/non-null.d.mts +0 -7
  152. package/dist/non-null.d.ts +0 -7
  153. package/dist/non-null.js +0 -196
  154. package/dist/non-null.js.map +0 -1
  155. package/dist/non-null.mjs +0 -167
  156. package/dist/non-null.mjs.map +0 -1
  157. package/dist/resolve-info.js +0 -245
  158. package/dist/resolve-info.js.map +0 -1
  159. package/dist/resolve-info.mjs +0 -219
  160. package/dist/resolve-info.mjs.map +0 -1
  161. package/dist/runtime.d.mts +0 -7
  162. package/dist/runtime.d.ts +0 -7
  163. package/dist/runtime.js +0 -115
  164. package/dist/runtime.js.map +0 -1
  165. package/dist/runtime.mjs +0 -91
  166. package/dist/runtime.mjs.map +0 -1
  167. package/dist/types.d.mts +0 -3
  168. package/dist/types.d.ts +0 -3
  169. package/dist/types.js +0 -17
  170. package/dist/types.js.map +0 -1
  171. package/dist/types.mjs +0 -1
  172. package/dist/types.mjs.map +0 -1
  173. package/dist/variables.d.mts +0 -17
  174. package/dist/variables.d.ts +0 -17
  175. package/dist/variables.js +0 -412
  176. package/dist/variables.js.map +0 -1
  177. package/dist/variables.mjs +0 -391
  178. package/dist/variables.mjs.map +0 -1
@@ -0,0 +1,766 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.resolveFieldDef = void 0;
7
+ exports.collectFields = collectFields;
8
+ exports.collectSubfields = collectSubfields;
9
+ exports.getArgumentDefs = getArgumentDefs;
10
+ exports.valueFromAST = valueFromAST;
11
+ exports.computeLocations = computeLocations;
12
+ exports.addPath = addPath;
13
+ exports.flattenPath = flattenPath;
14
+ exports.serializeObjectPathForSkipInclude = serializeObjectPathForSkipInclude;
15
+ exports.joinSkipIncludePath = joinSkipIncludePath;
16
+ const graphql_1 = require("graphql");
17
+ const execution_js_1 = require("./execution.js");
18
+ const inspect_js_1 = __importDefault(require("./inspect.js"));
19
+ const compat_js_1 = require("./compat.js");
20
+ Object.defineProperty(exports, "resolveFieldDef", { enumerable: true, get: function () { return compat_js_1.resolveFieldDef; } });
21
+ const inspect = (0, inspect_js_1.default)();
22
+ /**
23
+ * Given a selectionSet, adds all of the fields in that selection to
24
+ * the passed in map of fields, and returns it at the end.
25
+ *
26
+ * CollectFields requires the "runtime type" of an object. For a field which
27
+ * returns an Interface or Union type, the "runtime type" will be the actual
28
+ * Object type returned by that field.
29
+ */
30
+ function collectFields(compilationContext, runtimeType, selectionSet, fields, visitedFragmentNames, parentResponsePath) {
31
+ return collectFieldsImpl(compilationContext, runtimeType, selectionSet, fields, visitedFragmentNames, undefined, serializeObjectPathForSkipInclude(parentResponsePath));
32
+ }
33
+ /**
34
+ * Implementation of collectFields defined above with extra parameters
35
+ * used for recursion and need not be exposed publically
36
+ */
37
+ function collectFieldsImpl(compilationContext, runtimeType, selectionSet, fields, visitedFragmentNames, previousShouldInclude = [], parentResponsePath = "") {
38
+ const stack = [];
39
+ // Push in reverse so that stack.pop() will process in document order
40
+ for (let i = selectionSet.selections.length - 1; i >= 0; i--) {
41
+ stack.push({
42
+ selection: selectionSet.selections[i],
43
+ parentResponsePath,
44
+ previousShouldInclude
45
+ });
46
+ }
47
+ while (stack.length > 0) {
48
+ const { selection, parentResponsePath, previousShouldInclude } = stack.pop();
49
+ switch (selection.kind) {
50
+ case graphql_1.Kind.FIELD: {
51
+ collectFieldsForField({
52
+ compilationContext,
53
+ fields,
54
+ parentResponsePath,
55
+ previousShouldInclude,
56
+ selection
57
+ });
58
+ break;
59
+ }
60
+ case graphql_1.Kind.INLINE_FRAGMENT: {
61
+ if (!doesFragmentConditionMatch(compilationContext, selection, runtimeType)) {
62
+ break;
63
+ }
64
+ // current fragment's shouldInclude
65
+ const compiledSkipInclude = compileSkipInclude(compilationContext, selection);
66
+ const mergedShouldInclude = joinShouldIncludeCompilations(previousShouldInclude, [compiledSkipInclude]);
67
+ // Push in reverse so that stack.pop() will process in document order
68
+ const selectionsLength = selection.selectionSet.selections.length;
69
+ for (let i = selectionsLength - 1; i >= 0; i--) {
70
+ stack.push({
71
+ selection: selection.selectionSet.selections[i],
72
+ parentResponsePath,
73
+ previousShouldInclude: mergedShouldInclude
74
+ });
75
+ }
76
+ break;
77
+ }
78
+ case graphql_1.Kind.FRAGMENT_SPREAD: {
79
+ const fragName = selection.name.value;
80
+ if (visitedFragmentNames[fragName]) {
81
+ break;
82
+ }
83
+ visitedFragmentNames[fragName] = true;
84
+ const fragment = compilationContext.fragments[fragName];
85
+ if (!fragment ||
86
+ !doesFragmentConditionMatch(compilationContext, fragment, runtimeType)) {
87
+ break;
88
+ }
89
+ // current fragment's shouldInclude
90
+ const compiledSkipInclude = compileSkipInclude(compilationContext, selection);
91
+ const mergedShouldInclude = joinShouldIncludeCompilations(previousShouldInclude, [compiledSkipInclude]);
92
+ // Push in reverse so that stack.pop() will process in document order
93
+ const selectionsLength = fragment.selectionSet.selections.length;
94
+ for (let i = selectionsLength - 1; i >= 0; i--) {
95
+ stack.push({
96
+ selection: fragment.selectionSet.selections[i],
97
+ parentResponsePath,
98
+ previousShouldInclude: mergedShouldInclude
99
+ });
100
+ }
101
+ break;
102
+ }
103
+ }
104
+ }
105
+ return fields;
106
+ }
107
+ function collectFieldsForField({ compilationContext, fields, parentResponsePath, previousShouldInclude, selection }) {
108
+ const name = getFieldEntryKey(selection);
109
+ if (!fields[name]) {
110
+ fields[name] = [];
111
+ }
112
+ const fieldNode = selection;
113
+ // the current path of the field
114
+ // This is used to generate per path skip/include code
115
+ // because the same field can be reached from different paths (e.g. fragment reuse)
116
+ const currentPath = joinSkipIncludePath(parentResponsePath,
117
+ // use alias(instead of selection.name.value) if available as the responsePath used for lookup uses alias
118
+ name);
119
+ // `should include`s generated for the current fieldNode
120
+ const compiledSkipInclude = compileSkipInclude(compilationContext, selection);
121
+ /**
122
+ * Carry over fragment's skip and include code
123
+ *
124
+ * fieldNode.__internalShouldInclude
125
+ * ---------------------------------
126
+ * When the parent field has a skip or include, the current one
127
+ * should be skipped if the parent is skipped in the path.
128
+ *
129
+ * previousShouldInclude
130
+ * ---------------------
131
+ * `should include`s from fragment spread and inline fragments
132
+ *
133
+ * compileSkipInclude(selection)
134
+ * -----------------------------
135
+ * `should include`s generated for the current fieldNode
136
+ */
137
+ if (compilationContext.options.useExperimentalPathBasedSkipInclude) {
138
+ if (!fieldNode.__internalShouldIncludePath)
139
+ fieldNode.__internalShouldIncludePath = {};
140
+ fieldNode.__internalShouldIncludePath[currentPath] =
141
+ joinShouldIncludeCompilations(fieldNode.__internalShouldIncludePath?.[currentPath] ?? [], previousShouldInclude, [compiledSkipInclude]);
142
+ }
143
+ else {
144
+ // @deprecated
145
+ fieldNode.__internalShouldInclude = joinShouldIncludeCompilations(fieldNode.__internalShouldInclude ?? [], previousShouldInclude, [compiledSkipInclude]);
146
+ }
147
+ /**
148
+ * We augment the entire subtree as the parent object's skip/include
149
+ * directives influence the child even if the child doesn't have
150
+ * skip/include on it's own.
151
+ *
152
+ * Refer the function definition for example.
153
+ */
154
+ augmentFieldNodeTree(compilationContext, fieldNode, currentPath);
155
+ fields[name].push(fieldNode);
156
+ }
157
+ /**
158
+ * Augment __internalShouldInclude code for all sub-fields in the
159
+ * tree with @param rootfieldNode as the root.
160
+ *
161
+ * This is required to handle cases where there are multiple paths to
162
+ * the same node. And each of those paths contain different skip/include
163
+ * values.
164
+ *
165
+ * For example,
166
+ *
167
+ * ```
168
+ * {
169
+ * foo @skip(if: $c1) {
170
+ * bar @skip(if: $c2)
171
+ * }
172
+ * ... {
173
+ * foo @skip(if: $c3) {
174
+ * bar
175
+ * }
176
+ * }
177
+ * }
178
+ * ```
179
+ *
180
+ * We decide shouldInclude at runtime per fieldNode. When we handle the
181
+ * field `foo`, the logic is straight forward - it requires one of $c1 or $c3
182
+ * to be false.
183
+ *
184
+ * But, when we handle the field `bar`, and we are in the context of the fieldNode,
185
+ * not enough information is available. This is because, if we only included $c2
186
+ * to decide if bar is included, consider the case -
187
+ *
188
+ * $c1: true, $c2: true, $c3: false
189
+ *
190
+ * If we considered only $c2, we would have skipped bar. But the correct implementation
191
+ * is to include bar, because foo($c3) { bar } is not skipped. The entire sub-tree's
192
+ * logic is required to handle bar.
193
+ *
194
+ * So, to handle this case, we augment the tree at each point to consider the
195
+ * skip/include logic from the parent as well.
196
+ *
197
+ * @param compilationContext {CompilationContext} Required for getFragment by
198
+ * name to handle fragment spread operation.
199
+ *
200
+ * @param rootFieldNode {JitFieldNode} The root field to traverse from for
201
+ * adding __internalShouldInclude to all sub field nodes.
202
+ *
203
+ * @param parentResponsePath {string} The response path of the parent field.
204
+ */
205
+ function augmentFieldNodeTree(compilationContext, rootFieldNode, parentResponsePath) {
206
+ for (const selection of rootFieldNode.selectionSet?.selections ?? []) {
207
+ const stack = [];
208
+ stack.push({
209
+ parentFieldNode: rootFieldNode,
210
+ selection,
211
+ comesFromFragmentSpread: false,
212
+ parentResponsePath
213
+ });
214
+ while (stack.length > 0) {
215
+ const { parentFieldNode, selection, comesFromFragmentSpread, parentResponsePath } = stack.pop();
216
+ switch (selection.kind) {
217
+ case graphql_1.Kind.FIELD: {
218
+ const jitFieldNode = selection;
219
+ const currentPath = joinSkipIncludePath(parentResponsePath,
220
+ // use alias(instead of selection.name.value) if available as the responsePath used for lookup uses alias
221
+ getFieldEntryKey(jitFieldNode));
222
+ if (!comesFromFragmentSpread) {
223
+ if (compilationContext.options.useExperimentalPathBasedSkipInclude) {
224
+ if (!jitFieldNode.__internalShouldIncludePath)
225
+ jitFieldNode.__internalShouldIncludePath = {};
226
+ jitFieldNode.__internalShouldIncludePath[currentPath] =
227
+ joinShouldIncludeCompilations(parentFieldNode.__internalShouldIncludePath?.[parentResponsePath] ?? [], jitFieldNode.__internalShouldIncludePath?.[currentPath] ?? []);
228
+ }
229
+ else {
230
+ // @deprecated
231
+ jitFieldNode.__internalShouldInclude =
232
+ joinShouldIncludeCompilations(parentFieldNode.__internalShouldInclude ?? [], jitFieldNode.__internalShouldInclude ?? []);
233
+ }
234
+ }
235
+ // go further down the query tree
236
+ for (const selection of jitFieldNode.selectionSet?.selections ?? []) {
237
+ stack.push({
238
+ parentFieldNode: jitFieldNode,
239
+ selection,
240
+ comesFromFragmentSpread: false,
241
+ parentResponsePath: currentPath
242
+ });
243
+ }
244
+ break;
245
+ }
246
+ case graphql_1.Kind.INLINE_FRAGMENT: {
247
+ for (const subSelection of selection.selectionSet.selections) {
248
+ stack.push({
249
+ parentFieldNode,
250
+ selection: subSelection,
251
+ comesFromFragmentSpread: true,
252
+ parentResponsePath
253
+ });
254
+ }
255
+ break;
256
+ }
257
+ case graphql_1.Kind.FRAGMENT_SPREAD: {
258
+ const fragment = compilationContext.fragments[selection.name.value];
259
+ for (const subSelection of fragment.selectionSet.selections) {
260
+ stack.push({
261
+ parentFieldNode,
262
+ selection: subSelection,
263
+ comesFromFragmentSpread: true,
264
+ parentResponsePath
265
+ });
266
+ }
267
+ }
268
+ }
269
+ }
270
+ }
271
+ }
272
+ /**
273
+ * Joins a list of shouldInclude compiled code into a single logical
274
+ * statement.
275
+ *
276
+ * The operation is `&&` because, it is used to join parent->child
277
+ * relations in the query tree. Note: parent can be either parent field
278
+ * or fragment.
279
+ *
280
+ * For example,
281
+ * {
282
+ * foo @skip(if: $c1) {
283
+ * ... @skip(if: $c2) {
284
+ * bar @skip(if: $c3)
285
+ * }
286
+ * }
287
+ * }
288
+ *
289
+ * Only when a parent is included, the child is included. So, we use `&&`.
290
+ *
291
+ * compilationFor($c1) && compilationFor($c2) && compilationFor($c3)
292
+ *
293
+ * @param compilations
294
+ */
295
+ function joinShouldIncludeCompilations(...compilations) {
296
+ // remove "true" since we are joining with '&&' as `true && X` = `X`
297
+ // This prevents an explosion of `&& true` which could break
298
+ // V8's internal size limit for string.
299
+ //
300
+ // Note: the `true` appears if a field does not have a skip/include directive
301
+ // So, the more nested the query is, the more of unnecessary `&& true`
302
+ // we get.
303
+ //
304
+ // Failing to do this results in [RangeError: invalid array length]
305
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_array_length
306
+ const conditionsSet = new Set();
307
+ for (const conditions of compilations) {
308
+ for (const condition of conditions) {
309
+ if (condition !== "true") {
310
+ conditionsSet.add(condition);
311
+ }
312
+ }
313
+ }
314
+ return Array.from(conditionsSet);
315
+ }
316
+ /**
317
+ * Compiles directives `skip` and `include` and generates the compilation
318
+ * code based on GraphQL specification.
319
+ *
320
+ * @param node {SelectionNode} The selection node (field/fragment/inline-fragment)
321
+ * for which we generate the compiled skipInclude.
322
+ */
323
+ function compileSkipInclude(compilationContext, node) {
324
+ // minor optimization to avoid compilation if there are no directives
325
+ if (node.directives == null || node.directives.length < 1) {
326
+ return "true";
327
+ }
328
+ const { skipValue, includeValue } = compileSkipIncludeDirectiveValues(compilationContext, node);
329
+ /**
330
+ * Spec: https://spec.graphql.org/June2018/#sec--include
331
+ *
332
+ * Neither @skip nor @include has precedence over the other.
333
+ * In the case that both the @skip and @include directives
334
+ * are provided in on the same the field or fragment, it must
335
+ * be queried only if the @skip condition is false and the
336
+ * @include condition is true. Stated conversely, the field
337
+ * or fragment must not be queried if either the @skip
338
+ * condition is true or the @include condition is false.
339
+ */
340
+ if (skipValue != null && includeValue != null) {
341
+ return `${skipValue} === false && ${includeValue} === true`;
342
+ }
343
+ else if (skipValue != null) {
344
+ return `(${skipValue} === false)`;
345
+ }
346
+ else if (includeValue != null) {
347
+ return `(${includeValue} === true)`;
348
+ }
349
+ else {
350
+ return `true`;
351
+ }
352
+ }
353
+ /**
354
+ * Compile skip or include directive values into JIT compatible
355
+ * runtime code.
356
+ *
357
+ * @param node {SelectionNode}
358
+ */
359
+ function compileSkipIncludeDirectiveValues(compilationContext, node) {
360
+ const skipDirective = node.directives?.find((it) => it.name.value === graphql_1.GraphQLSkipDirective.name);
361
+ const includeDirective = node.directives?.find((it) => it.name.value === graphql_1.GraphQLIncludeDirective.name);
362
+ const skipValue = skipDirective
363
+ ? compileSkipIncludeDirective(compilationContext, skipDirective)
364
+ : // The null here indicates the absense of the directive
365
+ // which is later used to determine if both skip and include
366
+ // are present
367
+ null;
368
+ const includeValue = includeDirective
369
+ ? compileSkipIncludeDirective(compilationContext, includeDirective)
370
+ : // The null here indicates the absense of the directive
371
+ // which is later used to determine if both skip and include
372
+ // are present
373
+ null;
374
+ return { skipValue, includeValue };
375
+ }
376
+ /**
377
+ * Compile the skip/include directive node. Resolve variables to it's
378
+ * path from context, resolve scalars to their respective values.
379
+ *
380
+ * @param directive {DirectiveNode}
381
+ */
382
+ function compileSkipIncludeDirective(compilationContext, directive) {
383
+ const ifNode = directive.arguments?.find((it) => it.name.value === "if");
384
+ if (ifNode == null) {
385
+ throw new graphql_1.GraphQLError(`Directive '${directive.name.value}' is missing required arguments: 'if'`, (0, compat_js_1.getGraphQLErrorOptions)([directive]));
386
+ }
387
+ switch (ifNode.value.kind) {
388
+ case graphql_1.Kind.VARIABLE:
389
+ validateSkipIncludeVariableType(compilationContext, ifNode.value);
390
+ return `${execution_js_1.GLOBAL_VARIABLES_NAME}["${ifNode.value.name.value}"]`;
391
+ case graphql_1.Kind.BOOLEAN:
392
+ return `${ifNode.value.value.toString()}`;
393
+ default:
394
+ throw new graphql_1.GraphQLError(`Argument 'if' on Directive '${directive.name.value}' has an invalid value (${(0, graphql_1.valueFromASTUntyped)(ifNode.value)}). Expected type 'Boolean!'`, (0, compat_js_1.getGraphQLErrorOptions)([ifNode]));
395
+ }
396
+ }
397
+ /**
398
+ * Validate the skip and include directive's argument values at compile time.
399
+ *
400
+ * This validation step is required as these directives are part of an
401
+ * implicit schema in GraphQL.
402
+ *
403
+ * @param compilationContext {CompilationContext}
404
+ * @param variable {VariableNode} the variable used in 'if' argument of the skip/include directive
405
+ */
406
+ function validateSkipIncludeVariableType(compilationContext, variable) {
407
+ const variableDefinition = compilationContext.operation.variableDefinitions?.find((it) => it.variable.name.value === variable.name.value);
408
+ if (variableDefinition == null) {
409
+ throw new graphql_1.GraphQLError(`Variable '${variable.name.value}' is not defined`, (0, compat_js_1.getGraphQLErrorOptions)([variable]));
410
+ }
411
+ // Part of Spec text: https://spec.graphql.org/June2018/#sec-All-Variable-Usages-are-Allowed
412
+ if (!(
413
+ // The variable defintion is a Non-nullable Boolean type
414
+ ((variableDefinition.type.kind === graphql_1.Kind.NON_NULL_TYPE &&
415
+ variableDefinition.type.type.kind === graphql_1.Kind.NAMED_TYPE &&
416
+ variableDefinition.type.type.name.value === "Boolean") ||
417
+ // or the variable definition is a nullable Boolean type with a default value
418
+ (variableDefinition.type.kind === graphql_1.Kind.NAMED_TYPE &&
419
+ variableDefinition.type.name.value === "Boolean" &&
420
+ variableDefinition.defaultValue != null)))) {
421
+ throw new graphql_1.GraphQLError(`Variable '${variable.name.value}' of type '${typeNodeToString(variableDefinition.type)}' used in position expecting type 'Boolean!'`, (0, compat_js_1.getGraphQLErrorOptions)([variableDefinition]));
422
+ }
423
+ }
424
+ /**
425
+ * Print the string representation of the TypeNode for error messages
426
+ *
427
+ * @param type {TypeNode} type node to be converted to string representation
428
+ */
429
+ function typeNodeToString(type) {
430
+ switch (type.kind) {
431
+ case graphql_1.Kind.NAMED_TYPE:
432
+ return type.name.value;
433
+ case graphql_1.Kind.NON_NULL_TYPE:
434
+ return `${typeNodeToString(type.type)}!`;
435
+ case graphql_1.Kind.LIST_TYPE:
436
+ return `[${typeNodeToString(type.type)}]`;
437
+ }
438
+ }
439
+ /**
440
+ * Determines if a fragment is applicable to the given type.
441
+ */
442
+ function doesFragmentConditionMatch(compilationContext, fragment, type) {
443
+ const typeConditionNode = fragment.typeCondition;
444
+ if (!typeConditionNode) {
445
+ return true;
446
+ }
447
+ const conditionalType = (0, graphql_1.typeFromAST)(compilationContext.schema, typeConditionNode);
448
+ if (conditionalType === type) {
449
+ return true;
450
+ }
451
+ if (!conditionalType) {
452
+ return false;
453
+ }
454
+ if ((0, graphql_1.isAbstractType)(conditionalType)) {
455
+ return compilationContext.schema.isSubType(conditionalType, type);
456
+ }
457
+ return false;
458
+ }
459
+ /**
460
+ * Implements the logic to compute the key of a given field's entry
461
+ */
462
+ function getFieldEntryKey(node) {
463
+ return node.alias ? node.alias.value : node.name.value;
464
+ }
465
+ function collectSubfields(compilationContext, returnType, fieldNodes, parentResponsePath) {
466
+ let subFieldNodes = Object.create(null);
467
+ const visitedFragmentNames = Object.create(null);
468
+ for (const fieldNode of fieldNodes) {
469
+ const selectionSet = fieldNode.selectionSet;
470
+ if (selectionSet) {
471
+ subFieldNodes = collectFields(compilationContext, returnType, selectionSet, subFieldNodes, visitedFragmentNames, parentResponsePath);
472
+ }
473
+ }
474
+ return subFieldNodes;
475
+ }
476
+ /**
477
+ * Prepares an object map of argument values given a list of argument
478
+ * definitions and list of argument AST nodes.
479
+ *
480
+ * Note: The returned value is a plain Object with a prototype, since it is
481
+ * exposed to user code. Care should be taken to not pull values from the
482
+ * Object prototype.
483
+ */
484
+ function getArgumentDefs(def, node) {
485
+ const values = {};
486
+ const missing = [];
487
+ const argDefs = def.args;
488
+ const argNodes = node.arguments || [];
489
+ const argNodeMap = keyMap(argNodes, (arg) => arg.name.value);
490
+ for (const argDef of argDefs) {
491
+ const name = argDef.name;
492
+ if ((0, compat_js_1.hasDefaultValue)(argDef)) {
493
+ // handle both v16 defaultValue and v17 default.value
494
+ values[name] = (0, compat_js_1.getDefaultValue)(argDef);
495
+ }
496
+ const argType = argDef.type;
497
+ const argumentNode = argNodeMap[name];
498
+ let hasVariables = false;
499
+ if (argumentNode && argumentNode.value.kind === graphql_1.Kind.VARIABLE) {
500
+ hasVariables = true;
501
+ missing.push({
502
+ valueNode: argumentNode.value,
503
+ path: addPath(undefined, name, "literal"),
504
+ argument: { definition: argDef, node: argumentNode }
505
+ });
506
+ }
507
+ else if (argumentNode) {
508
+ const coercedValue = valueFromAST(argumentNode.value, argType);
509
+ if (coercedValue === undefined) {
510
+ // Note: ValuesOfCorrectType validation should catch this before
511
+ // execution. This is a runtime check to ensure execution does not
512
+ // continue with an invalid argument value.
513
+ throw new graphql_1.GraphQLError(`Argument "${name}" of type "${argType}" has invalid value ${(0, graphql_1.print)(argumentNode.value)}.`, (0, compat_js_1.getGraphQLErrorOptions)(argumentNode.value));
514
+ }
515
+ if (isASTValueWithVariables(coercedValue)) {
516
+ missing.push(...coercedValue.variables.map(({ valueNode, path }) => ({
517
+ valueNode,
518
+ path: addPath(path, name, "literal")
519
+ })));
520
+ }
521
+ values[name] = coercedValue.value;
522
+ }
523
+ if ((0, graphql_1.isNonNullType)(argType) && values[name] === undefined && !hasVariables) {
524
+ // If no value or a nullish value was provided to a variable with a
525
+ // non-null type (required), produce an error.
526
+ throw new graphql_1.GraphQLError(argumentNode
527
+ ? `Argument "${name}" of non-null type ` +
528
+ `"${argType}" must not be null.`
529
+ : `Argument "${name}" of required type ` +
530
+ `"${argType}" was not provided.`, (0, compat_js_1.getGraphQLErrorOptions)(node));
531
+ }
532
+ }
533
+ return { values, missing };
534
+ }
535
+ function isASTValueWithVariables(x) {
536
+ return !!x.variables;
537
+ }
538
+ function valueFromAST(valueNode, type) {
539
+ if ((0, graphql_1.isNonNullType)(type)) {
540
+ if (valueNode.kind === graphql_1.Kind.NULL) {
541
+ return; // Invalid: intentionally return no value.
542
+ }
543
+ return valueFromAST(valueNode, type.ofType);
544
+ }
545
+ if (valueNode.kind === graphql_1.Kind.NULL) {
546
+ // This is explicitly returning the value null.
547
+ return {
548
+ value: null
549
+ };
550
+ }
551
+ if (valueNode.kind === graphql_1.Kind.VARIABLE) {
552
+ return { value: null, variables: [{ valueNode, path: undefined }] };
553
+ }
554
+ if ((0, graphql_1.isListType)(type)) {
555
+ const itemType = type.ofType;
556
+ if (valueNode.kind === graphql_1.Kind.LIST) {
557
+ const coercedValues = [];
558
+ const variables = [];
559
+ const itemNodes = valueNode.values;
560
+ for (let i = 0; i < itemNodes.length; i++) {
561
+ const itemNode = itemNodes[i];
562
+ if (itemNode.kind === graphql_1.Kind.VARIABLE) {
563
+ coercedValues.push(null);
564
+ variables.push({
565
+ valueNode: itemNode,
566
+ path: addPath(undefined, i.toString(), "literal")
567
+ });
568
+ }
569
+ else {
570
+ const itemValue = valueFromAST(itemNode, itemType);
571
+ if (!itemValue) {
572
+ return; // Invalid: intentionally return no value.
573
+ }
574
+ coercedValues.push(itemValue.value);
575
+ if (isASTValueWithVariables(itemValue)) {
576
+ variables.push(...itemValue.variables.map(({ valueNode, path }) => ({
577
+ valueNode,
578
+ path: addPath(path, i.toString(), "literal")
579
+ })));
580
+ }
581
+ }
582
+ }
583
+ return { value: coercedValues, variables };
584
+ }
585
+ // Single item which will be coerced to a list
586
+ const coercedValue = valueFromAST(valueNode, itemType);
587
+ if (coercedValue === undefined) {
588
+ return; // Invalid: intentionally return no value.
589
+ }
590
+ if (isASTValueWithVariables(coercedValue)) {
591
+ return {
592
+ value: [coercedValue.value],
593
+ variables: coercedValue.variables.map(({ valueNode, path }) => ({
594
+ valueNode,
595
+ path: addPath(path, "0", "literal")
596
+ }))
597
+ };
598
+ }
599
+ return { value: [coercedValue.value] };
600
+ }
601
+ if ((0, graphql_1.isInputObjectType)(type)) {
602
+ if (valueNode.kind !== graphql_1.Kind.OBJECT) {
603
+ return; // Invalid: intentionally return no value.
604
+ }
605
+ const coercedObj = Object.create(null);
606
+ const variables = [];
607
+ const fieldNodes = keyMap(valueNode.fields, (field) => field.name.value);
608
+ const fields = Object.values(type.getFields());
609
+ for (const field of fields) {
610
+ if ((0, compat_js_1.hasDefaultValue)(field)) {
611
+ coercedObj[field.name] = (0, compat_js_1.getDefaultValue)(field);
612
+ }
613
+ const fieldNode = fieldNodes[field.name];
614
+ if (!fieldNode) {
615
+ continue;
616
+ }
617
+ const fieldValue = valueFromAST(fieldNode.value, field.type);
618
+ if (!fieldValue) {
619
+ return; // Invalid: intentionally return no value.
620
+ }
621
+ if (isASTValueWithVariables(fieldValue)) {
622
+ variables.push(...fieldValue.variables.map(({ valueNode, path }) => ({
623
+ valueNode,
624
+ path: addPath(path, field.name, "literal")
625
+ })));
626
+ }
627
+ coercedObj[field.name] = fieldValue.value;
628
+ }
629
+ return { value: coercedObj, variables };
630
+ }
631
+ if ((0, graphql_1.isEnumType)(type)) {
632
+ if (valueNode.kind !== graphql_1.Kind.ENUM) {
633
+ return; // Invalid: intentionally return no value.
634
+ }
635
+ const enumValue = type.getValue(valueNode.value);
636
+ if (!enumValue) {
637
+ return; // Invalid: intentionally return no value.
638
+ }
639
+ return { value: enumValue.value };
640
+ }
641
+ if ((0, graphql_1.isScalarType)(type)) {
642
+ // Scalars fulfill parsing a literal value via coerceInputLiteral().
643
+ // Invalid values represent a failure to parse correctly, in which case
644
+ // no value is returned.
645
+ let result;
646
+ try {
647
+ // By this point, we've filtered out VARIABLE nodes, so valueNode is a ConstValueNode
648
+ result = (0, compat_js_1.coerceInputLiteral)(type, valueNode);
649
+ }
650
+ catch (error) {
651
+ return; // Invalid: intentionally return no value.
652
+ }
653
+ if (isInvalid(result)) {
654
+ return; // Invalid: intentionally return no value.
655
+ }
656
+ return { value: result };
657
+ }
658
+ // Not reachable. All possible input types have been considered.
659
+ /* istanbul ignore next */
660
+ throw new Error(`Unexpected input type: "${inspect(type)}".`);
661
+ }
662
+ /**
663
+ * Creates a keyed JS object from an array, given a function to produce the keys
664
+ * for each value in the array.
665
+ *
666
+ * This provides a convenient lookup for the array items if the key function
667
+ * produces unique results.
668
+ *
669
+ * const phoneBook = [
670
+ * { name: 'Jon', num: '555-1234' },
671
+ * { name: 'Jenny', num: '867-5309' }
672
+ * ]
673
+ *
674
+ * // { Jon: { name: 'Jon', num: '555-1234' },
675
+ * // Jenny: { name: 'Jenny', num: '867-5309' } }
676
+ * const entriesByName = keyMap(
677
+ * phoneBook,
678
+ * entry => entry.name
679
+ * )
680
+ *
681
+ * // { name: 'Jenny', num: '857-6309' }
682
+ * const jennyEntry = entriesByName['Jenny']
683
+ *
684
+ */
685
+ function keyMap(list, keyFn) {
686
+ return list.reduce(
687
+ // eslint-disable-next-line no-sequences
688
+ (map, item) => ((map[keyFn(item)] = item), map), Object.create(null));
689
+ }
690
+ function computeLocations(nodes) {
691
+ return nodes.reduce((list, node) => {
692
+ if (node.loc) {
693
+ list.push(getLocation(node.loc));
694
+ }
695
+ return list;
696
+ }, []);
697
+ }
698
+ /**
699
+ * This is an alternate faster implementation of getLocation in graphql-js
700
+ *
701
+ * Optimization:
702
+ * In graphql-js, getLocation is implemented as finding line and column
703
+ * from a given position in the source. Since the use-case in GraphQL-JIT
704
+ * is to find the location of a node, we can directly use the line and
705
+ * column from the startToken of the node's location.
706
+ *
707
+ * @param loc the Node's location
708
+ * @returns the SourceLocation for the position
709
+ */
710
+ function getLocation(loc) {
711
+ // If the location already contains line and column, return it directly
712
+ return {
713
+ line: loc.startToken.line,
714
+ column: loc.startToken.column
715
+ };
716
+ }
717
+ function addPath(responsePath, key, type = "literal") {
718
+ return { prev: responsePath, key, type };
719
+ }
720
+ function flattenPath(path) {
721
+ const flattened = [];
722
+ let curr = path;
723
+ while (curr) {
724
+ flattened.push({ key: curr.key, type: curr.type });
725
+ curr = curr.prev;
726
+ }
727
+ return flattened;
728
+ }
729
+ /**
730
+ * Serialize a path for use in the skip/include directives.
731
+ *
732
+ * @param path The path to serialize
733
+ * @returns The path serialized as a string, with the root path first.
734
+ */
735
+ function serializeObjectPathForSkipInclude(path) {
736
+ let serialized = "";
737
+ let curr = path;
738
+ while (curr) {
739
+ if (curr.type === "literal") {
740
+ serialized = joinSkipIncludePath(curr.key, serialized);
741
+ }
742
+ curr = curr.prev;
743
+ }
744
+ return serialized;
745
+ }
746
+ /**
747
+ * join two path segments to a dot notation, handling empty strings
748
+ *
749
+ * @param a path segment
750
+ * @param b path segment
751
+ * @returns combined path in dot notation
752
+ */
753
+ function joinSkipIncludePath(a, b) {
754
+ if (a) {
755
+ if (b) {
756
+ return `${a}.${b}`;
757
+ }
758
+ return a;
759
+ }
760
+ return b;
761
+ }
762
+ function isInvalid(value) {
763
+ // eslint-disable-next-line no-self-compare
764
+ return value === undefined || value !== value;
765
+ }
766
+ //# sourceMappingURL=ast.js.map