graphql-data-generator 0.1.4 → 0.1.6
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/esm/cli.js +15 -4
- package/esm/codegen.js +35 -17
- package/package.json +5 -3
- package/types/codegen.d.ts +2 -1
- package/types/extendedTypes.d.ts +1 -1
package/esm/cli.js
CHANGED
|
@@ -14,8 +14,9 @@ const args = parseArgs({
|
|
|
14
14
|
operations: { type: "string", multiple: true },
|
|
15
15
|
scalar: { type: "string", multiple: true },
|
|
16
16
|
outfile: { type: "string" },
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
enums: { type: "boolean" },
|
|
18
|
+
notypenames: { type: "boolean" },
|
|
19
|
+
exports: { type: "string", multiple: true },
|
|
19
20
|
},
|
|
20
21
|
}).values;
|
|
21
22
|
const findFirst = async (path) => {
|
|
@@ -59,11 +60,21 @@ if (args.scalar) {
|
|
|
59
60
|
scalars[key] = value;
|
|
60
61
|
}
|
|
61
62
|
}
|
|
63
|
+
const validExports = ["operations", "types"];
|
|
64
|
+
const exports = args.exports
|
|
65
|
+
? args.exports.filter((e) => {
|
|
66
|
+
if (!validExports.includes(e)) {
|
|
67
|
+
throw new Error(`Invalid export. Must be ${validExports.join(", ")}`);
|
|
68
|
+
}
|
|
69
|
+
return true;
|
|
70
|
+
})
|
|
71
|
+
: [];
|
|
62
72
|
try {
|
|
63
73
|
const file = await formatCode(codegen(schema, operations, {
|
|
64
|
-
useEnums: args.
|
|
65
|
-
includeTypenames: args.
|
|
74
|
+
useEnums: args.enums ?? false,
|
|
75
|
+
includeTypenames: !args.notypenames,
|
|
66
76
|
scalars,
|
|
77
|
+
exports,
|
|
67
78
|
}));
|
|
68
79
|
if (args.outfile)
|
|
69
80
|
await dntShim.Deno.writeTextFile(args.outfile, file);
|
package/esm/codegen.js
CHANGED
|
@@ -278,7 +278,7 @@ const simpleType = (type, types, optional = true) => {
|
|
|
278
278
|
};
|
|
279
279
|
}
|
|
280
280
|
};
|
|
281
|
-
export const codegen = (schema, files, { useEnums = true, scalars = {}, includeTypenames = true } = {}) => {
|
|
281
|
+
export const codegen = (schema, files, { useEnums = true, scalars = {}, includeTypenames = true, exports = [], } = {}) => {
|
|
282
282
|
const schemaDoc = parse(schema);
|
|
283
283
|
const types = {};
|
|
284
284
|
const inputs = {};
|
|
@@ -374,9 +374,21 @@ export const codegen = (schema, files, { useEnums = true, scalars = {}, includeT
|
|
|
374
374
|
}
|
|
375
375
|
}
|
|
376
376
|
}
|
|
377
|
+
const operationDataName = (name, type) => {
|
|
378
|
+
if (inputs[name] || types[name]) {
|
|
379
|
+
return `${name}${type[0].toUpperCase()}${type.slice(1)}`;
|
|
380
|
+
}
|
|
381
|
+
for (const key in operations) {
|
|
382
|
+
if (type === key)
|
|
383
|
+
continue;
|
|
384
|
+
if (operations[key].some((o) => o.name === name))
|
|
385
|
+
return `${name}${type[0].toUpperCase()}${type.slice(1)}`;
|
|
386
|
+
}
|
|
387
|
+
return name;
|
|
388
|
+
};
|
|
377
389
|
const handledInputs = new Set();
|
|
378
390
|
const serializedTypes = Object.entries(operations).filter(([, v]) => v.length)
|
|
379
|
-
.flatMap(([
|
|
391
|
+
.flatMap(([operationType, collection]) => [
|
|
380
392
|
...collection.flatMap((c) => c.definition.variableDefinitions?.map((v) => {
|
|
381
393
|
let type = getType({
|
|
382
394
|
type: v.type,
|
|
@@ -423,20 +435,26 @@ export const codegen = (schema, files, { useEnums = true, scalars = {}, includeT
|
|
|
423
435
|
}
|
|
424
436
|
// return `type ${type.value} = ${serializeType(inputType)};`;
|
|
425
437
|
}).filter(Boolean)),
|
|
426
|
-
|
|
438
|
+
...collection.flatMap((o) => {
|
|
439
|
+
const name = operationDataName(o.name, operationType);
|
|
440
|
+
const arr = [
|
|
441
|
+
`${exports.includes("operations") ? "export " : ""}type ${name} = ${serializeType(getOperationType(o.definition, types, fragments, roots[o.definition.operation], references, includeTypenames), false)};`,
|
|
442
|
+
];
|
|
443
|
+
if (o.definition.variableDefinitions?.length) {
|
|
444
|
+
arr.push(`${exports.includes("operations") ? "export " : ""}type ${name}Variables = ${serializeType(getOperationVariables(o.definition, references), true)};`);
|
|
445
|
+
}
|
|
446
|
+
return arr;
|
|
447
|
+
}),
|
|
448
|
+
`export type ${operationNames[operationType].types} = {
|
|
427
449
|
${collection.map((o) => {
|
|
428
|
-
const
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
? `
|
|
433
|
-
variables: ${serializeType(variables, true, 2)};`
|
|
434
|
-
: ""}
|
|
435
|
-
};`;
|
|
450
|
+
const name = operationDataName(o.name, operationType);
|
|
451
|
+
return ` ${o.name}: { data: ${name};${o.definition.variableDefinitions?.length
|
|
452
|
+
? ` variables: ${name}Variables;`
|
|
453
|
+
: ""} };`;
|
|
436
454
|
}).join("\n")}
|
|
437
455
|
};
|
|
438
456
|
|
|
439
|
-
export const ${operationNames[
|
|
457
|
+
export const ${operationNames[operationType].list} = {
|
|
440
458
|
${collection.map((o) => ` ${o.name}: "${o.path}",`).join("\n")}
|
|
441
459
|
};`,
|
|
442
460
|
]);
|
|
@@ -445,7 +463,7 @@ ${collection.map((o) => ` ${o.name}: "${o.path}",`).join("\n")}
|
|
|
445
463
|
const def = inputs[i]?.[0];
|
|
446
464
|
if (!def)
|
|
447
465
|
throw new Error(`Could not find input '${i}'`);
|
|
448
|
-
return
|
|
466
|
+
return `${exports.includes("types") ? "export " : ""}type ${i} = ${serializeType(serializeInput(def.fields ?? [], false, {}, references), true)};`;
|
|
449
467
|
}), `export type Inputs = {
|
|
450
468
|
${Array.from(handledInputs).map((i) => ` ${i}: ${i};`).join("\n")}
|
|
451
469
|
};`, `export const inputs = [${Array.from(handledInputs).map((i) => `"${i}"`).join(", ")}] as const;`);
|
|
@@ -454,7 +472,7 @@ ${Array.from(handledInputs).map((i) => ` ${i}: ${i};`).join("\n")}
|
|
|
454
472
|
.map(([name, info]) => [name, ...info])
|
|
455
473
|
.filter((data) => data[1].kind === "ObjectTypeDefinition" && data[2].size > 0);
|
|
456
474
|
if (usedTypes.length) {
|
|
457
|
-
serializedTypes.unshift(...usedTypes.map(([name, type, usage]) =>
|
|
475
|
+
serializedTypes.unshift(...usedTypes.map(([name, type, usage]) => `${exports.includes("types") ? "export " : ""}type ${name} = {
|
|
458
476
|
${includeTypenames ? ` __typename: "${name}";\n` : ""}${type.fields?.filter((f) => usage.has(f.name.value)).map((v) => ` ${v.name.value}: ${serializeType(simpleType(v.type, types))};`).join("\n")}
|
|
459
477
|
};`), `export type Types = {
|
|
460
478
|
${usedTypes.map(([name]) => ` ${name}: ${name};`).join("\n")}
|
|
@@ -464,16 +482,16 @@ ${usedTypes.map(([name]) => ` ${name}: ${name};`).join("\n")}
|
|
|
464
482
|
serializedTypes.unshift(...usedReferences.map((r) => {
|
|
465
483
|
// TODO: warn if missing and use unknown instead
|
|
466
484
|
if (r.kind === "ScalarTypeDefinition") {
|
|
467
|
-
return
|
|
485
|
+
return `${exports.includes("types") ? "export " : ""}type ${r.name.value} = ${scalars[r.name.value] ??
|
|
468
486
|
raise(`Could not find scalar '${r.name.value}'`)};`;
|
|
469
487
|
}
|
|
470
488
|
if (useEnums) {
|
|
471
|
-
return
|
|
489
|
+
return `${exports.includes("types") ? "export " : ""}enum ${r.name.value} {
|
|
472
490
|
${r.values?.map((r) => r.name.value).join(",\n ")},
|
|
473
491
|
}`;
|
|
474
492
|
}
|
|
475
493
|
else {
|
|
476
|
-
return
|
|
494
|
+
return `${exports.includes("types") ? "export " : ""}type ${r.name.value} = ${r.values?.map((r) => `"${r.name.value}"`).join(" | ")};`;
|
|
477
495
|
}
|
|
478
496
|
}));
|
|
479
497
|
return serializedTypes.join("\n\n") + "\n";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphql-data-generator",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/vocesgraphql-data-generator/.git"
|
|
@@ -25,11 +25,13 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"fast-glob": "*",
|
|
28
|
-
"graphql": "*",
|
|
29
28
|
"@deno/shim-deno": "~0.18.0"
|
|
30
29
|
},
|
|
31
30
|
"devDependencies": {
|
|
32
31
|
"@types/node": "^20.9.0"
|
|
33
32
|
},
|
|
34
|
-
"_generatedBy": "dnt@dev"
|
|
33
|
+
"_generatedBy": "dnt@dev",
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"graphql": "*"
|
|
36
|
+
}
|
|
35
37
|
}
|
package/types/codegen.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
export declare const codegen: (schema: string, files: {
|
|
2
2
|
path: string;
|
|
3
3
|
content: string;
|
|
4
|
-
}[], { useEnums, scalars, includeTypenames }?: {
|
|
4
|
+
}[], { useEnums, scalars, includeTypenames, exports, }?: {
|
|
5
5
|
useEnums?: boolean;
|
|
6
6
|
scalars?: Record<string, string | undefined>;
|
|
7
7
|
includeTypenames?: boolean;
|
|
8
|
+
exports?: ("types" | "operations")[];
|
|
8
9
|
}) => string;
|
|
9
10
|
export declare const loadFiles: (schemaPath: string, operationDirs: string[]) => Promise<[schema: string, operations: {
|
|
10
11
|
path: string;
|
package/types/extendedTypes.d.ts
CHANGED
|
@@ -40,7 +40,7 @@ type MapOperationsToBuilders<T, Transforms, Extra> = {
|
|
|
40
40
|
data: infer U;
|
|
41
41
|
} ? U extends Record<string, unknown> ? U : Record<string, unknown> : Record<string, unknown>, T[K] extends {
|
|
42
42
|
variables: infer U;
|
|
43
|
-
} ? U :
|
|
43
|
+
} ? U : never, K extends keyof Transforms ? Transforms[K] : ContravariantEmpty, Extra>;
|
|
44
44
|
};
|
|
45
45
|
type ResolveOperationConflicts<T, Name extends string, A, B, C, D> = Omit<T, keyof A | keyof B | keyof C | keyof D> & PrefixKeys<Pick<T, keyof T & (keyof A | keyof B | keyof C | keyof D)>, Name>;
|
|
46
46
|
type ResolveConflicts<Queries, Mutations, Subscriptions, Types, Inputs> = ResolveOperationConflicts<Queries, "queries", Mutations, Subscriptions, Types, Inputs> & ResolveOperationConflicts<Mutations, "mutations", Queries, Subscriptions, Types, Inputs> & ResolveOperationConflicts<Subscriptions, "subscriptions", Queries, Mutations, Types, Inputs>;
|