graphql-data-generator 0.3.0-alpha.8 → 0.3.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.
- package/README.md +111 -78
- package/esm/cli.js +0 -1
- package/esm/index.js +1 -2
- package/esm/init.js +33 -10
- package/esm/jest.js +105 -42
- package/esm/plugin.cjs +1 -1
- package/esm/proxy.js +119 -58
- package/package.json +6 -2
- package/script/cli.js +0 -1
- package/script/index.js +3 -2
- package/script/init.js +56 -10
- package/script/jest.js +107 -42
- package/script/plugin.cjs +0 -2
- package/script/proxy.js +119 -58
- package/types/cli.d.ts +1 -1
- package/types/extendedTypes.d.ts +1 -1
- package/types/index.d.ts +2 -3
- package/types/init.d.ts +10 -0
- package/types/jest.d.ts +24 -5
- package/types/plugin.d.cts +1 -1
- package/types/proxy.d.ts +5 -0
- package/types/types.d.ts +1 -1
- package/esm/_dnt.polyfills.js +0 -1
- package/script/_dnt.polyfills.js +0 -2
- package/types/_dnt.polyfills.d.ts +0 -24
package/esm/proxy.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
import { Kind,
|
|
1
|
+
import { Kind, parse } from "graphql";
|
|
2
2
|
import { absurd } from "./util.js";
|
|
3
|
+
/**
|
|
4
|
+
* Updates the value to the base value, bypassing default transformations.
|
|
5
|
+
* Useful to clear optional inputs, resulting in `undefined` rather than `null`.
|
|
6
|
+
*/
|
|
7
|
+
export const clear = Symbol("clear");
|
|
8
|
+
/**
|
|
9
|
+
* Called to insert default patches at the front of nested objects (props &
|
|
10
|
+
* arrays). `build` will insert default patches for top-level objects.
|
|
11
|
+
*/
|
|
3
12
|
let getDefaultPatch = (__typename) => undefined;
|
|
4
13
|
export const withGetDefaultPatch = (newGetDefaultPatch, fn) => {
|
|
5
14
|
const prev = getDefaultPatch;
|
|
@@ -44,7 +53,7 @@ const resolveType = (definitions, path) => {
|
|
|
44
53
|
type = undefined;
|
|
45
54
|
if (kind === "field") {
|
|
46
55
|
const field = "fields" in definition
|
|
47
|
-
?
|
|
56
|
+
? getField(definitions, definition, name)
|
|
48
57
|
: undefined;
|
|
49
58
|
if (!field) {
|
|
50
59
|
throw new Error(`Could not find field ${name} on ${"name" in definition ? definition.name?.value : "<unknown>"}`);
|
|
@@ -91,31 +100,36 @@ const resolveType = (definitions, path) => {
|
|
|
91
100
|
}
|
|
92
101
|
return { parent, definition, type: type };
|
|
93
102
|
};
|
|
94
|
-
const resolveConcreteType = (definitions, definition, patch, prev) => {
|
|
95
|
-
const
|
|
96
|
-
const
|
|
103
|
+
const resolveConcreteType = (definitions, definition, patch, prev, selectionSet) => {
|
|
104
|
+
const objectDefinitions = definitions.filter((d) => d.kind === Kind.OBJECT_TYPE_DEFINITION);
|
|
105
|
+
const interfaceDefinitions = definitions.filter((d) => d.kind === Kind.INTERFACE_TYPE_DEFINITION);
|
|
97
106
|
let options = [];
|
|
98
107
|
const interfaces = [definition];
|
|
99
108
|
while (interfaces.length) {
|
|
100
109
|
const interfaceDefinition = interfaces.pop();
|
|
101
|
-
for (const
|
|
102
|
-
if (
|
|
110
|
+
for (const objectDefinition of objectDefinitions) {
|
|
111
|
+
if (objectDefinition.interfaces?.some((i) => i.name.value === interfaceDefinition.name.value) ||
|
|
103
112
|
("types" in interfaceDefinition &&
|
|
104
|
-
interfaceDefinition.types?.some((t) => t.name.value ===
|
|
105
|
-
options.push(
|
|
113
|
+
interfaceDefinition.types?.some((t) => t.name.value === objectDefinition.name.value)))
|
|
114
|
+
options.push(objectDefinition);
|
|
106
115
|
}
|
|
107
116
|
// TODO: is this a thing...?
|
|
108
|
-
for (const secondOrderInterfaceDefinition of
|
|
117
|
+
for (const secondOrderInterfaceDefinition of interfaceDefinitions) {
|
|
109
118
|
if (secondOrderInterfaceDefinition.interfaces?.some((i) => i.name.value === interfaceDefinition.name.value))
|
|
110
119
|
interfaces.push(secondOrderInterfaceDefinition);
|
|
111
120
|
}
|
|
112
121
|
}
|
|
113
122
|
if (options.length === 1)
|
|
114
123
|
return options[0];
|
|
115
|
-
for (const
|
|
124
|
+
for (const alias in patch) {
|
|
125
|
+
const field = selectionSet
|
|
126
|
+
? getSelectionField(definitions, selectionSet, alias)?.name.value ?? alias
|
|
127
|
+
: alias;
|
|
116
128
|
options = options.filter((o) => field === "__typename"
|
|
117
|
-
? o.name.value === patch[
|
|
118
|
-
: o.fields?.some((f) => f.name.value === field)
|
|
129
|
+
? o.name.value === patch[alias]
|
|
130
|
+
: o.fields?.some((f) => f.name.value === field) ||
|
|
131
|
+
(definition.kind === Kind.INTERFACE_TYPE_DEFINITION &&
|
|
132
|
+
definition.fields?.some((f) => f.name.value === field)));
|
|
119
133
|
if (options.length === 1)
|
|
120
134
|
return options[0];
|
|
121
135
|
}
|
|
@@ -131,7 +145,7 @@ const resolveConcreteType = (definitions, definition, patch, prev) => {
|
|
|
131
145
|
};
|
|
132
146
|
const resolveValue = (definitions, scalars, type, ctx) => {
|
|
133
147
|
if (type.kind !== Kind.NON_NULL_TYPE)
|
|
134
|
-
return null;
|
|
148
|
+
return ctx.input ? undefined : null;
|
|
135
149
|
if (type.type.kind === Kind.LIST_TYPE)
|
|
136
150
|
return [];
|
|
137
151
|
type = type.type;
|
|
@@ -153,36 +167,10 @@ const resolveValue = (definitions, scalars, type, ctx) => {
|
|
|
153
167
|
(fieldTypeDefinitions[0].kind === Kind.OBJECT_TYPE_DEFINITION ||
|
|
154
168
|
fieldTypeDefinitions[0].kind === Kind.INTERFACE_TYPE_DEFINITION ||
|
|
155
169
|
fieldTypeDefinitions[0].kind === Kind.INPUT_OBJECT_TYPE_DEFINITION)) {
|
|
156
|
-
|
|
157
|
-
// .filter((v) => !!v && typeof v === "object") as Patch<T[keyof T]>[];
|
|
158
|
-
const base = _proxy(definitions, scalars, fieldTypeDefinitions[0].name.value, [], { selectionSet: ctx.selectionSet });
|
|
159
|
-
const rawDefaultPatch = getDefaultPatch(type.name.value);
|
|
160
|
-
const defaultPatch = typeof rawDefaultPatch === "function"
|
|
161
|
-
? rawDefaultPatch(base)
|
|
162
|
-
: rawDefaultPatch;
|
|
163
|
-
if (defaultPatch) {
|
|
164
|
-
return _proxy(definitions, scalars, fieldTypeDefinitions[0].name.value, [base, defaultPatch]);
|
|
165
|
-
}
|
|
166
|
-
return base;
|
|
170
|
+
return _proxy(definitions, scalars, ctx.path ? `${ctx.path}.${ctx.prop}` : fieldTypeDefinitions[0].name.value, [], { selectionSet: ctx.selectionSet, nonNull: true });
|
|
167
171
|
}
|
|
168
172
|
throw new Error(`Unhandled default kind ${fieldTypeDefinitions.map((d) => d.kind)}`);
|
|
169
173
|
};
|
|
170
|
-
const gqlprint = (value) => {
|
|
171
|
-
if (typeof value !== "object" || !value)
|
|
172
|
-
return value;
|
|
173
|
-
// deno-lint-ignore no-explicit-any
|
|
174
|
-
if (Array.isArray(value))
|
|
175
|
-
return value.map(gqlprint);
|
|
176
|
-
const obj = {};
|
|
177
|
-
for (const field in value) {
|
|
178
|
-
if (field === "loc" && value[field] instanceof Location) {
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
// deno-lint-ignore no-explicit-any
|
|
182
|
-
obj[field] = gqlprint(value[field]);
|
|
183
|
-
}
|
|
184
|
-
return obj;
|
|
185
|
-
};
|
|
186
174
|
const getSelectionField = (definitions, selectionSet, selection, typename) => {
|
|
187
175
|
for (const s of selectionSet.selections) {
|
|
188
176
|
switch (s.kind) {
|
|
@@ -201,7 +189,9 @@ const getSelectionField = (definitions, selectionSet, selection, typename) => {
|
|
|
201
189
|
case Kind.INLINE_FRAGMENT: {
|
|
202
190
|
if (!s.typeCondition || !typename ||
|
|
203
191
|
s.typeCondition.name.value === typename) {
|
|
204
|
-
|
|
192
|
+
const value = getSelectionField(definitions, s.selectionSet, selection, typename);
|
|
193
|
+
if (value)
|
|
194
|
+
return value;
|
|
205
195
|
}
|
|
206
196
|
}
|
|
207
197
|
}
|
|
@@ -226,7 +216,9 @@ const getSelectionSetSelection = (definitions, selectionSet, selection, typename
|
|
|
226
216
|
case Kind.INLINE_FRAGMENT: {
|
|
227
217
|
if (!s.typeCondition || !typename ||
|
|
228
218
|
s.typeCondition.name.value === typename) {
|
|
229
|
-
|
|
219
|
+
const value = getSelectionSetSelection(definitions, s.selectionSet, selection, typename);
|
|
220
|
+
if (value)
|
|
221
|
+
return value;
|
|
230
222
|
}
|
|
231
223
|
}
|
|
232
224
|
}
|
|
@@ -258,21 +250,61 @@ const selectionSetToKeys = (definitions, selectionSet, typename) => {
|
|
|
258
250
|
}
|
|
259
251
|
return keys;
|
|
260
252
|
};
|
|
261
|
-
const
|
|
253
|
+
const getField = (definitions, definition, name) => {
|
|
254
|
+
const field = definition.fields?.find((f) => f.name.value === name);
|
|
255
|
+
if (field)
|
|
256
|
+
return field;
|
|
257
|
+
if (!("interfaces" in definition) ||
|
|
258
|
+
!definition.interfaces?.length)
|
|
259
|
+
return;
|
|
260
|
+
for (const interfaceName of definition.interfaces) {
|
|
261
|
+
const interfaceDefinition = definitions.find((d) => d.kind === Kind.INTERFACE_TYPE_DEFINITION &&
|
|
262
|
+
d.name.value === interfaceName.name.value);
|
|
263
|
+
if (!interfaceDefinition)
|
|
264
|
+
continue;
|
|
265
|
+
const field = interfaceDefinition.fields?.find((f) => f.name.value === name);
|
|
266
|
+
if (field)
|
|
267
|
+
return field;
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
const _proxy = (definitions, scalars, path, patches, { prev, resolvedType = resolveType(definitions, path), selectionSet, nonNull, } = {}) => {
|
|
262
271
|
const { parent = path, definition } = resolvedType;
|
|
263
272
|
let type = resolvedType.type;
|
|
264
|
-
if (!prev
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
273
|
+
if (!prev) {
|
|
274
|
+
if (nonNull) {
|
|
275
|
+
const typeName = type.kind === Kind.NAMED_TYPE
|
|
276
|
+
? type.name.value
|
|
277
|
+
: type.kind === Kind.NON_NULL_TYPE && type.type.kind === Kind.NAMED_TYPE
|
|
278
|
+
? type.type.name.value
|
|
279
|
+
: undefined;
|
|
280
|
+
if (typeName) {
|
|
281
|
+
const rawDefaultPatch = getDefaultPatch(typeName);
|
|
282
|
+
if (rawDefaultPatch)
|
|
283
|
+
patches = [rawDefaultPatch, ...patches];
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (patches.length) {
|
|
287
|
+
prev = _proxy(definitions, scalars, path, patches.slice(0, -1), {
|
|
288
|
+
selectionSet,
|
|
289
|
+
});
|
|
290
|
+
}
|
|
268
291
|
}
|
|
269
292
|
const rawPatch = patches.length > 0 ? patches[patches.length - 1] : undefined;
|
|
270
293
|
const patch = typeof rawPatch === "function"
|
|
271
294
|
? prev ? rawPatch(prev) : undefined
|
|
272
295
|
: rawPatch;
|
|
296
|
+
if (patch === clear) {
|
|
297
|
+
return _proxy(definitions, scalars, path, [], { selectionSet });
|
|
298
|
+
}
|
|
273
299
|
if (type.kind !== Kind.NON_NULL_TYPE) {
|
|
274
|
-
if (!patches.length)
|
|
275
|
-
return (prev ??
|
|
300
|
+
if (!patches.length && !nonNull) {
|
|
301
|
+
return (prev ??
|
|
302
|
+
(resolveType(definitions, path.split(".").slice(0, -1).join("."))
|
|
303
|
+
.definition?.kind ===
|
|
304
|
+
Kind.INPUT_OBJECT_TYPE_DEFINITION
|
|
305
|
+
? undefined
|
|
306
|
+
: null));
|
|
307
|
+
}
|
|
276
308
|
}
|
|
277
309
|
else
|
|
278
310
|
type = type.type;
|
|
@@ -280,6 +312,9 @@ const _proxy = (definitions, scalars, path, patches, { prev, resolvedType = reso
|
|
|
280
312
|
if (!patches.length)
|
|
281
313
|
return (prev ?? []);
|
|
282
314
|
const value = (patches.at(-1) ?? []);
|
|
315
|
+
if (value === clear) {
|
|
316
|
+
return _proxy(definitions, scalars, path, [], { selectionSet });
|
|
317
|
+
}
|
|
283
318
|
const previousArray = (prev ?? []);
|
|
284
319
|
const lastIndex = Math.max(previousArray.length - 1, 0);
|
|
285
320
|
const nextIndex = previousArray.length;
|
|
@@ -297,7 +332,15 @@ const _proxy = (definitions, scalars, path, patches, { prev, resolvedType = reso
|
|
|
297
332
|
}));
|
|
298
333
|
const arr = [];
|
|
299
334
|
for (let i = 0; i < length; i++) {
|
|
335
|
+
const nestType = type.type.kind === Kind.NAMED_TYPE
|
|
336
|
+
? type.type.name.value
|
|
337
|
+
: type.type.kind === Kind.NON_NULL_TYPE &&
|
|
338
|
+
type.type.type.kind === Kind.NAMED_TYPE
|
|
339
|
+
? type.type.type.name.value
|
|
340
|
+
: undefined;
|
|
300
341
|
arr[i] = _proxy(definitions, scalars, `${path}.${i}`, [
|
|
342
|
+
// TODO: should be handled in _proxy
|
|
343
|
+
nestType && !previousArray[i] ? getDefaultPatch(nestType) : undefined,
|
|
301
344
|
value[i],
|
|
302
345
|
i === lastIndex ? value.last : undefined,
|
|
303
346
|
i === nextIndex ? value.next : undefined,
|
|
@@ -326,7 +369,7 @@ const _proxy = (definitions, scalars, path, patches, { prev, resolvedType = reso
|
|
|
326
369
|
? getSelectionField(definitions, selectionSet, prop)
|
|
327
370
|
: undefined;
|
|
328
371
|
const unaliased = selectionField ? selectionField.name.value : prop;
|
|
329
|
-
const field =
|
|
372
|
+
const field = getField(definitions, definition, unaliased);
|
|
330
373
|
if (!field)
|
|
331
374
|
return target[prop];
|
|
332
375
|
// Get from patch
|
|
@@ -335,24 +378,28 @@ const _proxy = (definitions, scalars, path, patches, { prev, resolvedType = reso
|
|
|
335
378
|
const value = typeof rawValue === "function"
|
|
336
379
|
? rawValue(prev)
|
|
337
380
|
: rawValue;
|
|
381
|
+
if (value === clear) {
|
|
382
|
+
return target[prop] = _proxy(definitions, scalars, `${path}.${unaliased}`, [], { selectionSet });
|
|
383
|
+
}
|
|
338
384
|
if (value && typeof value === "object") {
|
|
339
385
|
const nonNullFieldType = field.type.kind === Kind.NON_NULL_TYPE
|
|
340
386
|
? field.type.type
|
|
341
387
|
: field.type;
|
|
342
388
|
if (nonNullFieldType.kind === Kind.LIST_TYPE) {
|
|
343
|
-
return target[prop] = _proxy(definitions, scalars, `${path}.${
|
|
389
|
+
return target[prop] = _proxy(definitions, scalars, `${path}.${unaliased}`, [value], {
|
|
344
390
|
prev: prev?.[prop],
|
|
345
391
|
selectionSet: selectionSet
|
|
346
|
-
? getSelectionSetSelection(definitions, selectionSet,
|
|
392
|
+
? getSelectionSetSelection(definitions, selectionSet, unaliased)
|
|
347
393
|
: undefined,
|
|
348
394
|
});
|
|
349
395
|
}
|
|
350
396
|
const childPatches = patches.map((p) => p[prop]).filter((v) => !!v && typeof v === "object");
|
|
351
|
-
return target[prop] = _proxy(definitions, scalars, `${path}.${
|
|
397
|
+
return target[prop] = _proxy(definitions, scalars, `${path}.${unaliased}`, childPatches, {
|
|
352
398
|
prev: prev?.[prop],
|
|
353
399
|
selectionSet: selectionSet
|
|
354
|
-
? getSelectionSetSelection(definitions, selectionSet,
|
|
400
|
+
? getSelectionSetSelection(definitions, selectionSet, unaliased)
|
|
355
401
|
: undefined,
|
|
402
|
+
nonNull: true,
|
|
356
403
|
});
|
|
357
404
|
}
|
|
358
405
|
if (value !== undefined) {
|
|
@@ -369,10 +416,12 @@ const _proxy = (definitions, scalars, path, patches, { prev, resolvedType = reso
|
|
|
369
416
|
// Otherwise generate it from type
|
|
370
417
|
return target[prop] = resolveValue(definitions, scalars, field.type, {
|
|
371
418
|
hostType: definition.name.value,
|
|
372
|
-
|
|
419
|
+
path,
|
|
420
|
+
prop: unaliased,
|
|
373
421
|
selectionSet: selectionSet
|
|
374
422
|
? getSelectionSetSelection(definitions, selectionSet, prop)
|
|
375
423
|
: undefined,
|
|
424
|
+
input: definition.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION,
|
|
376
425
|
});
|
|
377
426
|
};
|
|
378
427
|
const keys = [
|
|
@@ -405,7 +454,7 @@ const _proxy = (definitions, scalars, path, patches, { prev, resolvedType = reso
|
|
|
405
454
|
case Kind.UNION_TYPE_DEFINITION:
|
|
406
455
|
case Kind.INTERFACE_TYPE_DEFINITION: {
|
|
407
456
|
// When creating prev, we need hint the desired type...
|
|
408
|
-
const concreteType = resolveConcreteType(definitions, definition, patch, prev);
|
|
457
|
+
const concreteType = resolveConcreteType(definitions, definition, patch, prev, selectionSet);
|
|
409
458
|
return _proxy(definitions, scalars, concreteType.name.value, patches, {
|
|
410
459
|
prev,
|
|
411
460
|
selectionSet,
|
|
@@ -466,7 +515,7 @@ const _proxy = (definitions, scalars, path, patches, { prev, resolvedType = reso
|
|
|
466
515
|
mock.variables[name] = mockPrev.variables[name];
|
|
467
516
|
continue;
|
|
468
517
|
}
|
|
469
|
-
const result = resolveValue(definitions, scalars, variableDefinition.type, { hostType: `${path}Variables`, prop: name });
|
|
518
|
+
const result = resolveValue(definitions, scalars, variableDefinition.type, { hostType: `${path}Variables`, prop: name, input: false });
|
|
470
519
|
if (result != null) {
|
|
471
520
|
if (!mock.variables)
|
|
472
521
|
mock.variables = {};
|
|
@@ -489,6 +538,10 @@ const _proxy = (definitions, scalars, path, patches, { prev, resolvedType = reso
|
|
|
489
538
|
// Query, Mutation, Subscription
|
|
490
539
|
const operationKey = definition.operation[0].toUpperCase() +
|
|
491
540
|
definition.operation.slice(1);
|
|
541
|
+
if (value === clear) {
|
|
542
|
+
mock.data[prop] = _proxy(definitions, scalars, `${operationKey}.${selection.name.value}`, [], { selectionSet: selection.selectionSet });
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
492
545
|
mock.data[prop] = _proxy(definitions, scalars, `${operationKey}.${selection.name.value}`, value != null ? [value] : [], {
|
|
493
546
|
prev: mockPrev?.data?.[prop],
|
|
494
547
|
selectionSet: selection.selectionSet,
|
|
@@ -534,6 +587,14 @@ const _proxy = (definitions, scalars, path, patches, { prev, resolvedType = reso
|
|
|
534
587
|
const value = typeof scalar === "function" ? scalar(parent) : scalar;
|
|
535
588
|
return value;
|
|
536
589
|
}
|
|
590
|
+
case Kind.ENUM_TYPE_DEFINITION: {
|
|
591
|
+
const patch = patches[patches.length - 1];
|
|
592
|
+
if (patch != null)
|
|
593
|
+
return patch;
|
|
594
|
+
if (prev != null)
|
|
595
|
+
return prev;
|
|
596
|
+
return definition.values?.[0]?.name.value;
|
|
597
|
+
}
|
|
537
598
|
default:
|
|
538
599
|
throw new Error(`Unhandled definition kind '${definition.kind}'`);
|
|
539
600
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphql-data-generator",
|
|
3
|
-
"version": "0.3.0
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/voces/graphql-data-generator.git"
|
|
@@ -61,7 +61,8 @@
|
|
|
61
61
|
"react": "*",
|
|
62
62
|
"@types/react": "*",
|
|
63
63
|
"@apollo/client": "*",
|
|
64
|
-
"@testing-library/dom": "*"
|
|
64
|
+
"@testing-library/dom": "*",
|
|
65
|
+
"@testing-library/react": "*"
|
|
65
66
|
},
|
|
66
67
|
"peerDependenciesMeta": {
|
|
67
68
|
"react": {
|
|
@@ -75,6 +76,9 @@
|
|
|
75
76
|
},
|
|
76
77
|
"@testing-library/dom": {
|
|
77
78
|
"optional": true
|
|
79
|
+
},
|
|
80
|
+
"@testing-library/react": {
|
|
81
|
+
"optional": true
|
|
78
82
|
}
|
|
79
83
|
}
|
|
80
84
|
}
|
package/script/cli.js
CHANGED
|
@@ -27,7 +27,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
27
27
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
28
28
|
};
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
require("./_dnt.polyfills.js");
|
|
31
30
|
const dntShim = __importStar(require("./_dnt.shims.js"));
|
|
32
31
|
const node_fs_1 = require("node:fs");
|
|
33
32
|
const fast_glob_1 = __importDefault(require("fast-glob"));
|
package/script/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toObject = exports.formatCode = exports.proxy = exports.codegen = exports.init = void 0;
|
|
4
|
-
require("./_dnt.polyfills.js");
|
|
3
|
+
exports.toObject = exports.formatCode = exports.proxy = exports.operation = exports.clear = exports.codegen = exports.init = void 0;
|
|
5
4
|
var init_js_1 = require("./init.js");
|
|
6
5
|
Object.defineProperty(exports, "init", { enumerable: true, get: function () { return init_js_1.init; } });
|
|
7
6
|
var codegen_js_1 = require("./codegen.js");
|
|
8
7
|
Object.defineProperty(exports, "codegen", { enumerable: true, get: function () { return codegen_js_1.codegen; } });
|
|
9
8
|
var proxy_js_1 = require("./proxy.js");
|
|
9
|
+
Object.defineProperty(exports, "clear", { enumerable: true, get: function () { return proxy_js_1.clear; } });
|
|
10
|
+
Object.defineProperty(exports, "operation", { enumerable: true, get: function () { return proxy_js_1.operation; } });
|
|
10
11
|
Object.defineProperty(exports, "proxy", { enumerable: true, get: function () { return proxy_js_1.proxy; } });
|
|
11
12
|
var util_js_1 = require("./util.js");
|
|
12
13
|
Object.defineProperty(exports, "formatCode", { enumerable: true, get: function () { return util_js_1.formatCode; } });
|
package/script/init.js
CHANGED
|
@@ -1,16 +1,50 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
26
|
exports.init = void 0;
|
|
27
|
+
const dntShim = __importStar(require("./_dnt.shims.js"));
|
|
28
|
+
const node_module_1 = require("node:module");
|
|
4
29
|
const node_fs_1 = require("node:fs");
|
|
5
30
|
const graphql_1 = require("graphql");
|
|
6
|
-
const node_path_1 = require("node:path");
|
|
7
31
|
const graphql_tag_pluck_1 = require("@graphql-tools/graphql-tag-pluck");
|
|
8
32
|
const proxy_js_1 = require("./proxy.js");
|
|
9
33
|
const util_js_1 = require("./util.js");
|
|
34
|
+
const node_path_1 = require("node:path");
|
|
35
|
+
globalThis.require ??= (0, node_module_1.createRequire)(dntShim.Deno.cwd());
|
|
10
36
|
const files = {};
|
|
11
|
-
const loadFile = (path) =>
|
|
12
|
-
|
|
13
|
-
|
|
37
|
+
const loadFile = (path) => {
|
|
38
|
+
if (files[path])
|
|
39
|
+
return files[path];
|
|
40
|
+
const raw = files[path] = (0, node_fs_1.readFileSync)(path, "utf-8");
|
|
41
|
+
const imports = Array.from(raw.matchAll(/#import "(.*)"/gm), ([, importPath]) => loadFile(require.resolve(importPath.startsWith(".")
|
|
42
|
+
? (0, node_path_1.resolve)(dntShim.Deno.cwd(), (0, node_path_1.dirname)(path), importPath)
|
|
43
|
+
: importPath, { paths: [dntShim.Deno.cwd()] })));
|
|
44
|
+
if (!imports.length)
|
|
45
|
+
return raw;
|
|
46
|
+
return [raw, ...imports].join("\n\n");
|
|
47
|
+
};
|
|
14
48
|
const getOperationContentMap = {};
|
|
15
49
|
const getOperationContent = (path, operationName) => {
|
|
16
50
|
const existing = getOperationContentMap[path];
|
|
@@ -26,7 +60,7 @@ const getOperationContent = (path, operationName) => {
|
|
|
26
60
|
const document = (0, graphql_1.parse)(s);
|
|
27
61
|
const firstOp = document.definitions.find((d) => d.kind === graphql_1.Kind.OPERATION_DEFINITION);
|
|
28
62
|
if (!firstOp)
|
|
29
|
-
throw new Error(`
|
|
63
|
+
throw new Error(`Could not find an operation in ${path}`);
|
|
30
64
|
return [firstOp.name?.value, document];
|
|
31
65
|
}));
|
|
32
66
|
}
|
|
@@ -35,7 +69,16 @@ const getOperationContent = (path, operationName) => {
|
|
|
35
69
|
}
|
|
36
70
|
return getOperationContent(path, operationName);
|
|
37
71
|
};
|
|
38
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Initialize the data builder.
|
|
74
|
+
* @param schema The plain text of your schema.
|
|
75
|
+
* @param queries List of queries exported from generated types.
|
|
76
|
+
* @param mutations List of mutations exported from generated types.
|
|
77
|
+
* @param subscriptions List of subscriptions exported from generated types.
|
|
78
|
+
* @param types List of types exported from generated types.
|
|
79
|
+
* @param inputs List of types exported from generated types.
|
|
80
|
+
* @param scalars A mapping to generate scalar values. Function values will be invoked with their `__typename`.
|
|
81
|
+
*/
|
|
39
82
|
const init = (schema, queries, mutations, subscriptions, types, inputs, scalars, options) => (fn) => {
|
|
40
83
|
const doc = (0, graphql_1.parse)(schema);
|
|
41
84
|
const build = {};
|
|
@@ -160,10 +203,13 @@ const init = (schema, queries, mutations, subscriptions, types, inputs, scalars,
|
|
|
160
203
|
if (transforms[name] && "default" in transforms[name]) {
|
|
161
204
|
patches = [transforms[name].default, ...patches];
|
|
162
205
|
}
|
|
163
|
-
const {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
206
|
+
const { mock, parsedQuery } = (0, proxy_js_1.withGetDefaultPatch)((type) => transforms[type]?.default, () => {
|
|
207
|
+
const { request: { query: parsedQuery, ...request }, ...raw } = (0, proxy_js_1.operation)(doc.definitions, scalars, query, ...patches);
|
|
208
|
+
const mock = (0, util_js_1.toObject)({
|
|
209
|
+
request: { ...request },
|
|
210
|
+
...raw,
|
|
211
|
+
});
|
|
212
|
+
return { mock, parsedQuery };
|
|
167
213
|
});
|
|
168
214
|
mock.request.query = parsedQuery;
|
|
169
215
|
Error.captureStackTrace(mock, builder);
|