graphql-data-generator 0.2.2 → 0.2.4
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/codegen.js +11 -3
- package/esm/plugin.cjs +2 -1
- package/esm/proxy.js +1 -1
- package/esm/util.js +22 -8
- package/package.json +2 -2
package/esm/codegen.js
CHANGED
|
@@ -289,7 +289,9 @@ const defaultScalars = {
|
|
|
289
289
|
ID: "string",
|
|
290
290
|
};
|
|
291
291
|
export const codegen = (schema, files, { enums = "literals", scalars, includeTypenames = true, exports = [], typesFile, namingConvention = "keep", } = {}) => {
|
|
292
|
-
const rename =
|
|
292
|
+
const rename = typesFile
|
|
293
|
+
? convertFactory({ namingConvention })
|
|
294
|
+
: (v) => v;
|
|
293
295
|
const schemaDoc = parse(typeof schema === "string" ? schema : printSchema(schema));
|
|
294
296
|
scalars = { ...defaultScalars, ...scalars };
|
|
295
297
|
const types = {};
|
|
@@ -391,6 +393,10 @@ export const codegen = (schema, files, { enums = "literals", scalars, includeTyp
|
|
|
391
393
|
console.warn(`Skipping unnamed operation in '${path}'`);
|
|
392
394
|
continue;
|
|
393
395
|
}
|
|
396
|
+
if (operations[definition.operation].some((o) => o.name === name)) {
|
|
397
|
+
console.warn(`Skipping duplicate operation '${name}'`);
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
394
400
|
operations[definition.operation].push({
|
|
395
401
|
name,
|
|
396
402
|
path,
|
|
@@ -501,7 +507,7 @@ ${collection.map((o) => ` ${o.name}: "${relative(process.cwd(), o.path)}",`).jo
|
|
|
501
507
|
throw new Error(`Could not find input '${i}'`);
|
|
502
508
|
return `${exports.includes("types") ? "export " : ""}type ${i} = ${serializeType(serializeInput(def.fields ?? [], false, {}, references), true)};`;
|
|
503
509
|
}).filter(filterOutputTypes), `export type Inputs = {
|
|
504
|
-
${Array.from(handledInputs).map((i) => ` ${i}: ${i};`).join("\n")}
|
|
510
|
+
${Array.from(handledInputs).map((i) => ` ${i}: ${rename(i)};`).join("\n")}
|
|
505
511
|
};`, `export const inputs = [${Array.from(handledInputs).map((i) => `"${i}"`).join(", ")}] as const;`);
|
|
506
512
|
}
|
|
507
513
|
const usedTypes = Object.entries(types)
|
|
@@ -549,7 +555,9 @@ ${usedTypes.map(([name]) => ` ${name}: ${rename(name)};`).join("\n")}
|
|
|
549
555
|
}
|
|
550
556
|
else if (enums.startsWith("import:") &&
|
|
551
557
|
usedReferences.some((r) => r.kind === Kind.ENUM_TYPE_DEFINITION)) {
|
|
552
|
-
serializedTypes.unshift(`import {
|
|
558
|
+
serializedTypes.unshift(`import {
|
|
559
|
+
${usedReferences.filter((r) => r.kind === Kind.ENUM_TYPE_DEFINITION).map((r) => r.name.value).join(",\n ")},
|
|
560
|
+
} from "${enums.slice(7)}";`);
|
|
553
561
|
}
|
|
554
562
|
return serializedTypes.join("\n\n") + "\n";
|
|
555
563
|
};
|
package/esm/plugin.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
module.exports = {
|
|
3
3
|
async plugin(schema, documents, config) {
|
|
4
4
|
const { codegen } = await import("./codegen.js");
|
|
5
|
-
|
|
5
|
+
const { formatCode } = await import("./util.js");
|
|
6
|
+
return (config.banner ?? "") + await formatCode(codegen(schema, documents.map((d) => ({ path: d.location, content: d.rawSDL })), { namingConvention: "change-case-all#pascalCase", ...config }));
|
|
6
7
|
},
|
|
7
8
|
};
|
package/esm/proxy.js
CHANGED
|
@@ -62,7 +62,7 @@ const resolveType = (definitions, path) => {
|
|
|
62
62
|
}
|
|
63
63
|
type = argument.type;
|
|
64
64
|
}
|
|
65
|
-
while (parts[i + 2]?.match(/^\d
|
|
65
|
+
while (parts[i + 2]?.match(/^\d+$/)) {
|
|
66
66
|
if (type.kind === Kind.NON_NULL_TYPE)
|
|
67
67
|
type = type.type;
|
|
68
68
|
if (type.kind !== Kind.LIST_TYPE)
|
package/esm/util.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
2
|
export const raise = (error) => {
|
|
3
3
|
if (typeof error === "string") {
|
|
4
4
|
const err = new Error(error);
|
|
@@ -10,14 +10,28 @@ export const raise = (error) => {
|
|
|
10
10
|
export const formatCode = async (input) => {
|
|
11
11
|
try {
|
|
12
12
|
return await new Promise((resolve, reject) => {
|
|
13
|
-
const process =
|
|
14
|
-
|
|
15
|
-
return reject(error);
|
|
16
|
-
if (stderr)
|
|
17
|
-
return reject(new Error(stderr));
|
|
18
|
-
resolve(stdout);
|
|
13
|
+
const process = spawn("deno", ["fmt", "-"], {
|
|
14
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
19
15
|
});
|
|
20
|
-
|
|
16
|
+
let output = "";
|
|
17
|
+
let errorOutput = "";
|
|
18
|
+
process.stdout.on("data", (data) => {
|
|
19
|
+
output += data.toString();
|
|
20
|
+
});
|
|
21
|
+
process.stderr.on("data", (data) => {
|
|
22
|
+
errorOutput += data.toString();
|
|
23
|
+
});
|
|
24
|
+
process.on("close", (code) => {
|
|
25
|
+
if (code !== 0 || errorOutput) {
|
|
26
|
+
reject(new Error(errorOutput || `Process exited with code ${code}`));
|
|
27
|
+
}
|
|
28
|
+
else
|
|
29
|
+
resolve(output);
|
|
30
|
+
});
|
|
31
|
+
process.on("error", (error) => {
|
|
32
|
+
reject(error);
|
|
33
|
+
});
|
|
34
|
+
process.stdin.write(input);
|
|
21
35
|
process.stdin?.end();
|
|
22
36
|
});
|
|
23
37
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphql-data-generator",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
|
-
"url": "git+https://github.com/
|
|
6
|
+
"url": "git+https://github.com/voces/graphql-data-generator.git"
|
|
7
7
|
},
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"bugs": {
|