goatly 0.1.0 → 0.1.2

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 (161) hide show
  1. package/README.md +874 -1
  2. package/bin/goatly.js +99 -0
  3. package/dist/cli/index.js +234 -0
  4. package/dist/compat/gatsby-node-entry.js +16 -0
  5. package/dist/compat/gatsby-package.json +6 -0
  6. package/dist/compat/md5-file.js +47 -0
  7. package/dist/compat-apis.json +72 -0
  8. package/dist/compat-apis.json.js +62 -0
  9. package/dist/graphql.js +3 -0
  10. package/dist/node/actions/index.js +619 -0
  11. package/dist/node/actions/job-store.js +180 -0
  12. package/dist/node/actions/jobs.js +174 -0
  13. package/dist/node/api-runner.js +258 -0
  14. package/dist/node/bootstrap.js +569 -0
  15. package/dist/node/build/assets.js +110 -0
  16. package/dist/node/build/incremental.js +208 -0
  17. package/dist/node/build/index.js +763 -0
  18. package/dist/node/build/memory-governor.js +450 -0
  19. package/dist/node/build/output-dirs.js +70 -0
  20. package/dist/node/build/page-data.js +40 -0
  21. package/dist/node/build/page-files.js +186 -0
  22. package/dist/node/build/partitioned-state.js +258 -0
  23. package/dist/node/build/query-worker.js +626 -0
  24. package/dist/node/build/query-workers.js +863 -0
  25. package/dist/node/build/render-thread.js +103 -0
  26. package/dist/node/build/render-worker.js +162 -0
  27. package/dist/node/build/state.js +24 -0
  28. package/dist/node/build/timing-breakdown.js +216 -0
  29. package/dist/node/build/work-clock.js +75 -0
  30. package/dist/node/build/worker-pool.js +242 -0
  31. package/dist/node/cache-layout.js +99 -0
  32. package/dist/node/cache.js +1430 -0
  33. package/dist/node/compat/chokidar-shim.js +148 -0
  34. package/dist/node/compat/core-utils-lmdb.js +145 -0
  35. package/dist/node/compat/create-node-id-shim.js +1 -0
  36. package/dist/node/compat/module-hooks.js +245 -0
  37. package/dist/node/compat/redux-shim.js +1 -0
  38. package/dist/node/config/index.js +162 -0
  39. package/dist/node/config/not-found-pages.js +95 -0
  40. package/dist/node/datastore/codec.js +144 -0
  41. package/dist/node/datastore/index.js +127 -0
  42. package/dist/node/datastore/logfile/cache.js +89 -0
  43. package/dist/node/datastore/logfile/index.js +1329 -0
  44. package/dist/node/datastore/logfile/record.js +71 -0
  45. package/dist/node/datastore/types.js +1 -0
  46. package/dist/node/develop/express-compat.js +207 -0
  47. package/dist/node/develop/graphiql.js +22 -0
  48. package/dist/node/develop/hydration-report.js +41 -0
  49. package/dist/node/develop/index.js +618 -0
  50. package/dist/node/develop/local-url.js +10 -0
  51. package/dist/node/develop/ssr-runner.js +66 -0
  52. package/dist/node/gql.js +5 -0
  53. package/dist/node/index.js +15 -0
  54. package/dist/node/info.js +270 -0
  55. package/dist/node/internal-plugins/data-bridge.js +196 -0
  56. package/dist/node/internal-plugins/page-creator.js +318 -0
  57. package/dist/node/loader/import-module.js +160 -0
  58. package/dist/node/node-helpers.js +66 -0
  59. package/dist/node/not-found-pages.js +220 -0
  60. package/dist/node/plugins/api-list.js +12 -0
  61. package/dist/node/plugins/load-plugins.js +427 -0
  62. package/dist/node/plugins/resolve-plugin.js +79 -0
  63. package/dist/node/plugins/static-exports.js +64 -0
  64. package/dist/node/query/compile.js +326 -0
  65. package/dist/node/query/dependencies.js +45 -0
  66. package/dist/node/query/execute.js +417 -0
  67. package/dist/node/query/extract.js +372 -0
  68. package/dist/node/query/gql-tada-introspection.js +124 -0
  69. package/dist/node/query/graphql-env.js +231 -0
  70. package/dist/node/query/graphql-runner.js +79 -0
  71. package/dist/node/redux/actions.js +1 -0
  72. package/dist/node/redux/index.js +133 -0
  73. package/dist/node/redux/node-changes.js +128 -0
  74. package/dist/node/redux/reducers.js +193 -0
  75. package/dist/node/redux/state.js +71 -0
  76. package/dist/node/renderer.js +124 -0
  77. package/dist/node/reporter-transports.js +259 -0
  78. package/dist/node/reporter.js +497 -0
  79. package/dist/node/schema/date.js +539 -0
  80. package/dist/node/schema/extensions.js +315 -0
  81. package/dist/node/schema/index.js +542 -0
  82. package/dist/node/schema/infer.js +388 -0
  83. package/dist/node/schema/inline-objects.js +128 -0
  84. package/dist/node/schema/naming.js +20 -0
  85. package/dist/node/schema/node-model.js +207 -0
  86. package/dist/node/schema/query-engine.js +1808 -0
  87. package/dist/node/schema/resolver-kinds.js +7 -0
  88. package/dist/node/schema/resolvers.js +273 -0
  89. package/dist/node/schema/type-builders.js +45 -0
  90. package/dist/node/schema/types.js +241 -0
  91. package/dist/node/serve.js +90 -0
  92. package/dist/node/site-page-nodes.js +277 -0
  93. package/dist/node/types.js +1 -0
  94. package/dist/node/utils/content-digest.js +107 -0
  95. package/dist/node/utils/cpus.js +104 -0
  96. package/dist/node/utils/create-node-id.js +51 -0
  97. package/dist/node/utils/env.js +25 -0
  98. package/dist/node/utils/fs-retry.js +74 -0
  99. package/dist/node/utils/memory-signals.js +441 -0
  100. package/dist/node/utils/memory.js +657 -0
  101. package/dist/node/utils/murmurhash.js +32 -0
  102. package/dist/node/utils/parser.js +33 -0
  103. package/dist/node/utils/partition-hash.js +19 -0
  104. package/dist/node/utils/paths.js +122 -0
  105. package/dist/node/utils/resolver.js +48 -0
  106. package/dist/node/utils/runtime.js +5 -0
  107. package/dist/node/utils/serializable.js +41 -0
  108. package/dist/node/utils/strings.js +41 -0
  109. package/dist/node/vite/config.js +289 -0
  110. package/dist/node/vite/paths.js +12 -0
  111. package/dist/node/vite/plugin-deps.js +232 -0
  112. package/dist/node/vite/plugin.js +283 -0
  113. package/dist/node/vite/react-compiler.js +362 -0
  114. package/dist/node/vite/source-files.js +83 -0
  115. package/dist/node/vite/static-image.js +136 -0
  116. package/dist/node/vite/styled-components.js +73 -0
  117. package/dist/node/vite/transforms.js +330 -0
  118. package/dist/node/wasm-data/host.js +406 -0
  119. package/dist/node/wasm-data/lower.js +406 -0
  120. package/dist/node/wasm-data/runner.js +79 -0
  121. package/dist/node/wasm-data/worker.js +95 -0
  122. package/dist/reporter.js +5 -0
  123. package/dist/runtime/api-runner.js +81 -0
  124. package/dist/runtime/app.js +256 -0
  125. package/dist/runtime/client-entry.js +4 -0
  126. package/dist/runtime/client.js +383 -0
  127. package/dist/runtime/default-html.js +31 -0
  128. package/dist/runtime/head-markup.js +100 -0
  129. package/dist/runtime/head-sync.js +101 -0
  130. package/dist/runtime/head-tags.js +45 -0
  131. package/dist/runtime/hydration-report.js +111 -0
  132. package/dist/runtime/index.js +12 -0
  133. package/dist/runtime/link.js +293 -0
  134. package/dist/runtime/loader.js +449 -0
  135. package/dist/runtime/not-found.js +61 -0
  136. package/dist/runtime/page-renderer.js +35 -0
  137. package/dist/runtime/preamble.js +47 -0
  138. package/dist/runtime/react-compat.js +14 -0
  139. package/dist/runtime/renderer-shared.js +10 -0
  140. package/dist/runtime/renderers/react-client.js +14 -0
  141. package/dist/runtime/renderers/react-server.js +99 -0
  142. package/dist/runtime/renderers/react.js +17 -0
  143. package/dist/runtime/renderers/types.js +1 -0
  144. package/dist/runtime/root-error-handlers.js +29 -0
  145. package/dist/runtime/router/history.js +132 -0
  146. package/dist/runtime/router/index.js +240 -0
  147. package/dist/runtime/router/utils.js +118 -0
  148. package/dist/runtime/script.js +110 -0
  149. package/dist/runtime/scroll.js +40 -0
  150. package/dist/runtime/slice.js +11 -0
  151. package/dist/runtime/ssr.js +240 -0
  152. package/dist/runtime/static-query.js +47 -0
  153. package/dist/utils.js +4 -0
  154. package/package.json +105 -3
  155. package/types/graphql-tada.d.ts +223 -0
  156. package/types/graphql.d.ts +2 -0
  157. package/types/index.d.ts +2287 -0
  158. package/types/node.d.ts +389 -0
  159. package/types/reporter.d.ts +187 -0
  160. package/types/router.d.ts +102 -0
  161. package/types/vendor/gql-tada.d.ts +2529 -0
