@nxgt/graphql-codegen-msw 0.0.0-stage → 0.1.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 (50) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +293 -2
  3. package/THIRD-PARTY-NOTICES.md +71 -0
  4. package/dist/config.d.ts +66 -0
  5. package/dist/config.d.ts.map +1 -0
  6. package/dist/imports.d.ts +17 -0
  7. package/dist/imports.d.ts.map +1 -0
  8. package/dist/index.d.ts +13 -0
  9. package/dist/index.d.ts.map +1 -0
  10. package/dist/index.js +365 -0
  11. package/dist/index.js.map +15 -0
  12. package/dist/input-types.d.ts +11 -0
  13. package/dist/input-types.d.ts.map +1 -0
  14. package/dist/naming.d.ts +23 -0
  15. package/dist/naming.d.ts.map +1 -0
  16. package/dist/operations.d.ts +17 -0
  17. package/dist/operations.d.ts.map +1 -0
  18. package/dist/options.d.ts +31 -0
  19. package/dist/options.d.ts.map +1 -0
  20. package/dist/runtime/coercion/cross-inspect.d.ts +19 -0
  21. package/dist/runtime/coercion/cross-inspect.d.ts.map +1 -0
  22. package/dist/runtime/coercion/document.d.ts +51 -0
  23. package/dist/runtime/coercion/document.d.ts.map +1 -0
  24. package/dist/runtime/coercion/index.d.ts +30 -0
  25. package/dist/runtime/coercion/index.d.ts.map +1 -0
  26. package/dist/runtime/coercion/inspect.d.ts +9 -0
  27. package/dist/runtime/coercion/inspect.d.ts.map +1 -0
  28. package/dist/runtime/coercion/issues.d.ts +19 -0
  29. package/dist/runtime/coercion/issues.d.ts.map +1 -0
  30. package/dist/runtime/coercion/messages.d.ts +37 -0
  31. package/dist/runtime/coercion/messages.d.ts.map +1 -0
  32. package/dist/runtime/coercion/suggestions.d.ts +15 -0
  33. package/dist/runtime/coercion/suggestions.d.ts.map +1 -0
  34. package/dist/runtime/coercion/validate.d.ts +39 -0
  35. package/dist/runtime/coercion/validate.d.ts.map +1 -0
  36. package/dist/runtime/index.d.ts +6 -0
  37. package/dist/runtime/index.d.ts.map +1 -0
  38. package/dist/runtime/index.js +595 -0
  39. package/dist/runtime/index.js.map +21 -0
  40. package/dist/runtime/mock-operation.d.ts +65 -0
  41. package/dist/runtime/mock-operation.d.ts.map +1 -0
  42. package/dist/runtime/refusals.d.ts +36 -0
  43. package/dist/runtime/refusals.d.ts.map +1 -0
  44. package/dist/runtime/response.d.ts +34 -0
  45. package/dist/runtime/response.d.ts.map +1 -0
  46. package/docs/README.md +14 -0
  47. package/docs/guide/handlers.md +654 -0
  48. package/docs/roadmap.md +40 -0
  49. package/docs/troubleshooting.md +304 -0
  50. package/package.json +80 -5
