@constructive-io/graphql-codegen 2.23.2 → 2.24.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.
Files changed (90) hide show
  1. package/README.md +147 -2
  2. package/cli/codegen/babel-ast.d.ts +46 -0
  3. package/cli/codegen/babel-ast.js +145 -0
  4. package/cli/codegen/barrel.d.ts +7 -2
  5. package/cli/codegen/barrel.js +159 -97
  6. package/cli/codegen/client.js +61 -0
  7. package/cli/codegen/custom-mutations.d.ts +2 -12
  8. package/cli/codegen/custom-mutations.js +116 -124
  9. package/cli/codegen/custom-queries.d.ts +2 -10
  10. package/cli/codegen/custom-queries.js +246 -335
  11. package/cli/codegen/index.d.ts +3 -0
  12. package/cli/codegen/index.js +72 -3
  13. package/cli/codegen/invalidation.d.ts +20 -0
  14. package/cli/codegen/invalidation.js +327 -0
  15. package/cli/codegen/mutation-keys.d.ts +24 -0
  16. package/cli/codegen/mutation-keys.js +247 -0
  17. package/cli/codegen/mutations.d.ts +3 -19
  18. package/cli/codegen/mutations.js +372 -383
  19. package/cli/codegen/orm/barrel.d.ts +1 -1
  20. package/cli/codegen/orm/barrel.js +42 -10
  21. package/cli/codegen/orm/client-generator.d.ts +1 -19
  22. package/cli/codegen/orm/client-generator.js +108 -77
  23. package/cli/codegen/orm/custom-ops-generator.d.ts +1 -12
  24. package/cli/codegen/orm/custom-ops-generator.js +192 -235
  25. package/cli/codegen/orm/input-types-generator.d.ts +13 -1
  26. package/cli/codegen/orm/input-types-generator.js +403 -147
  27. package/cli/codegen/orm/model-generator.d.ts +1 -19
  28. package/cli/codegen/orm/model-generator.js +229 -234
  29. package/cli/codegen/queries.d.ts +3 -11
  30. package/cli/codegen/queries.js +582 -389
  31. package/cli/codegen/query-keys.d.ts +15 -0
  32. package/cli/codegen/query-keys.js +477 -0
  33. package/cli/codegen/scalars.js +1 -0
  34. package/cli/codegen/schema-types-generator.d.ts +15 -10
  35. package/cli/codegen/schema-types-generator.js +87 -175
  36. package/cli/codegen/type-resolver.d.ts +1 -30
  37. package/cli/codegen/type-resolver.js +0 -53
  38. package/cli/codegen/types.d.ts +1 -1
  39. package/cli/codegen/types.js +76 -21
  40. package/cli/commands/generate.js +1 -0
  41. package/cli/index.js +1 -0
  42. package/esm/cli/codegen/babel-ast.d.ts +46 -0
  43. package/esm/cli/codegen/babel-ast.js +97 -0
  44. package/esm/cli/codegen/barrel.d.ts +7 -2
  45. package/esm/cli/codegen/barrel.js +126 -97
  46. package/esm/cli/codegen/client.js +61 -0
  47. package/esm/cli/codegen/custom-mutations.d.ts +2 -12
  48. package/esm/cli/codegen/custom-mutations.js +83 -124
  49. package/esm/cli/codegen/custom-queries.d.ts +2 -10
  50. package/esm/cli/codegen/custom-queries.js +214 -336
  51. package/esm/cli/codegen/index.d.ts +3 -0
  52. package/esm/cli/codegen/index.js +68 -2
  53. package/esm/cli/codegen/invalidation.d.ts +20 -0
  54. package/esm/cli/codegen/invalidation.js +291 -0
  55. package/esm/cli/codegen/mutation-keys.d.ts +24 -0
  56. package/esm/cli/codegen/mutation-keys.js +211 -0
  57. package/esm/cli/codegen/mutations.d.ts +3 -19
  58. package/esm/cli/codegen/mutations.js +340 -384
  59. package/esm/cli/codegen/orm/barrel.d.ts +1 -1
  60. package/esm/cli/codegen/orm/barrel.js +10 -11
  61. package/esm/cli/codegen/orm/client-generator.d.ts +1 -19
  62. package/esm/cli/codegen/orm/client-generator.js +76 -78
  63. package/esm/cli/codegen/orm/custom-ops-generator.d.ts +1 -12
  64. package/esm/cli/codegen/orm/custom-ops-generator.js +160 -236
  65. package/esm/cli/codegen/orm/input-types-generator.d.ts +13 -1
  66. package/esm/cli/codegen/orm/input-types-generator.js +371 -148
  67. package/esm/cli/codegen/orm/model-generator.d.ts +1 -19
  68. package/esm/cli/codegen/orm/model-generator.js +197 -235
  69. package/esm/cli/codegen/queries.d.ts +3 -11
  70. package/esm/cli/codegen/queries.js +550 -390
  71. package/esm/cli/codegen/query-keys.d.ts +15 -0
  72. package/esm/cli/codegen/query-keys.js +441 -0
  73. package/esm/cli/codegen/scalars.js +1 -0
  74. package/esm/cli/codegen/schema-types-generator.d.ts +15 -10
  75. package/esm/cli/codegen/schema-types-generator.js +54 -175
  76. package/esm/cli/codegen/type-resolver.d.ts +1 -30
  77. package/esm/cli/codegen/type-resolver.js +0 -49
  78. package/esm/cli/codegen/types.d.ts +1 -1
  79. package/esm/cli/codegen/types.js +44 -22
  80. package/esm/cli/commands/generate.js +1 -0
  81. package/esm/cli/index.js +1 -0
  82. package/esm/types/config.d.ts +75 -0
  83. package/esm/types/config.js +19 -1
  84. package/package.json +6 -4
  85. package/types/config.d.ts +75 -0
  86. package/types/config.js +20 -2
  87. package/cli/codegen/ts-ast.d.ts +0 -124
  88. package/cli/codegen/ts-ast.js +0 -280
  89. package/esm/cli/codegen/ts-ast.d.ts +0 -124
  90. package/esm/cli/codegen/ts-ast.js +0 -260
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Barrel file generators for ORM client
2
+ * Barrel file generators for ORM client (Babel AST-based)
3
3
  *
