graphql-data-generator 0.2.1 → 0.2.3

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 CHANGED
@@ -96,6 +96,10 @@ const createPost = build.CreatePost({ data: { createPost: { id: "post-id" } } })
96
96
  - `import:file` Import enums from the provided path. E.g.,
97
97
  `import { Status } from "file";`
98
98
  - `exports=[operations|types]`: Toggle exporting of operations and/or types.
99
+ - `namingConvention=NamingConvention`: Specify a
100
+ [`NamingConvention`](https://the-guild.dev/graphql/codegen/docs/config-reference/naming-convention)
101
+ for generated types. Defaults to `change-case-all#pascalCase` if using the
102
+ plugin, otherwise defaults to `keep`.
99
103
  - `notypenames`: Toggle automatic inclusion of `__typename`.
100
104
  - `operations=dir`: Restrict generation of operations to a specific directory.
101
105
  Can be used multiple times for multiple directories.
package/esm/cli.js CHANGED
@@ -12,6 +12,7 @@ const args = parseArgs({
12
12
  banner: { type: "string" },
13
13
  enums: { type: "string" },
14
14
  exports: { type: "string", multiple: true },
15
+ namingConvention: { type: "string" },
15
16
  notypenames: { type: "boolean" },
16
17
  operations: { type: "string", multiple: true },
17
18
  outfile: { type: "string" },
@@ -84,6 +85,7 @@ try {
84
85
  scalars,
85
86
  exports,
86
87
  typesFile: args.typesFile,
88
+ namingConvention: args.namingConvention,
87
89
  }));
88
90
  if (args.outfile)
89
91
  await dntShim.Deno.writeTextFile(args.outfile, file);
package/esm/codegen.js CHANGED
@@ -4,6 +4,7 @@ import fg from "fast-glob";
4
4
  import { join, relative } from "node:path";
5
5
  import { raise } from "./util.js";
6
6
  import process from "node:process";
7
+ import { convertFactory, } from "@graphql-codegen/visitor-plugin-common";
7
8
  const getType = ({ type, ...props }) => {
8
9
  if (type.kind === "NamedType") {
9
10
  if (props.selections) {
@@ -287,7 +288,10 @@ const defaultScalars = {
287
288
  Boolean: "boolean",
288
289
  ID: "string",
289
290
  };
290
- export const codegen = (schema, files, { enums = "literals", scalars, includeTypenames = true, exports = [], typesFile, } = {}) => {
291
+ export const codegen = (schema, files, { enums = "literals", scalars, includeTypenames = true, exports = [], typesFile, namingConvention = "keep", } = {}) => {
292
+ const rename = typesFile
293
+ ? convertFactory({ namingConvention })
294
+ : (v) => v;
291
295
  const schemaDoc = parse(typeof schema === "string" ? schema : printSchema(schema));
292
296
  scalars = { ...defaultScalars, ...scalars };
293
297
  const types = {};
@@ -389,6 +393,10 @@ export const codegen = (schema, files, { enums = "literals", scalars, includeTyp
389
393
  console.warn(`Skipping unnamed operation in '${path}'`);
390
394
  continue;
391
395
  }
396
+ if (operations[definition.operation].some((o) => o.name === name)) {
397
+ console.warn(`Skipping duplicate operation '${name}'`);
398
+ continue;
399
+ }
392
400
  operations[definition.operation].push({
393
401
  name,
394
402
  path,
@@ -404,9 +412,10 @@ export const codegen = (schema, files, { enums = "literals", scalars, includeTyp
404
412
  }
405
413
  }
406
414
  }
407
- const operationDataName = (name, type) => {
415
+ const operationDataName = (operation, type) => {
416
+ const name = operation.name;
408
417
  if (inputs[name] || types[name] || typesFile) {
409
- return `${name}${type[0].toUpperCase()}${type.slice(1)}`;
418
+ return rename(`${name}${type[0].toUpperCase()}${type.slice(1)}`);
410
419
  }
411
420
  for (const key in operations) {
412
421
  if (type === key)
@@ -467,7 +476,7 @@ export const codegen = (schema, files, { enums = "literals", scalars, includeTyp
467
476
  // return `type ${type.value} = ${serializeType(inputType)};`;
468
477
  }).filter(Boolean)).filter(filterOutputTypes),
469
478
  ...collection.flatMap((o) => {
470
- const name = operationDataName(o.name, operationType);
479
+ const name = operationDataName(o, operationType);
471
480
  const resolvedOperationType = getOperationType(o.definition, types, fragments, roots[o.definition.operation], references, includeTypenames);
472
481
  const arr = [
473
482
  `${exports.includes("operations") ? "export " : ""}type ${name} = ${serializeType(resolvedOperationType, false, undefined)};`,
@@ -480,7 +489,7 @@ export const codegen = (schema, files, { enums = "literals", scalars, includeTyp
480
489
  ,
481
490
  `export type ${operationNames[operationType].types} = {
482
491
  ${collection.map((o) => {
483
- const name = operationDataName(o.name, operationType);
492
+ const name = operationDataName(o, operationType);
484
493
  return ` ${o.name}: { data: ${name};${o.definition.variableDefinitions?.length
485
494
  ? ` variables: ${name}Variables;`
486
495
  : ""} };`;
@@ -498,7 +507,7 @@ ${collection.map((o) => ` ${o.name}: "${relative(process.cwd(), o.path)}",`).jo
498
507
  throw new Error(`Could not find input '${i}'`);
499
508
  return `${exports.includes("types") ? "export " : ""}type ${i} = ${serializeType(serializeInput(def.fields ?? [], false, {}, references), true)};`;
500
509
  }).filter(filterOutputTypes), `export type Inputs = {
501
- ${Array.from(handledInputs).map((i) => ` ${i}: ${i};`).join("\n")}
510
+ ${Array.from(handledInputs).map((i) => ` ${i}: ${rename(i)};`).join("\n")}
502
511
  };`, `export const inputs = [${Array.from(handledInputs).map((i) => `"${i}"`).join(", ")}] as const;`);
503
512
  }
504
513
  const usedTypes = Object.entries(types)
@@ -508,7 +517,7 @@ ${Array.from(handledInputs).map((i) => ` ${i}: ${i};`).join("\n")}
508
517
  serializedTypes.unshift(...usedTypes.map(([name, type, usage]) => `${exports.includes("types") ? "export " : ""}type ${name} = {
509
518
  ${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")}
510
519
  };`).filter(filterOutputTypes), `export type Types = {
511
- ${usedTypes.map(([name]) => ` ${name}: ${name};`).join("\n")}
520
+ ${usedTypes.map(([name]) => ` ${name}: ${rename(name)};`).join("\n")}
512
521
  };`, `export const types = [${usedTypes.map(([name]) => `"${name}"`).join(", ")}] as const;`);
513
522
  }
514
523
  const usedReferences = Object.values(references).filter((r) => r[1]).map((r) => r[0]);
@@ -529,7 +538,7 @@ ${usedTypes.map(([name]) => ` ${name}: ${name};`).join("\n")}
529
538
  }).filter(filterOutputTypes));
530
539
  if (typesFile) {
531
540
  const operationsImports = (operations) => operations.flatMap((o) => {
532
- const prefix = operationDataName(o.name, o.definition.operation);
541
+ const prefix = operationDataName(o, o.definition.operation);
533
542
  return o.definition.variableDefinitions?.length
534
543
  ? [`${prefix}`, `${prefix}Variables`]
535
544
  : `${prefix}`;
@@ -538,8 +547,8 @@ ${usedTypes.map(([name]) => ` ${name}: ${name};`).join("\n")}
538
547
  ...operationsImports(operations.query),
539
548
  ...operationsImports(operations.mutation),
540
549
  ...operationsImports(operations.subscription),
541
- ...usedTypes.map((u) => u[0]),
542
- ...Array.from(handledInputs),
550
+ ...usedTypes.map((u) => rename(u[0])),
551
+ ...Array.from(handledInputs, (i) => rename(i)),
543
552
  ];
544
553
  serializedTypes.unshift(`import {\n ${imports.sort().join(",\n ")},
545
554
  } from "${typesFile}";`);
package/esm/plugin.cjs CHANGED
@@ -2,6 +2,6 @@
2
2
  module.exports = {
3
3
  async plugin(schema, documents, config) {
4
4
  const { codegen } = await import("./codegen.js");
5
- return (config.banner ?? "") + codegen(schema, documents.map((d) => ({ path: d.location, content: d.rawSDL })), config);
5
+ return (config.banner ?? "") + codegen(schema, documents.map((d) => ({ path: d.location, content: d.rawSDL })), { namingConvention: "change-case-all#pascalCase", ...config });
6
6
  },
7
7
  };
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-data-generator",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/vocesgraphql-data-generator/.git"
@@ -30,6 +30,7 @@
30
30
  "graphql-data-generator": "./esm/cli.js"
31
31
  },
32
32
  "dependencies": {
33
+ "@graphql-codegen/visitor-plugin-common": "*",
33
34
  "fast-glob": "*",
34
35
  "@deno/shim-deno": "~0.18.0"
35
36
  },
@@ -1,13 +1,15 @@
1
1
  import type { GraphQLSchema } from "graphql";
2
+ import { type NamingConvention } from "@graphql-codegen/visitor-plugin-common";
2
3
  export declare const codegen: (schema: GraphQLSchema | string, files: {
3
4
  path: string;
4
5
  content: string;
5
- }[], { enums, scalars, includeTypenames, exports, typesFile, }?: {
6
+ }[], { enums, scalars, includeTypenames, exports, typesFile, namingConvention, }?: {
6
7
  enums?: string;
7
8
  scalars?: Record<string, string | undefined>;
8
9
  includeTypenames?: boolean;
9
10
  exports?: ("types" | "operations")[];
10
11
  typesFile?: string;
12
+ namingConvention?: NamingConvention;
11
13
  }) => string;
12
14
  export declare const loadFiles: (schemaPath: string, operationDirs: string[]) => Promise<[schema: string, operations: {
13
15
  path: string;