@@ -0,0 +1,595 @@
1
+ // src/runtime/index.ts
2
+ import {
3
+ fieldIssueOf as fieldIssueOf2,
4
+ fieldsError,
5
+ inputPath,
6
+ invalidInputError as invalidInputError2
7
+ } from "@nxgt/graphql-validation";
8
+
9
+ // src/runtime/mock-operation.ts
10
+ import {
11
+ graphql
12
+ } from "msw/graphql";
13
+
14
+ // src/runtime/coercion/index.ts
15
+ import { GraphQLError } from "graphql";
16
+
17
+ // src/runtime/coercion/document.ts
18
+ import {
19
+ Kind,
20
+ parse
21
+ } from "graphql";
22
+ function declared(source) {
23
+ const at = source.indexOf(" = ");
24
+ const printed = at === -1 ? source : source.slice(0, at);
25
+ return { type: typeRef(printed.trim()), hasDefault: at !== -1 };
26
+ }
27
+ function typeRef(printed) {
28
+ if (printed.endsWith("!"))
29
+ return { kind: "non-null", of: typeRef(printed.slice(0, -1)) };
30
+ if (printed.startsWith("[") && printed.endsWith("]"))
31
+ return { kind: "list", of: typeRef(printed.slice(1, -1)) };
32
+ return { kind: "named", name: printed };
33
+ }
34
+ function printType(type) {
35
+ switch (type.kind) {
36
+ case "non-null":
37
+ return `${printType(type.of)}!`;
38
+ case "list":
39
+ return `[${printType(type.of)}]`;
40
+ default:
41
+ return type.name;
42
+ }
43
+ }
44
+ function definitionsOf(query, name) {
45
+ let operations;
46
+ try {
47
+ operations = parse(query).definitions.filter((node) => node.kind === Kind.OPERATION_DEFINITION);
48
+ } catch {
49
+ return new Map;
50
+ }
51
+ const operation = operations.find((node) => node.name?.value === name) ?? operations[0];
52
+ return new Map((operation?.variableDefinitions ?? []).map((node) => [
53
+ node.variable.name.value,
54
+ node
55
+ ]));
56
+ }
57
+
58
+ // src/runtime/coercion/issues.ts
59
+ function placed(issues, prefix = []) {
60
+ return issues.flatMap((issue) => {
61
+ const path = [...prefix, ...issue.path.map(segment)];
62
+ const own = { path, issue };
63
+ if (issue.code !== "invalid_union")
64
+ return [own];
65
+ return [own, ...issue.errors.flatMap((branch) => placed(branch, path))];
66
+ });
67
+ }
68
+ function segment(key) {
69
+ return typeof key === "number" ? key : String(key);
70
+ }
71
+ function issueAt(issues, path) {
72
+ return issues.find((placedIssue) => placedIssue.path.length >= path.length && path.every((key, index) => placedIssue.path[index] === key))?.issue;
73
+ }
74
+
75
+ // src/runtime/coercion/cross-inspect.ts
76
+ /*!
77
+ * Ported from cross-inspect 1.0.1 (packages/inspect),
78
+ * https://github.com/ardatan/graphql-tools
79
+ * Copyright (c) 2020 The Guild, Inc.
80
+ * itself taken from graphql-js, https://github.com/graphql/graphql-js
81
+ * Copyright (c) GraphQL Contributors
82
+ * @license MIT
83
+ */
84
+ function crossInspect(value) {
85
+ return formatValue(value, 0);
86
+ }
87
+ var MAX_DEPTH = 3;
88
+ function formatValue(value, depth) {
89
+ if (typeof value === "string")
90
+ return JSON.stringify(value);
91
+ if (typeof value !== "object" || value === null)
92
+ return String(value);
93
+ if (Array.isArray(value)) {
94
+ if (value.length === 0)
95
+ return "[]";
96
+ if (depth >= MAX_DEPTH)
97
+ return "[Array]";
98
+ return `[${value.map((item) => formatValue(item, depth + 1)).join(", ")}]`;
99
+ }
100
+ const entries = Object.entries(value);
101
+ if (entries.length === 0)
102
+ return "{}";
103
+ if (depth >= MAX_DEPTH)
104
+ return "[Object]";
105
+ return `{ ${entries.map(([key, item]) => `${key}: ${formatValue(item, depth + 1)}`).join(", ")} }`;
106
+ }
107
+
108
+ // src/runtime/coercion/inspect.ts
109
+ /*!
110
+ * Ported from graphql-js 17.0.2 (src/jsutils/inspect.ts),
111
+ * https://github.com/graphql/graphql-js
112
+ * Copyright (c) GraphQL Contributors
113
+ * @license MIT
114
+ */
115
+ var MAX_ARRAY_LENGTH = 10;
116
+ var MAX_RECURSIVE_DEPTH = 2;
117
+ function inspect(value) {
118
+ return formatValue2(value, []);
119
+ }
120
+ function formatValue2(value, seenValues) {
121
+ switch (typeof value) {
122
+ case "string":
123
+ return JSON.stringify(value);
124
+ case "function":
125
+ return value.name ? `[function ${value.name}]` : "[function]";
126
+ case "object":
127
+ return formatObjectValue(value, seenValues);
128
+ default:
129
+ return String(value);
130
+ }
131
+ }
132
+ function formatObjectValue(value, previouslySeenValues) {
133
+ if (value === null) {
134
+ return "null";
135
+ }
136
+ if (previouslySeenValues.includes(value)) {
137
+ return "[Circular]";
138
+ }
139
+ const seenValues = [...previouslySeenValues, value];
140
+ if (isJSONable(value)) {
141
+ const jsonValue = value.toJSON();
142
+ if (jsonValue !== value) {
143
+ return typeof jsonValue === "string" ? jsonValue : formatValue2(jsonValue, seenValues);
144
+ }
145
+ } else if (Array.isArray(value)) {
146
+ return formatArray(value, seenValues);
147
+ }
148
+ return formatObject(value, seenValues);
149
+ }
150
+ function isJSONable(value) {
151
+ return typeof value.toJSON === "function";
152
+ }
153
+ function formatObject(object, seenValues) {
154
+ const entries = Object.entries(object);
155
+ if (entries.length === 0) {
156
+ return "{}";
157
+ }
158
+ if (seenValues.length > MAX_RECURSIVE_DEPTH) {
159
+ return `[${getObjectTag(object)}]`;
160
+ }
161
+ const properties = entries.map(([key, value]) => `${key}: ${formatValue2(value, seenValues)}`);
162
+ return `{ ${properties.join(", ")} }`;
163
+ }
164
+ function formatArray(array, seenValues) {
165
+ if (array.length === 0) {
166
+ return "[]";
167
+ }
168
+ if (seenValues.length > MAX_RECURSIVE_DEPTH) {
169
+ return "[Array]";
170
+ }
171
+ const len = Math.min(MAX_ARRAY_LENGTH, array.length);
172
+ const remaining = array.length - len;
173
+ const items = [];
174
+ for (let i = 0;i < len; ++i) {
175
+ items.push(formatValue2(array[i], seenValues));
176
+ }
177
+ if (remaining === 1) {
178
+ items.push("... 1 more item");
179
+ } else if (remaining > 1) {
180
+ items.push(`... ${remaining} more items`);
181
+ }
182
+ return `[${items.join(", ")}]`;
183
+ }
184
+ function getObjectTag(object) {
185
+ const tag = Object.prototype.toString.call(object).replace(/^\[object /, "").replace(/]$/, "");
186
+ if (tag === "Object" && typeof object.constructor === "function") {
187
+ const name = object.constructor.name;
188
+ if (typeof name === "string" && name !== "") {
189
+ return name;
190
+ }
191
+ }
192
+ return tag;
193
+ }
194
+
195
+ // src/runtime/coercion/suggestions.ts
196
+ /*!
197
+ * Ported from graphql-js 17.0.2 (src/jsutils/suggestionList.ts,
198
+ * didYouMean.ts, formatList.ts, naturalCompare.ts),
199
+ * https://github.com/graphql/graphql-js
200
+ * Copyright (c) GraphQL Contributors
201
+ * @license MIT
202
+ */
203
+ var MAX_SUGGESTIONS = 5;
204
+ function didYouMean(suggestions, subject) {
205
+ if (suggestions.length === 0)
206
+ return "";
207
+ const listed = orList(suggestions.slice(0, MAX_SUGGESTIONS).map((item) => `"${item}"`));
208
+ return ` Did you mean ${subject ? `${subject} ` : ""}${listed}?`;
209
+ }
210
+ function orList(items) {
211
+ if (items.length === 1)
212
+ return items[0] ?? "";
213
+ if (items.length === 2)
214
+ return `${items[0]} or ${items[1]}`;
215
+ return `${items.slice(0, -1).join(", ")}, or ${items.at(-1)}`;
216
+ }
217
+ function suggestionList(input, options) {
218
+ const distances = new Map;
219
+ const threshold = Math.floor(input.length * 0.4) + 1;
220
+ for (const option of options) {
221
+ const distance = lexicalDistance(input, option, threshold);
222
+ if (distance !== undefined)
223
+ distances.set(option, distance);
224
+ }
225
+ return [...distances.keys()].sort((a, b) => (distances.get(a) ?? 0) - (distances.get(b) ?? 0) || naturalCompare(a, b));
226
+ }
227
+ function lexicalDistance(input, option, threshold) {
228
+ if (input === option)
229
+ return 0;
230
+ const lowerInput = input.toLowerCase();
231
+ const lowerOption = option.toLowerCase();
232
+ if (lowerInput === lowerOption)
233
+ return 1;
234
+ let a = codes(lowerOption);
235
+ let b = codes(lowerInput);
236
+ if (a.length < b.length)
237
+ [a, b] = [b, a];
238
+ if (a.length - b.length > threshold)
239
+ return;
240
+ const rows = [0, 1, 2].map(() => new Array(b.length + 1).fill(0));
241
+ const row = (i) => rows[i % 3];
242
+ for (let j = 0;j <= b.length; j++)
243
+ row(0)[j] = j;
244
+ for (let i = 1;i <= a.length; i++) {
245
+ const up = row(i - 1);
246
+ const current = row(i);
247
+ current[0] = i;
248
+ let smallest = i;
249
+ for (let j = 1;j <= b.length; j++) {
250
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
251
+ let cell = Math.min((up[j] ?? 0) + 1, (current[j - 1] ?? 0) + 1, (up[j - 1] ?? 0) + cost);
252
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1])
253
+ cell = Math.min(cell, (row(i - 2)[j - 2] ?? 0) + 1);
254
+ if (cell < smallest)
255
+ smallest = cell;
256
+ current[j] = cell;
257
+ }
258
+ if (smallest > threshold)
259
+ return;
260
+ }
261
+ const distance = row(a.length)[b.length] ?? 0;
262
+ return distance <= threshold ? distance : undefined;
263
+ }
264
+ function codes(text) {
265
+ return Array.from({ length: text.length }, (_, i) => text.charCodeAt(i));
266
+ }
267
+ var DIGIT_0 = 48;
268
+ var DIGIT_9 = 57;
269
+ var isDigit = (code) => !Number.isNaN(code) && DIGIT_0 <= code && code <= DIGIT_9;
270
+ function naturalCompare(aText, bText) {
271
+ let a = 0;
272
+ let b = 0;
273
+ while (a < aText.length && b < bText.length) {
274
+ let aChar = aText.charCodeAt(a);
275
+ let bChar = bText.charCodeAt(b);
276
+ if (isDigit(aChar) && isDigit(bChar)) {
277
+ let aNumber = 0;
278
+ do {
279
+ ++a;
280
+ aNumber = aNumber * 10 + aChar - DIGIT_0;
281
+ aChar = aText.charCodeAt(a);
282
+ } while (isDigit(aChar) && aNumber > 0);
283
+ let bNumber = 0;
284
+ do {
285
+ ++b;
286
+ bNumber = bNumber * 10 + bChar - DIGIT_0;
287
+ bChar = bText.charCodeAt(b);
288
+ } while (isDigit(bChar) && bNumber > 0);
289
+ if (aNumber !== bNumber)
290
+ return aNumber < bNumber ? -1 : 1;
291
+ } else {
292
+ if (aChar !== bChar)
293
+ return aChar < bChar ? -1 : 1;
294
+ ++a;
295
+ ++b;
296
+ }
297
+ }
298
+ return aText.length - bText.length;
299
+ }
300
+
301
+ // src/runtime/coercion/messages.ts
302
+ /*!
303
+ * Messages ported from graphql-js 17.0.2 (src/type/scalars.ts,
304
+ * src/type/definition.ts), https://github.com/graphql/graphql-js
305
+ * Copyright (c) GraphQL Contributors
306
+ * and from @graphql-tools/executor 2.0.3 (execution/validateInputValue,
307
+ * execution/values), https://github.com/ardatan/graphql-tools
308
+ * Copyright (c) 2020 The Guild, Inc.
309
+ * @license MIT
310
+ */
311
+ var INT_MAX = 2147483647;
312
+ var INT_MIN = -2147483648;
313
+ function builtinRefusal(name, value) {
314
+ const found = inspect(value);
315
+ switch (name) {
316
+ case "String":
317
+ return typeof value === "string" ? undefined : `String cannot represent a non string value: ${found}`;
318
+ case "Boolean":
319
+ return typeof value === "boolean" ? undefined : `Boolean cannot represent a non boolean value: ${found}`;
320
+ case "Float":
321
+ return typeof value === "number" && Number.isFinite(value) ? undefined : `Float cannot represent non numeric value: ${found}`;
322
+ case "ID":
323
+ return typeof value === "string" || Number.isInteger(value) ? undefined : `ID cannot represent value: ${found}`;
324
+ case "Int":
325
+ return intRefusal(value);
326
+ default:
327
+ return;
328
+ }
329
+ }
330
+ function intRefusal(value) {
331
+ if (typeof value !== "number" || !Number.isInteger(value))
332
+ return `Int cannot represent non-integer value: ${inspect(value)}`;
333
+ if (value > INT_MAX || value < INT_MIN)
334
+ return `Int cannot represent non 32-bit signed integer value: ${inspect(value)}`;
335
+ return;
336
+ }
337
+ function enumRefusal(name, values, value) {
338
+ if (typeof value !== "string") {
339
+ const found = inspect(value);
340
+ return `Enum "${name}" cannot represent non-string value: ${found}.${suggest(found, values)}`;
341
+ }
342
+ if (values.includes(value))
343
+ return;
344
+ return `Value "${value}" does not exist in "${name}" enum.${suggest(value, values)}`;
345
+ }
346
+ function suggest(value, values) {
347
+ return didYouMean(suggestionList(value, values), "the enum value");
348
+ }
349
+ function scalarRefusal(name, issue) {
350
+ return issue && `${name} cannot represent this input: ${issue.message}`;
351
+ }
352
+ var objectRefusal = {
353
+ notAnObject: (type, value) => `Expected value of type "${type}" to be an object, found: ${crossInspect(value)}.`,
354
+ missingField: (type, field, object) => `Expected value of type "${type}" to include required field "${field}", found: ${crossInspect(object)}.`,
355
+ unknownField: (type, field, known, object) => {
356
+ const suggestion = didYouMean(suggestionList(field, known));
357
+ return `Expected value of type "${type}" not to include unknown field "${field}"${suggestion ? `.${suggestion} Found` : ", found"}: ${crossInspect(object)}.`;
358
+ },
359
+ oneOf: (type) => `Within OneOf Input Object type "${type}", exactly one field must be specified, and the value for that field must be non-null.`,
360
+ notProvided: (type) => `Expected a value of non-null type "${type}" to be provided.`,
361
+ isNull: (type) => `Expected value of non-null type "${type}" not to be null.`
362
+ };
363
+ var variableRefusal = {
364
+ notProvided: (name, type) => `Variable "$${name}" of required type "${type}" was not provided.`,
365
+ isNull: (name, type) => `Variable "$${name}" of non-null type "${type}" must not be null.`,
366
+ invalidValue: (name, value, path, reason) => {
367
+ const at = path.length > 0 ? ` at "${name}${path.map((key) => typeof key === "number" ? `[${key}]` : `.${key}`).join("")}"` : "";
368
+ return `Variable "$${name}" got invalid value ${crossInspect(value)}${at}; ${reason}`;
369
+ }
370
+ };
371
+
372
+ // src/runtime/coercion/validate.ts
373
+ /*!
374
+ * Ported from @graphql-tools/executor 2.0.3 (execution/validateInputValue),
375
+ * https://github.com/ardatan/graphql-tools
376
+ * Copyright (c) 2020 The Guild, Inc.
377
+ * itself a copy of graphql-js 17's validateInputValue,
378
+ * https://github.com/graphql/graphql-js
379
+ * Copyright (c) GraphQL Contributors
380
+ * @license MIT
381
+ */
382
+ function validateValue(walk, value, type, at) {
383
+ if (type.kind === "non-null") {
384
+ if (value === undefined)
385
+ report(walk, at, value, objectRefusal.notProvided(printType(type)));
386
+ else if (value === null)
387
+ report(walk, at, value, objectRefusal.isNull(printType(type)));
388
+ else
389
+ validateValue(walk, value, type.of, at);
390
+ } else if (value === null || value === undefined) {} else if (type.kind === "list") {
391
+ validateList(walk, value, type.of, at);
392
+ } else {
393
+ validateNamed(walk, value, type.name, at);
394
+ }
395
+ }
396
+ function validateList(walk, value, item, at) {
397
+ if (!Array.isArray(value)) {
398
+ validateValue(walk, value, item, { ...at, zodPath: [...at.zodPath, 0] });
399
+ return;
400
+ }
401
+ value.forEach((element, index) => {
402
+ validateValue(walk, element, item, {
403
+ path: [...at.path, index],
404
+ zodPath: [...at.zodPath, index]
405
+ });
406
+ });
407
+ }
408
+ var BUILT_INS = new Set([
409
+ "String",
410
+ "Int",
411
+ "Float",
412
+ "Boolean",
413
+ "ID"
414
+ ]);
415
+ function validateNamed(walk, value, name, at) {
416
+ const named = walk.types[name] ?? (BUILT_INS.has(name) ? { kind: "builtin" } : { kind: "scalar" });
417
+ if (named.kind === "input") {
418
+ validateObject(walk, value, name, named, at);
419
+ return;
420
+ }
421
+ const reason = leafRefusal(walk, value, name, named, at);
422
+ if (reason !== undefined)
423
+ report(walk, at, value, reason);
424
+ }
425
+ function leafRefusal(walk, value, name, named, at) {
426
+ switch (named.kind) {
427
+ case "builtin":
428
+ return builtinRefusal(name, value);
429
+ case "enum":
430
+ return enumRefusal(name, named.values, value);
431
+ case "scalar":
432
+ return scalarRefusal(name, issueAt(walk.issues, at.zodPath));
433
+ default:
434
+ throw new Error(STALE_FILE);
435
+ }
436
+ }
437
+ var STALE_FILE = "@nxgt/graphql-codegen-msw: the generated file was written by another version than the runtime; regenerate it";
438
+ function validateObject(walk, value, name, type, at) {
439
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
440
+ report(walk, at, value, objectRefusal.notAnObject(name, value));
441
+ return;
442
+ }
443
+ const object = value;
444
+ for (const [field, source] of Object.entries(type.fields)) {
445
+ const { type: fieldType, hasDefault } = declared(source);
446
+ const fieldValue = Object.hasOwn(object, field) ? object[field] : undefined;
447
+ if (fieldValue !== undefined)
448
+ validateValue(walk, fieldValue, fieldType, {
449
+ path: [...at.path, field],
450
+ zodPath: [...at.zodPath, field]
451
+ });
452
+ else if (fieldType.kind === "non-null" && !hasDefault)
453
+ report(walk, at, object, objectRefusal.missingField(name, field, object));
454
+ }
455
+ const names = Object.keys(type.fields);
456
+ const known = [];
457
+ for (const key of Object.keys(object)) {
458
+ if (object[key] === undefined)
459
+ continue;
460
+ if (Object.hasOwn(type.fields, key))
461
+ known.push(key);
462
+ else
463
+ report(walk, at, object, objectRefusal.unknownField(name, key, names, object));
464
+ }
465
+ if (type.oneOf)
466
+ validateOneOf(walk, object, name, known, at);
467
+ }
468
+ function validateOneOf(walk, object, name, known, at) {
469
+ const [only] = known;
470
+ if (known.length !== 1 || only === undefined)
471
+ report(walk, at, object, objectRefusal.oneOf(name));
472
+ else if (object[only] === null)
473
+ report(walk, { ...at, path: [...at.path, only] }, object, objectRefusal.oneOf(name));
474
+ }
475
+ function report(walk, at, value, reason) {
476
+ walk.problems.push({ path: at.path, value, reason });
477
+ }
478
+
479
+ // src/runtime/coercion/index.ts
480
+ var COERCION_STATUS = 400;
481
+ function coercionErrors(issues, request, operation) {
482
+ const definitions = definitionsOf(request.query, request.operationName);
483
+ const sent = typeof request.variables === "object" && request.variables !== null ? request.variables : {};
484
+ const walked = placed(issues);
485
+ const { variableTypes = {}, inputTypes = {} } = operation;
486
+ return Object.entries(variableTypes).flatMap(([name, source]) => variableMessages(name, source, sent, {
487
+ types: inputTypes,
488
+ issues: walked,
489
+ problems: []
490
+ }).map((message) => located(message, definitions.get(name))));
491
+ }
492
+ function variableMessages(name, source, sent, walk) {
493
+ const { type, hasDefault } = declared(source);
494
+ if (!Object.hasOwn(sent, name))
495
+ return type.kind === "non-null" && !hasDefault ? [variableRefusal.notProvided(name, printType(type))] : [];
496
+ const value = sent[name];
497
+ if (value === null && type.kind === "non-null")
498
+ return [variableRefusal.isNull(name, printType(type))];
499
+ validateValue(walk, value, type, { path: [], zodPath: [name] });
500
+ return walk.problems.map(({ path, value: found, reason }) => variableRefusal.invalidValue(name, found, path, reason));
501
+ }
502
+ function located(message, definition) {
503
+ return new GraphQLError(message, definition ? { nodes: definition } : {}).toJSON();
504
+ }
505
+
506
+ // src/runtime/response.ts
507
+ import { HttpResponse } from "msw";
508
+ function isGraphQLError(value) {
509
+ return value instanceof Error && (value.name === "GraphQLError" || Object.prototype.toString.call(value) === "[object GraphQLError]") && typeof value.toJSON === "function";
510
+ }
511
+ function formatted(error) {
512
+ const json = isGraphQLError(error) ? error.toJSON() : error;
513
+ const { extensions, ...rest } = json;
514
+ if (extensions === undefined)
515
+ return json;
516
+ const { http: _http, unexpected: _unexpected, ...kept } = extensions;
517
+ return Object.keys(kept).length > 0 ? { ...rest, extensions: kept } : rest;
518
+ }
519
+ function validStatus(status) {
520
+ return typeof status === "number" && Number.isInteger(status) && status >= 200 && status <= 599 && status !== 204 && status !== 205 && status !== 304 ? status : undefined;
521
+ }
522
+ function statusOf(error) {
523
+ const http = error.extensions?.http;
524
+ return validStatus(http?.status);
525
+ }
526
+ function errorResponse(error, init = {}) {
527
+ const errors = Array.isArray(error) ? error : [error];
528
+ const statuses = errors.map(statusOf).filter((status) => status !== undefined);
529
+ const status = validStatus(init.status) ?? (statuses.length > 0 ? Math.max(...statuses) : 200);
530
+ return HttpResponse.json({ errors: errors.map(formatted) }, { status });
531
+ }
532
+
533
+ // src/runtime/mock-operation.ts
534
+ function mockOperation(operation, resolver, options = {}) {
535
+ const { endpoint = operation.endpoint, ...handlerOptions } = options;
536
+ const link = graphql.link(endpoint);
537
+ const where = { type: operation.type, name: operation.name };
538
+ return link[operation.type](operation.name, async (info) => {
539
+ const checked = await operation.variables.safeParseAsync(info.variables);
540
+ if (!checked.success) {
541
+ const { issues } = checked.error;
542
+ const refused = coercionErrors(issues, info, operation);
543
+ if (refused.length > 0)
544
+ return errorResponse(refused, { status: COERCION_STATUS });
545
+ const { error, status } = await operation.invalidInput(issues, where);
546
+ return errorResponse(error, { status });
547
+ }
548
+ try {
549
+ const answer = await resolver(info);
550
+ assertNotIterator(answer, operation.name);
551
+ return answer;
552
+ } catch (error) {
553
+ if (isGraphQLError(error))
554
+ return errorResponse(error);
555
+ throw error;
556
+ }
557
+ }, handlerOptions);
558
+ }
559
+ function assertNotIterator(answer, name) {
560
+ if (typeof answer === "object" && answer !== null && !(answer instanceof Response) && typeof answer.next === "function") {
561
+ throw new TypeError(`@nxgt/graphql-codegen-msw: the resolver of ${name} returned an iterator (a generator function?). Each request's variables are checked before the resolver runs, which msw cannot do once it keeps an iterator: answer each request from a plain function, or register one handler per answer with { once: true }.`);
562
+ }
563
+ }
564
+ // src/runtime/refusals.ts
565
+ import {
566
+ badUserInput,
567
+ fieldIssueOf,
568
+ invalidInputError
569
+ } from "@nxgt/graphql-validation";
570
+ import { z } from "zod";
571
+ function invalidInputRefusal(options = {}) {
572
+ const { translate } = options;
573
+ return (issues) => ({
574
+ error: invalidInputError(issues.map(fieldIssueOf), translate ? { translate } : {}),
575
+ status: 400
576
+ });
577
+ }
578
+ var badUserInputRefusal = (issues, operation) => ({
579
+ error: badUserInput(operation.name, new z.ZodError([...issues])),
580
+ status: 200
581
+ });
582
+ export {
583
+ badUserInputRefusal,
584
+ errorResponse,
585
+ fieldIssueOf2 as fieldIssueOf,
586
+ fieldsError,
587
+ inputPath,
588
+ invalidInputError2 as invalidInputError,
589
+ invalidInputRefusal,
590
+ isGraphQLError,
591
+ mockOperation
592
+ };
593
+
594
+ //# debugId=A8F75623BD88F85364756E2164756E21
595
+ //# sourceMappingURL=index.js.map