4
4
  * Generates index.ts files that re-export all models and operations.
5
5
  */
@@ -1,17 +1,48 @@
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 () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
3
36
  exports.generateModelsBarrel = generateModelsBarrel;
4
37
  exports.generateTypesBarrel = generateTypesBarrel;
5
- const ts_ast_1 = require("../ts-ast");
38
+ const t = __importStar(require("@babel/types"));
39
+ const babel_ast_1 = require("../babel-ast");
6
40
  const utils_1 = require("../utils");
7
41
  /**
8
42
  * Generate the models/index.ts barrel file
9
43
  */
10
44
  function generateModelsBarrel(tables) {
11
- const project = (0, ts_ast_1.createProject)();
12
- const sourceFile = (0, ts_ast_1.createSourceFile)(project, 'index.ts');
13
- // Add file header
14
- sourceFile.insertText(0, (0, ts_ast_1.createFileHeader)('Models barrel export') + '\n\n');
45
+ const statements = [];
15
46
  // Export all model classes (Select types are now in input-types.ts)
16
47
  for (const table of tables) {
17
48
  const { typeName } = (0, utils_1.getTableNames)(table);
@@ -19,14 +50,15 @@ function generateModelsBarrel(tables) {
19
50
  // Use same naming logic as model-generator to avoid "index.ts" clash with barrel file
20
51
  const baseFileName = (0, utils_1.lcFirst)(typeName);
21
52
  const moduleFileName = baseFileName === 'index' ? `${baseFileName}Model` : baseFileName;
22
- sourceFile.addExportDeclaration({
23
- moduleSpecifier: `./${moduleFileName}`,
24
- namedExports: [modelName],
25
- });
53
+ // Create: export { ModelName } from './moduleName';
54
+ const exportDecl = t.exportNamedDeclaration(null, [t.exportSpecifier(t.identifier(modelName), t.identifier(modelName))], t.stringLiteral(`./${moduleFileName}`));
55
+ statements.push(exportDecl);
26
56
  }
57
+ const header = (0, utils_1.getGeneratedFileHeader)('Models barrel export');
58
+ const code = (0, babel_ast_1.generateCode)(statements);
27
59
  return {
28
60
  fileName: 'models/index.ts',
29
- content: (0, ts_ast_1.getFormattedOutput)(sourceFile),
61
+ content: header + '\n' + code,
30
62
  };
31
63
  }
32
64
  /**
@@ -1,25 +1,7 @@
1
1
  /**
2
- * ORM Client generator
2
+ * ORM Client generator (Babel AST-based)
3
3
  *
4
4
  * Generates the createClient() factory function and main client file.
5
- *
6
- * Example output:
7
- * ```typescript
8
- * import { OrmClient, OrmClientConfig } from './client';
9
- * import { UserModel } from './models/user';
10
- * import { queryOperations } from './query';
11
- * import { mutationOperations } from './mutation';
12
- *
13
- * export function createClient(config: OrmClientConfig) {
14
- * const client = new OrmClient(config);
15
- * return {
16
- * user: new UserModel(client),
17
- * post: new PostModel(client),
18
- * query: queryOperations(client),
19
- * mutation: mutationOperations(client),
20
- * };
21
- * }
22
- * ```
23
5
  */
24
6
  import type { CleanTable } from '../../../types/schema';
25
7
  export interface GeneratedClientFile {
@@ -1,10 +1,44 @@
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 () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
3
36
  exports.generateOrmClientFile = generateOrmClientFile;
4
37
  exports.generateQueryBuilderFile = generateQueryBuilderFile;
5
38
  exports.generateSelectTypesFile = generateSelectTypesFile;
6
39
  exports.generateCreateClientFile = generateCreateClientFile;
7
- const ts_ast_1 = require("../ts-ast");
40
+ const t = __importStar(require("@babel/types"));
41
+ const babel_ast_1 = require("../babel-ast");
8
42
  const utils_1 = require("../utils");
9
43
  /**
10
44
  * Generate the main client.ts file (OrmClient class)
@@ -543,105 +577,102 @@ export type InferSelectResult<TEntity, TSelect> = TSelect extends undefined
543
577
  content,
544
578
  };
545
579
  }
580
+ function createImportDeclaration(moduleSpecifier, namedImports, typeOnly = false) {
581
+ const specifiers = namedImports.map((name) => t.importSpecifier(t.identifier(name), t.identifier(name)));
582
+ const decl = t.importDeclaration(specifiers, t.stringLiteral(moduleSpecifier));
583
+ decl.importKind = typeOnly ? 'type' : 'value';
584
+ return decl;
585
+ }
546
586
  /**
547
587
  * Generate the main index.ts with createClient factory
548
588
  */
549
589
  function generateCreateClientFile(tables, hasCustomQueries, hasCustomMutations) {
550
- const project = (0, ts_ast_1.createProject)();
551
- const sourceFile = (0, ts_ast_1.createSourceFile)(project, 'index.ts');
552
- // Add file header
553
- sourceFile.insertText(0, (0, ts_ast_1.createFileHeader)('ORM Client - createClient factory') + '\n\n');
590
+ const statements = [];
554
591
  // Add imports
555
- const imports = [
556
- (0, ts_ast_1.createImport)({
557
- moduleSpecifier: './client',
558
- namedImports: ['OrmClient'],
559
- typeOnlyNamedImports: ['OrmClientConfig'],
560
- }),
561
- ];
592
+ // Import OrmClient (value) and OrmClientConfig (type) separately
593
+ statements.push(createImportDeclaration('./client', ['OrmClient']));
594
+ statements.push(createImportDeclaration('./client', ['OrmClientConfig'], true));
562
595
  // Import models
563
596
  for (const table of tables) {
564
597
  const { typeName } = (0, utils_1.getTableNames)(table);
565
598
  const modelName = `${typeName}Model`;
566
599
  const fileName = (0, utils_1.lcFirst)(typeName);
567
- imports.push((0, ts_ast_1.createImport)({
568
- moduleSpecifier: `./models/${fileName}`,
569
- namedImports: [modelName],
570
- }));
600
+ statements.push(createImportDeclaration(`./models/${fileName}`, [modelName]));
571
601
  }
572
602
  // Import custom operations
573
603
  if (hasCustomQueries) {
574
- imports.push((0, ts_ast_1.createImport)({
575
- moduleSpecifier: './query',
576
- namedImports: ['createQueryOperations'],
577
- }));
604
+ statements.push(createImportDeclaration('./query', ['createQueryOperations']));
578
605
  }
579
606
  if (hasCustomMutations) {
580
- imports.push((0, ts_ast_1.createImport)({
581
- moduleSpecifier: './mutation',
582
- namedImports: ['createMutationOperations'],
583
- }));
607
+ statements.push(createImportDeclaration('./mutation', ['createMutationOperations']));
584
608
  }
585
- sourceFile.addImportDeclarations(imports);
586
609
  // Re-export types and classes
587
- sourceFile.addStatements('\n// Re-export types and classes');
588
- sourceFile.addStatements("export type { OrmClientConfig, QueryResult, GraphQLError } from './client';");
589
- sourceFile.addStatements("export { GraphQLRequestError } from './client';");
590
- sourceFile.addStatements("export { QueryBuilder } from './query-builder';");
591
- sourceFile.addStatements("export * from './select-types';");
592
- // Generate createClient function
593
- sourceFile.addStatements('\n// ============================================================================');
594
- sourceFile.addStatements('// Client Factory');
595
- sourceFile.addStatements('// ============================================================================\n');
596
- // Build the return object
597
- const modelEntries = tables.map((table) => {
610
+ // export type { OrmClientConfig, QueryResult, GraphQLError } from './client';
611
+ const typeExportDecl = t.exportNamedDeclaration(null, [
612
+ t.exportSpecifier(t.identifier('OrmClientConfig'), t.identifier('OrmClientConfig')),
613
+ t.exportSpecifier(t.identifier('QueryResult'), t.identifier('QueryResult')),
614
+ t.exportSpecifier(t.identifier('GraphQLError'), t.identifier('GraphQLError')),
615
+ ], t.stringLiteral('./client'));
616
+ typeExportDecl.exportKind = 'type';
617
+ statements.push(typeExportDecl);
618
+ // export { GraphQLRequestError } from './client';
619
+ statements.push(t.exportNamedDeclaration(null, [t.exportSpecifier(t.identifier('GraphQLRequestError'), t.identifier('GraphQLRequestError'))], t.stringLiteral('./client')));
620
+ // export { QueryBuilder } from './query-builder';
621
+ statements.push(t.exportNamedDeclaration(null, [t.exportSpecifier(t.identifier('QueryBuilder'), t.identifier('QueryBuilder'))], t.stringLiteral('./query-builder')));
622
+ // export * from './select-types';
623
+ statements.push(t.exportAllDeclaration(t.stringLiteral('./select-types')));
624
+ // Build the return object properties
625
+ const returnProperties = [];
626
+ for (const table of tables) {
598
627
  const { typeName, singularName } = (0, utils_1.getTableNames)(table);
599
- return `${singularName}: new ${typeName}Model(client)`;
600
- });
601
- let returnObject = modelEntries.join(',\n ');
628
+ const modelName = `${typeName}Model`;
629
+ returnProperties.push(t.objectProperty(t.identifier(singularName), t.newExpression(t.identifier(modelName), [t.identifier('client')])));
630
+ }
602
631
  if (hasCustomQueries) {
603
- returnObject += ',\n query: createQueryOperations(client)';
632
+ returnProperties.push(t.objectProperty(t.identifier('query'), t.callExpression(t.identifier('createQueryOperations'), [t.identifier('client')])));
604
633
  }
605
634
  if (hasCustomMutations) {
606
- returnObject += ',\n mutation: createMutationOperations(client)';
635
+ returnProperties.push(t.objectProperty(t.identifier('mutation'), t.callExpression(t.identifier('createMutationOperations'), [t.identifier('client')])));
607
636
  }
608
- sourceFile.addFunction({
609
- name: 'createClient',
610
- isExported: true,
611
- parameters: [{ name: 'config', type: 'OrmClientConfig' }],
612
- statements: `const client = new OrmClient(config);
613
-
614
- return {
615
- ${returnObject},
616
- };`,
617
- docs: [
618
- {
619
- description: `Create an ORM client instance
620
-
621
- @example
622
- \`\`\`typescript
623
- const db = createClient({
624
- endpoint: 'https://api.example.com/graphql',
625
- headers: { Authorization: 'Bearer token' },
626
- });
627
-
628
- // Query users
629
- const users = await db.user.findMany({
630
- select: { id: true, name: true },
631
- first: 10,
632
- }).execute();
633
-
634
- // Create a user
635
- const newUser = await db.user.create({
636
- data: { name: 'John', email: 'john@example.com' },
637
- select: { id: true },
638
- }).execute();
639
- \`\`\``,
640
- },
641
- ],
642
- });
637
+ // Build the createClient function body
638
+ const clientDecl = t.variableDeclaration('const', [
639
+ t.variableDeclarator(t.identifier('client'), t.newExpression(t.identifier('OrmClient'), [t.identifier('config')])),
640
+ ]);
641
+ const returnStmt = t.returnStatement(t.objectExpression(returnProperties));
642
+ const configParam = t.identifier('config');
643
+ configParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('OrmClientConfig')));
644
+ const createClientFunc = t.functionDeclaration(t.identifier('createClient'), [configParam], t.blockStatement([clientDecl, returnStmt]));
645
+ // Add JSDoc comment
646
+ const jsdocComment = (0, babel_ast_1.commentBlock)(`*
647
+ * Create an ORM client instance
648
+ *
649
+ * @example
650
+ * \`\`\`typescript
651
+ * const db = createClient({
652
+ * endpoint: 'https://api.example.com/graphql',
653
+ * headers: { Authorization: 'Bearer token' },
654
+ * });
655
+ *
656
+ * // Query users
657
+ * const users = await db.user.findMany({
658
+ * select: { id: true, name: true },
659
+ * first: 10,
660
+ * }).execute();
661
+ *
662
+ * // Create a user
663
+ * const newUser = await db.user.create({
664
+ * data: { name: 'John', email: 'john@example.com' },
665
+ * select: { id: true },
666
+ * }).execute();
667
+ * \`\`\`
668
+ `);
669
+ const exportedFunc = t.exportNamedDeclaration(createClientFunc);
670
+ exportedFunc.leadingComments = [jsdocComment];
671
+ statements.push(exportedFunc);
672
+ const header = (0, utils_1.getGeneratedFileHeader)('ORM Client - createClient factory');
673
+ const code = (0, babel_ast_1.generateCode)(statements);
643
674
  return {
644
675
  fileName: 'index.ts',
645
- content: (0, ts_ast_1.getFormattedOutput)(sourceFile),
676
+ content: header + '\n' + code,
646
677
  };
647
678
  }
@@ -1,19 +1,8 @@
1
1
  /**
2
- * Custom operations generator for ORM client
2
+ * Custom operations generator for ORM client (Babel AST-based)
3
3
  *
4
4
  * Generates db.query.* and db.mutation.* namespaces for non-table operations
5
5
  * like login, register, currentUser, etc.
6
- *
7
- * Example output:
8
- * ```typescript
9
- * // query/index.ts
10
- * export function createQueryOperations(client: OrmClient) {
11
- * return {
12
- * currentUser: (args?: { select?: CurrentUserSelect }) =>
13
- * new QueryBuilder({ ... }),
14
- * };
15
- * }
16
- * ```
17
6
  */
18
7
  import type { CleanOperation } from '../../../types/schema';
19
8
  export interface GeneratedCustomOpsFile {