graphql-data-generator 0.2.0 → 0.2.2

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
@@ -1,9 +1,10 @@
1
1
  import { readFile } from "node:fs/promises";
2
- import { Kind, OperationTypeNode, parse, printSchema } from "graphql";
2
+ import { Kind, parse, printSchema } from "graphql";
3
3
  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,8 @@ 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 = convertFactory({ namingConvention });
291
293
  const schemaDoc = parse(typeof schema === "string" ? schema : printSchema(schema));
292
294
  scalars = { ...defaultScalars, ...scalars };
293
295
  const types = {};
@@ -309,13 +311,13 @@ export const codegen = (schema, files, { enums = "literals", scalars, includeTyp
309
311
  {
310
312
  for (const operationType of definition.operationTypes) {
311
313
  switch (operationType.operation) {
312
- case OperationTypeNode.QUERY:
314
+ case "query":
313
315
  queryKey = operationType.type.name.value;
314
316
  break;
315
- case OperationTypeNode.MUTATION:
317
+ case "mutation":
316
318
  mutationKey = operationType.type.name.value;
317
319
  break;
318
- case OperationTypeNode.SUBSCRIPTION:
320
+ case "subscription":
319
321
  subscriptionKey = operationType.type.name.value;
320
322
  break;
321
323
  }
@@ -404,9 +406,10 @@ export const codegen = (schema, files, { enums = "literals", scalars, includeTyp
404
406
  }
405
407
  }
406
408
  }
407
- const operationDataName = (name, type) => {
409
+ const operationDataName = (operation, type) => {
410
+ const name = operation.name;
408
411
  if (inputs[name] || types[name] || typesFile) {
409
- return `${name}${type[0].toUpperCase()}${type.slice(1)}`;
412
+ return rename(`${name}${type[0].toUpperCase()}${type.slice(1)}`);
410
413
  }
411
414
  for (const key in operations) {
412
415
  if (type === key)
@@ -467,7 +470,7 @@ export const codegen = (schema, files, { enums = "literals", scalars, includeTyp
467
470
  // return `type ${type.value} = ${serializeType(inputType)};`;
468
471
  }).filter(Boolean)).filter(filterOutputTypes),
469
472
  ...collection.flatMap((o) => {
470
- const name = operationDataName(o.name, operationType);
473
+ const name = operationDataName(o, operationType);
471
474
  const resolvedOperationType = getOperationType(o.definition, types, fragments, roots[o.definition.operation], references, includeTypenames);
472
475
  const arr = [
473
476
  `${exports.includes("operations") ? "export " : ""}type ${name} = ${serializeType(resolvedOperationType, false, undefined)};`,
@@ -480,7 +483,7 @@ export const codegen = (schema, files, { enums = "literals", scalars, includeTyp
480
483
  ,
481
484
  `export type ${operationNames[operationType].types} = {
482
485
  ${collection.map((o) => {
483
- const name = operationDataName(o.name, operationType);
486
+ const name = operationDataName(o, operationType);
484
487
  return ` ${o.name}: { data: ${name};${o.definition.variableDefinitions?.length
485
488
  ? ` variables: ${name}Variables;`
486
489
  : ""} };`;
@@ -508,7 +511,7 @@ ${Array.from(handledInputs).map((i) => ` ${i}: ${i};`).join("\n")}
508
511
  serializedTypes.unshift(...usedTypes.map(([name, type, usage]) => `${exports.includes("types") ? "export " : ""}type ${name} = {
509
512
  ${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
513
  };`).filter(filterOutputTypes), `export type Types = {
511
- ${usedTypes.map(([name]) => ` ${name}: ${name};`).join("\n")}
514
+ ${usedTypes.map(([name]) => ` ${name}: ${rename(name)};`).join("\n")}
512
515
  };`, `export const types = [${usedTypes.map(([name]) => `"${name}"`).join(", ")}] as const;`);
513
516
  }
514
517
  const usedReferences = Object.values(references).filter((r) => r[1]).map((r) => r[0]);
@@ -529,7 +532,7 @@ ${usedTypes.map(([name]) => ` ${name}: ${name};`).join("\n")}
529
532
  }).filter(filterOutputTypes));
530
533
  if (typesFile) {
531
534
  const operationsImports = (operations) => operations.flatMap((o) => {
532
- const prefix = operationDataName(o.name, o.definition.operation);
535
+ const prefix = operationDataName(o, o.definition.operation);
533
536
  return o.definition.variableDefinitions?.length
534
537
  ? [`${prefix}`, `${prefix}Variables`]
535
538
  : `${prefix}`;
@@ -538,8 +541,8 @@ ${usedTypes.map(([name]) => ` ${name}: ${name};`).join("\n")}
538
541
  ...operationsImports(operations.query),
539
542
  ...operationsImports(operations.mutation),
540
543
  ...operationsImports(operations.subscription),
541
- ...usedTypes.map((u) => u[0]),
542
- ...Array.from(handledInputs),
544
+ ...usedTypes.map((u) => rename(u[0])),
545
+ ...Array.from(handledInputs, (i) => rename(i)),
543
546
  ];
544
547
  serializedTypes.unshift(`import {\n ${imports.sort().join(",\n ")},
545
548
  } 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-data-generator",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
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;