@@ -0,0 +1,417 @@
1
+ import { GraphQLBoolean, GraphQLFloat, GraphQLID, GraphQLIncludeDirective, GraphQLInt, GraphQLSkipDirective, GraphQLString, Kind, SchemaMetaFieldDef, TypeMetaFieldDef, TypeNameMetaFieldDef, assertValidSchema, defaultTypeResolver, execute, getArgumentValues, getDirectiveValues, getNamedType, getVariableValues, isAbstractType, isLeafType, isListType, isNonNullType, isObjectType, typeFromAST } from "../gql.js";
2
+ import { sharesArgs, syncVariantOf } from "../schema/inline-objects.js";
3
+ // src/node/query/execute.ts
4
+ /** a plan per document and schema (undefined: graphql-js executes the document) */
5
+ const plans = /* @__PURE__ */ new WeakMap();
6
+ /** queries graphql-js executed instead (unsupported documents, errors): for tests */
7
+ const executeStats = {
8
+ planned: 0,
9
+ fallbacks: 0
10
+ };
11
+ /** graphql-js' execute, after the plan could not be used */
12
+ function fallback(args) {
13
+ executeStats.fallbacks++;
14
+ return execute(args);
15
+ }
16
+ /** graphql-js' isPromise (primitives first: no `then` lookup on their prototype) */
17
+ const isPromise = (value) => (typeof value === "object" && value !== null || typeof value === "function") && typeof value.then === "function";
18
+ /**
19
+ * `execute()` of graphql-js for the queries goatly runs (page, static and API queries):
20
+ * the same result, faster. Rejects nothing graphql-js would not reject.
21
+ */
22
+ function executeQuery(args, options = {}) {
23
+ const plan = planFor(args.schema, args.document);
24
+ if (!plan || args.variableValues != null && typeof args.variableValues !== "object") return fallback(args);
25
+ const coerced = getVariableValues(args.schema, plan.operation.variableDefinitions ?? [], args.variableValues ?? {}, { maxErrors: 50 });
26
+ if (coerced.errors) return fallback(args);
27
+ executeStats.planned++;
28
+ const run = {
29
+ schema: args.schema,
30
+ fragments: plan.fragments,
31
+ operation: plan.operation,
32
+ variableValues: coerced.coerced,
33
+ context: args.contextValue,
34
+ args: void 0,
35
+ plainObjects: options.plainObjects === true
36
+ };
37
+ let data;
38
+ try {
39
+ data = executeFields(run, plan.root, void 0, void 0);
40
+ } catch {
41
+ return fallback(args);
42
+ }
43
+ if (isPromise(data)) return data.then((value) => ({ data: value }), () => fallback(args));
44
+ return { data };
45
+ }
46
+ function planFor(schema, document) {
47
+ const cached = plans.get(document);
48
+ if (cached && cached.schema === schema) return cached.plan;
49
+ let plan;
50
+ try {
51
+ assertValidSchema(schema);
52
+ plan = compile(schema, document);
53
+ } catch {
54
+ plan = void 0;
55
+ }
56
+ plans.set(document, {
57
+ schema,
58
+ plan
59
+ });
60
+ return plan;
61
+ }
62
+ function compile(schema, document) {
63
+ let operation;
64
+ const fragments = Object.create(null);
65
+ for (const definition of document.definitions) if (definition.kind === Kind.OPERATION_DEFINITION) {
66
+ if (operation) return void 0;
67
+ operation = definition;
68
+ } else if (definition.kind === Kind.FRAGMENT_DEFINITION) fragments[definition.name.value] = definition;
69
+ if (!operation || operation.operation !== "query") return void 0;
70
+ const root = schema.getQueryType();
71
+ if (!root || !supported(document)) return void 0;
72
+ const plan = {
73
+ schema,
74
+ operation,
75
+ fragments,
76
+ root: void 0
77
+ };
78
+ plan.root = {
79
+ plan,
80
+ type: root,
81
+ nodes: []
82
+ };
83
+ plan.root.fields = fieldPlans(plan, root, [operation.selectionSet]);
84
+ if (plan.root.fields.some((f) => f.def === SchemaMetaFieldDef || f.def === TypeMetaFieldDef)) return void 0;
85
+ return plan;
86
+ }
87
+ /**
88
+ * Documents whose field collection does not depend on variables (`@skip`/`@include` with
89
+ * literal arguments only) and whose response names are plain object keys.
90
+ */
91
+ function supported(node) {
92
+ if (!node || typeof node !== "object") return true;
93
+ if (Array.isArray(node)) return node.every(supported);
94
+ const n = node;
95
+ if (n.kind === Kind.DIRECTIVE) {
96
+ const name = n.name.value;
97
+ if ((name === "skip" || name === "include") && n.arguments?.some(usesVariables)) return false;
98
+ return true;
99
+ }
100
+ if (n.kind === Kind.FIELD && (n.alias?.value ?? n.name.value) === "__proto__") return false;
101
+ if (n.kind === Kind.OPERATION_DEFINITION) return supported(n.selectionSet);
102
+ if (n.kind === Kind.FRAGMENT_DEFINITION) return supported(n.selectionSet);
103
+ if (n.kind === Kind.DOCUMENT) return supported(n.definitions);
104
+ if (n.kind === Kind.SELECTION_SET) return supported(n.selections);
105
+ return supported(n.directives) && supported(n.selectionSet);
106
+ }
107
+ function usesVariables(node) {
108
+ if (!node || typeof node !== "object") return false;
109
+ if (Array.isArray(node)) return node.some(usesVariables);
110
+ if (node.kind === Kind.VARIABLE) return true;
111
+ for (const key in node) {
112
+ if (key === "loc") continue;
113
+ if (usesVariables(node[key])) return true;
114
+ }
115
+ return false;
116
+ }
117
+ function shouldInclude(node) {
118
+ if (!node.directives?.length) return true;
119
+ if (getDirectiveValues(GraphQLSkipDirective, node, {})?.if === true) return false;
120
+ return getDirectiveValues(GraphQLIncludeDirective, node, {})?.if !== false;
121
+ }
122
+ function conditionMatches(schema, fragment, type) {
123
+ if (!fragment.typeCondition) return true;
124
+ const condition = typeFromAST(schema, fragment.typeCondition);
125
+ if (condition === type) return true;
126
+ return !!condition && isAbstractType(condition) && schema.isSubType(condition, type);
127
+ }
128
+ function collect(plan, type, selectionSet, fields, visited) {
129
+ for (const selection of selectionSet.selections) switch (selection.kind) {
130
+ case Kind.FIELD: {
131
+ if (!shouldInclude(selection)) continue;
132
+ const key = selection.alias ? selection.alias.value : selection.name.value;
133
+ const list = fields.get(key);
134
+ if (list) list.push(selection);
135
+ else fields.set(key, [selection]);
136
+ break;
137
+ }
138
+ case Kind.INLINE_FRAGMENT:
139
+ if (!shouldInclude(selection) || !conditionMatches(plan.schema, selection, type)) continue;
140
+ collect(plan, type, selection.selectionSet, fields, visited);
141
+ break;
142
+ case Kind.FRAGMENT_SPREAD: {
143
+ const name = selection.name.value;
144
+ if (visited.has(name) || !shouldInclude(selection)) continue;
145
+ visited.add(name);
146
+ const fragment = plan.fragments[name];
147
+ if (!fragment || !conditionMatches(plan.schema, fragment, type)) continue;
148
+ collect(plan, type, fragment.selectionSet, fields, visited);
149
+ break;
150
+ }
151
+ }
152
+ }
153
+ /**
154
+ * The fields of `selectionSets` on `type`, by response key, collected as the executor collects
155
+ * them (literal `@skip`/`@include`, fragments on the type): for the WASM data side's lowering
156
+ * (`wasm-data/lower.ts`), whose key order and fragment merging must be the executor's.
157
+ */
158
+ function collectFields(schema, fragments, type, selectionSets) {
159
+ const plan = {
160
+ schema,
161
+ fragments
162
+ };
163
+ const fields = /* @__PURE__ */ new Map();
164
+ const visited = /* @__PURE__ */ new Set();
165
+ for (const set of selectionSets) collect(plan, type, set, fields, visited);
166
+ return fields;
167
+ }
168
+ /** whether the executor plans the document itself (else graphql-js executes it) */
169
+ function documentSupported(document) {
170
+ return supported(document);
171
+ }
172
+ function fieldPlans(plan, type, selectionSets) {
173
+ const fields = /* @__PURE__ */ new Map();
174
+ const visited = /* @__PURE__ */ new Set();
175
+ for (const set of selectionSets) collect(plan, type, set, fields, visited);
176
+ const out = [];
177
+ for (const [key, nodes] of fields) {
178
+ const name = nodes[0].name.value;
179
+ let def;
180
+ if (name === SchemaMetaFieldDef.name && plan.schema.getQueryType() === type) def = SchemaMetaFieldDef;
181
+ else if (name === TypeMetaFieldDef.name && plan.schema.getQueryType() === type) def = TypeMetaFieldDef;
182
+ else if (name === TypeNameMetaFieldDef.name) def = TypeNameMetaFieldDef;
183
+ else def = type.getFields()[name];
184
+ if (!def) continue;
185
+ const named = getNamedType(def.type);
186
+ const composite = !isLeafType(named);
187
+ const typeInfo = isAbstractType(named) || isObjectType(named) && !!named.isTypeOf;
188
+ const field = {
189
+ key,
190
+ name,
191
+ nodes,
192
+ def,
193
+ parentType: type,
194
+ resolve: syncVariantOf(def.resolve),
195
+ typename: def === TypeNameMetaFieldDef,
196
+ hasArgs: def.args.length > 0,
197
+ shareArgs: def.resolve !== void 0 && sharesArgs(def.resolve),
198
+ needsPath: def.resolve !== void 0 || composite,
199
+ needsInfo: def.resolve !== void 0 || typeInfo,
200
+ complete: void 0
201
+ };
202
+ field.complete = completer(plan, field, def.type);
203
+ out.push(field);
204
+ }
205
+ return out;
206
+ }
207
+ /** handles the rejections of promises a failed execution leaves behind */
208
+ function settle(values) {
209
+ for (const value of values) if (isPromise(value)) value.then(void 0, () => {});
210
+ }
211
+ function executeFields(run, plan, source, path) {
212
+ const fields = plan.fields ??= fieldPlans(plan.plan, plan.type, plan.nodes.flatMap((node) => node.selectionSet ? [node.selectionSet] : []));
213
+ const out = run.plainObjects ? {} : Object.create(null);
214
+ let pending;
215
+ for (let i = 0; i < fields.length; i++) {
216
+ const field = fields[i];
217
+ let value;
218
+ try {
219
+ value = executeField(run, field, source, path);
220
+ } catch (e) {
221
+ if (pending) settle(pending);
222
+ throw e;
223
+ }
224
+ if (isPromise(value)) {
225
+ out[field.key] = null;
226
+ (pending ??= []).push(field.key, value);
227
+ } else out[field.key] = value;
228
+ }
229
+ if (!pending) return out;
230
+ const keys = pending;
231
+ const promises = [];
232
+ for (let i = 1; i < keys.length; i += 2) promises.push(keys[i]);
233
+ return Promise.all(promises).then((values) => {
234
+ for (let i = 0; i < values.length; i++) out[keys[2 * i]] = values[i];
235
+ return out;
236
+ });
237
+ }
238
+ function executeField(run, field, source, parent) {
239
+ if (field.typename) return field.parentType.name;
240
+ const path = field.needsPath ? {
241
+ prev: parent,
242
+ key: field.key,
243
+ typename: field.parentType.name
244
+ } : void 0;
245
+ let info;
246
+ let value;
247
+ const resolve = field.resolve;
248
+ if (resolve === void 0) {
249
+ if (field.hasArgs) argsOf(run, field, true);
250
+ if (source !== null && (typeof source === "object" || typeof source === "function")) {
251
+ value = source[field.name];
252
+ if (typeof value === "function") {
253
+ info = resolveInfo(run, field, path ?? fieldPath(field, parent));
254
+ value = source[field.name](argsOf(run, field), run.context, info);
255
+ }
256
+ }
257
+ } else {
258
+ info = resolveInfo(run, field, path);
259
+ value = resolve(source, argsOf(run, field), run.context, info);
260
+ }
261
+ if (field.needsInfo) info ??= resolveInfo(run, field, path);
262
+ if (isPromise(value)) return value.then((resolved) => field.complete(run, resolved, path, info));
263
+ return field.complete(run, value, path, info);
264
+ }
265
+ const fieldPath = (field, parent) => ({
266
+ prev: parent,
267
+ key: field.key,
268
+ typename: field.parentType.name
269
+ });
270
+ function resolveInfo(run, field, path) {
271
+ return {
272
+ fieldName: field.def.name,
273
+ fieldNodes: field.nodes,
274
+ returnType: field.def.type,
275
+ parentType: field.parentType,
276
+ path,
277
+ schema: run.schema,
278
+ fragments: run.fragments,
279
+ rootValue: void 0,
280
+ operation: run.operation,
281
+ variableValues: run.variableValues
282
+ };
283
+ }
284
+ const EMPTY_ARGS = Object.freeze({});
285
+ /** a field's arguments (`check`: only coerced, once per query, for a field without resolver) */
286
+ function argsOf(run, field, check = false) {
287
+ if (!field.hasArgs) return field.shareArgs ? EMPTY_ARGS : {};
288
+ if (!field.shareArgs && !check) return getArgumentValues(field.def, field.nodes[0], run.variableValues);
289
+ const cache = run.args ??= /* @__PURE__ */ new Map();
290
+ let args = cache.get(field);
291
+ if (!args) {
292
+ args = getArgumentValues(field.def, field.nodes[0], run.variableValues);
293
+ cache.set(field, args);
294
+ }
295
+ return args;
296
+ }
297
+ function nullError(field) {
298
+ return /* @__PURE__ */ new Error(`Cannot return null for non-nullable field ${field.parentType.name}.${field.def.name}.`);
299
+ }
300
+ function completer(plan, field, type) {
301
+ if (isNonNullType(type)) {
302
+ const inner = completer(plan, field, type.ofType);
303
+ return (run, value, path, info) => {
304
+ if (value instanceof Error) throw value;
305
+ const completed = inner(run, value, path, info);
306
+ if (completed === null) throw nullError(field);
307
+ return completed;
308
+ };
309
+ }
310
+ if (isListType(type)) {
311
+ const item = completer(plan, field, type.ofType);
312
+ return (run, value, path, info) => {
313
+ if (value instanceof Error) throw value;
314
+ if (value == null) return null;
315
+ if (typeof value !== "object" || typeof value[Symbol.iterator] !== "function") throw new Error(`Expected Iterable for field "${field.parentType.name}.${field.name}".`);
316
+ const out = [];
317
+ let promises = false;
318
+ try {
319
+ if (Array.isArray(value)) for (let i = 0; i < value.length; i++) {
320
+ const completed = completeItem(run, field, item, value[i], i, path, info);
321
+ if (isPromise(completed)) promises = true;
322
+ out.push(completed);
323
+ }
324
+ else for (const raw of value) {
325
+ const completed = completeItem(run, field, item, raw, out.length, path, info);
326
+ if (isPromise(completed)) promises = true;
327
+ out.push(completed);
328
+ }
329
+ } catch (e) {
330
+ if (promises) settle(out);
331
+ if (Array.isArray(value)) settle(value);
332
+ throw e;
333
+ }
334
+ return promises ? Promise.all(out) : out;
335
+ };
336
+ }
337
+ if (isLeafType(type)) {
338
+ const serialize = serializer(type);
339
+ return (_run, value) => {
340
+ if (value instanceof Error) throw value;
341
+ if (value == null) return null;
342
+ return serialize(value);
343
+ };
344
+ }
345
+ if (isAbstractType(type)) return abstractCompleter(plan, field, type);
346
+ const object = {
347
+ plan,
348
+ type,
349
+ nodes: field.nodes
350
+ };
351
+ return (run, value, path, info) => {
352
+ if (value instanceof Error) throw value;
353
+ if (value == null) return null;
354
+ return completeObject(run, object, value, path, info);
355
+ };
356
+ }
357
+ function completeItem(run, field, item, raw, index, path, info) {
358
+ const itemPath = field.needsPath ? {
359
+ prev: path,
360
+ key: index,
361
+ typename: void 0
362
+ } : void 0;
363
+ return isPromise(raw) ? raw.then((resolved) => item(run, resolved, itemPath, info)) : item(run, raw, itemPath, info);
364
+ }
365
+ function completeObject(run, object, value, path, info) {
366
+ if (object.type.isTypeOf) {
367
+ const matches = object.type.isTypeOf(value, run.context, info);
368
+ if (isPromise(matches)) return matches.then((ok) => {
369
+ if (!ok) throw new Error(`Expected value of type "${object.type.name}".`);
370
+ return executeFields(run, object, value, path);
371
+ });
372
+ if (!matches) throw new Error(`Expected value of type "${object.type.name}".`);
373
+ }
374
+ return executeFields(run, object, value, path);
375
+ }
376
+ function abstractCompleter(plan, field, type) {
377
+ /** a plan per runtime type (validated once) */
378
+ const objects = /* @__PURE__ */ new Map();
379
+ const objectFor = (run, name) => {
380
+ if (typeof name !== "string") throw new Error(`Abstract type "${type.name}": no type.`);
381
+ let object = objects.get(name);
382
+ if (!object) {
383
+ const runtime = run.schema.getType(name);
384
+ if (!runtime || !isObjectType(runtime) || !run.schema.isSubType(type, runtime)) throw new Error(`Abstract type "${type.name}": invalid runtime type "${name}".`);
385
+ object = {
386
+ plan,
387
+ type: runtime,
388
+ nodes: field.nodes
389
+ };
390
+ objects.set(name, object);
391
+ }
392
+ return object;
393
+ };
394
+ return (run, value, path, info) => {
395
+ if (value instanceof Error) throw value;
396
+ if (value == null) return null;
397
+ const name = (type.resolveType ?? defaultTypeResolver)(value, run.context, info, type);
398
+ if (isPromise(name)) return name.then((resolved) => completeObject(run, objectFor(run, resolved), value, path, info));
399
+ return completeObject(run, objectFor(run, name), value, path, info);
400
+ };
401
+ }
402
+ const MAX_INT = 2147483647;
403
+ const MIN_INT = -2147483648;
404
+ /** a leaf type's serialize, with the identity cases of the built-in scalars inlined */
405
+ function serializer(type) {
406
+ const checked = (value) => {
407
+ const serialized = type.serialize(value);
408
+ if (serialized == null) throw new Error(`Expected ${type.name}.serialize to return a value.`);
409
+ return serialized;
410
+ };
411
+ if (type === GraphQLString || type === GraphQLID) return (value) => typeof value === "string" ? value : checked(value);
412
+ if (type === GraphQLBoolean) return (value) => typeof value === "boolean" ? value : checked(value);
413
+ if (type === GraphQLInt) return (value) => typeof value === "number" && Number.isInteger(value) && value <= MAX_INT && value >= MIN_INT ? value : checked(value);
414
+ if (type === GraphQLFloat) return (value) => typeof value === "number" && Number.isFinite(value) ? value : checked(value);
415
+ return checked;
416
+ }
417
+ export { collectFields, documentSupported, executeQuery, executeStats };