@constructive-io/graphql-codegen 2.23.3 → 2.24.1

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 (92) hide show
  1. package/README.md +147 -2
  2. package/cli/codegen/babel-ast.d.ts +53 -0
  3. package/cli/codegen/babel-ast.js +160 -0
  4. package/cli/codegen/barrel.d.ts +7 -2
  5. package/cli/codegen/barrel.js +193 -102
  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 +236 -335
  11. package/cli/codegen/gql-ast.js +22 -1
  12. package/cli/codegen/index.d.ts +3 -0
  13. package/cli/codegen/index.js +73 -3
  14. package/cli/codegen/invalidation.d.ts +20 -0
  15. package/cli/codegen/invalidation.js +327 -0
  16. package/cli/codegen/mutation-keys.d.ts +24 -0
  17. package/cli/codegen/mutation-keys.js +247 -0
  18. package/cli/codegen/mutations.d.ts +5 -19
  19. package/cli/codegen/mutations.js +385 -383
  20. package/cli/codegen/orm/barrel.d.ts +1 -1
  21. package/cli/codegen/orm/barrel.js +42 -10
  22. package/cli/codegen/orm/client-generator.d.ts +1 -19
  23. package/cli/codegen/orm/client-generator.js +108 -77
  24. package/cli/codegen/orm/custom-ops-generator.d.ts +1 -12
  25. package/cli/codegen/orm/custom-ops-generator.js +192 -235
  26. package/cli/codegen/orm/input-types-generator.d.ts +13 -1
  27. package/cli/codegen/orm/input-types-generator.js +425 -147
  28. package/cli/codegen/orm/model-generator.d.ts +1 -19
  29. package/cli/codegen/orm/model-generator.js +229 -234
  30. package/cli/codegen/queries.d.ts +4 -12
  31. package/cli/codegen/queries.js +660 -390
  32. package/cli/codegen/query-keys.d.ts +15 -0
  33. package/cli/codegen/query-keys.js +477 -0
  34. package/cli/codegen/scalars.js +1 -0
  35. package/cli/codegen/schema-types-generator.d.ts +15 -10
  36. package/cli/codegen/schema-types-generator.js +87 -175
  37. package/cli/codegen/type-resolver.d.ts +1 -30
  38. package/cli/codegen/type-resolver.js +0 -53
  39. package/cli/codegen/types.d.ts +1 -1
  40. package/cli/codegen/types.js +76 -21
  41. package/cli/codegen/utils.d.ts +6 -0
  42. package/cli/codegen/utils.js +19 -0
  43. package/esm/cli/codegen/babel-ast.d.ts +53 -0
  44. package/esm/cli/codegen/babel-ast.js +111 -0
  45. package/esm/cli/codegen/barrel.d.ts +7 -2
  46. package/esm/cli/codegen/barrel.js +161 -103
  47. package/esm/cli/codegen/client.js +61 -0
  48. package/esm/cli/codegen/custom-mutations.d.ts +2 -12
  49. package/esm/cli/codegen/custom-mutations.js +83 -124
  50. package/esm/cli/codegen/custom-queries.d.ts +2 -10
  51. package/esm/cli/codegen/custom-queries.js +204 -336
  52. package/esm/cli/codegen/gql-ast.js +23 -2
  53. package/esm/cli/codegen/index.d.ts +3 -0
  54. package/esm/cli/codegen/index.js +69 -2
  55. package/esm/cli/codegen/invalidation.d.ts +20 -0
  56. package/esm/cli/codegen/invalidation.js +291 -0
  57. package/esm/cli/codegen/mutation-keys.d.ts +24 -0
  58. package/esm/cli/codegen/mutation-keys.js +211 -0
  59. package/esm/cli/codegen/mutations.d.ts +5 -19
  60. package/esm/cli/codegen/mutations.js +353 -384
  61. package/esm/cli/codegen/orm/barrel.d.ts +1 -1
  62. package/esm/cli/codegen/orm/barrel.js +10 -11
  63. package/esm/cli/codegen/orm/client-generator.d.ts +1 -19
  64. package/esm/cli/codegen/orm/client-generator.js +76 -78
  65. package/esm/cli/codegen/orm/custom-ops-generator.d.ts +1 -12
  66. package/esm/cli/codegen/orm/custom-ops-generator.js +160 -236
  67. package/esm/cli/codegen/orm/input-types-generator.d.ts +13 -1
  68. package/esm/cli/codegen/orm/input-types-generator.js +393 -148
  69. package/esm/cli/codegen/orm/model-generator.d.ts +1 -19
  70. package/esm/cli/codegen/orm/model-generator.js +197 -235
  71. package/esm/cli/codegen/queries.d.ts +4 -12
  72. package/esm/cli/codegen/queries.js +628 -391
  73. package/esm/cli/codegen/query-keys.d.ts +15 -0
  74. package/esm/cli/codegen/query-keys.js +441 -0
  75. package/esm/cli/codegen/scalars.js +1 -0
  76. package/esm/cli/codegen/schema-types-generator.d.ts +15 -10
  77. package/esm/cli/codegen/schema-types-generator.js +54 -175
  78. package/esm/cli/codegen/type-resolver.d.ts +1 -30
  79. package/esm/cli/codegen/type-resolver.js +0 -49
  80. package/esm/cli/codegen/types.d.ts +1 -1
  81. package/esm/cli/codegen/types.js +44 -22
  82. package/esm/cli/codegen/utils.d.ts +6 -0
  83. package/esm/cli/codegen/utils.js +18 -0
  84. package/esm/types/config.d.ts +75 -0
  85. package/esm/types/config.js +18 -0
  86. package/package.json +6 -4
  87. package/types/config.d.ts +75 -0
  88. package/types/config.js +19 -1
  89. package/cli/codegen/ts-ast.d.ts +0 -124
  90. package/cli/codegen/ts-ast.js +0 -280
  91. package/esm/cli/codegen/ts-ast.d.ts +0 -124
  92. package/esm/cli/codegen/ts-ast.js +0 -260
@@ -0,0 +1,247 @@
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
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.generateMutationKeysFile = generateMutationKeysFile;
37
+ const utils_1 = require("./utils");
38
+ const t = __importStar(require("@babel/types"));
39
+ const babel_ast_1 = require("./babel-ast");
40
+ /**
41
+ * Generate mutation keys declaration for a single table entity
42
+ */
43
+ function generateEntityMutationKeysDeclaration(table, relationships) {
44
+ const { typeName, singularName } = (0, utils_1.getTableNames)(table);
45
+ const entityKey = typeName.toLowerCase();
46
+ const keysName = `${(0, utils_1.lcFirst)(typeName)}MutationKeys`;
47
+ const relationship = relationships[entityKey];
48
+ const properties = [];
49
+ // all property
50
+ const allProp = t.objectProperty(t.identifier('all'), (0, babel_ast_1.constArray)([t.stringLiteral('mutation'), t.stringLiteral(entityKey)]));
51
+ (0, babel_ast_1.addJSDocComment)(allProp, [`All ${singularName} mutation keys`]);
52
+ properties.push(allProp);
53
+ // create property
54
+ let createProp;
55
+ if (relationship) {
56
+ const fkParam = t.identifier(relationship.foreignKey);
57
+ fkParam.optional = true;
58
+ fkParam.typeAnnotation = t.tsTypeAnnotation(t.tsStringKeyword());
59
+ const arrowFn = t.arrowFunctionExpression([fkParam], t.conditionalExpression(t.identifier(relationship.foreignKey), (0, babel_ast_1.constArray)([
60
+ t.stringLiteral('mutation'),
61
+ t.stringLiteral(entityKey),
62
+ t.stringLiteral('create'),
63
+ t.objectExpression([
64
+ t.objectProperty(t.identifier(relationship.foreignKey), t.identifier(relationship.foreignKey), false, true)
65
+ ]),
66
+ ]), (0, babel_ast_1.constArray)([
67
+ t.stringLiteral('mutation'),
68
+ t.stringLiteral(entityKey),
69
+ t.stringLiteral('create'),
70
+ ])));
71
+ createProp = t.objectProperty(t.identifier('create'), arrowFn);
72
+ }
73
+ else {
74
+ const arrowFn = t.arrowFunctionExpression([], (0, babel_ast_1.constArray)([
75
+ t.stringLiteral('mutation'),
76
+ t.stringLiteral(entityKey),
77
+ t.stringLiteral('create'),
78
+ ]));
79
+ createProp = t.objectProperty(t.identifier('create'), arrowFn);
80
+ }
81
+ (0, babel_ast_1.addJSDocComment)(createProp, [`Create ${singularName} mutation key`]);
82
+ properties.push(createProp);
83
+ // update property
84
+ const updateArrowFn = t.arrowFunctionExpression([(0, babel_ast_1.typedParam)('id', t.tsUnionType([t.tsStringKeyword(), t.tsNumberKeyword()]))], (0, babel_ast_1.constArray)([
85
+ t.stringLiteral('mutation'),
86
+ t.stringLiteral(entityKey),
87
+ t.stringLiteral('update'),
88
+ t.identifier('id'),
89
+ ]));
90
+ const updateProp = t.objectProperty(t.identifier('update'), updateArrowFn);
91
+ (0, babel_ast_1.addJSDocComment)(updateProp, [`Update ${singularName} mutation key`]);
92
+ properties.push(updateProp);
93
+ // delete property
94
+ const deleteArrowFn = t.arrowFunctionExpression([(0, babel_ast_1.typedParam)('id', t.tsUnionType([t.tsStringKeyword(), t.tsNumberKeyword()]))], (0, babel_ast_1.constArray)([
95
+ t.stringLiteral('mutation'),
96
+ t.stringLiteral(entityKey),
97
+ t.stringLiteral('delete'),
98
+ t.identifier('id'),
99
+ ]));
100
+ const deleteProp = t.objectProperty(t.identifier('delete'), deleteArrowFn);
101
+ (0, babel_ast_1.addJSDocComment)(deleteProp, [`Delete ${singularName} mutation key`]);
102
+ properties.push(deleteProp);
103
+ return t.exportNamedDeclaration(t.variableDeclaration('const', [
104
+ t.variableDeclarator(t.identifier(keysName), (0, babel_ast_1.asConst)(t.objectExpression(properties)))
105
+ ]));
106
+ }
107
+ /**
108
+ * Generate custom mutation keys declaration
109
+ */
110
+ function generateCustomMutationKeysDeclaration(operations) {
111
+ if (operations.length === 0)
112
+ return null;
113
+ const properties = [];
114
+ for (const op of operations) {
115
+ const hasArgs = op.args.length > 0;
116
+ let prop;
117
+ if (hasArgs) {
118
+ const identifierParam = t.identifier('identifier');
119
+ identifierParam.optional = true;
120
+ identifierParam.typeAnnotation = t.tsTypeAnnotation(t.tsStringKeyword());
121
+ const arrowFn = t.arrowFunctionExpression([identifierParam], t.conditionalExpression(t.identifier('identifier'), (0, babel_ast_1.constArray)([
122
+ t.stringLiteral('mutation'),
123
+ t.stringLiteral(op.name),
124
+ t.identifier('identifier'),
125
+ ]), (0, babel_ast_1.constArray)([t.stringLiteral('mutation'), t.stringLiteral(op.name)])));
126
+ prop = t.objectProperty(t.identifier(op.name), arrowFn);
127
+ }
128
+ else {
129
+ const arrowFn = t.arrowFunctionExpression([], (0, babel_ast_1.constArray)([t.stringLiteral('mutation'), t.stringLiteral(op.name)]));
130
+ prop = t.objectProperty(t.identifier(op.name), arrowFn);
131
+ }
132
+ (0, babel_ast_1.addJSDocComment)(prop, [`Mutation key for ${op.name}`]);
133
+ properties.push(prop);
134
+ }
135
+ return t.exportNamedDeclaration(t.variableDeclaration('const', [
136
+ t.variableDeclarator(t.identifier('customMutationKeys'), (0, babel_ast_1.asConst)(t.objectExpression(properties)))
137
+ ]));
138
+ }
139
+ /**
140
+ * Generate the unified mutation keys store declaration
141
+ */
142
+ function generateUnifiedMutationStoreDeclaration(tables, hasCustomMutations) {
143
+ const properties = [];
144
+ for (const table of tables) {
145
+ const { typeName } = (0, utils_1.getTableNames)(table);
146
+ const keysName = `${(0, utils_1.lcFirst)(typeName)}MutationKeys`;
147
+ properties.push(t.objectProperty(t.identifier((0, utils_1.lcFirst)(typeName)), t.identifier(keysName)));
148
+ }
149
+ if (hasCustomMutations) {
150
+ properties.push(t.objectProperty(t.identifier('custom'), t.identifier('customMutationKeys')));
151
+ }
152
+ const decl = t.exportNamedDeclaration(t.variableDeclaration('const', [
153
+ t.variableDeclarator(t.identifier('mutationKeys'), (0, babel_ast_1.asConst)(t.objectExpression(properties)))
154
+ ]));
155
+ (0, babel_ast_1.addJSDocComment)(decl, [
156
+ 'Unified mutation key store',
157
+ '',
158
+ 'Use this for tracking in-flight mutations with useIsMutating.',
159
+ '',
160
+ '@example',
161
+ '```ts',
162
+ "import { useIsMutating } from '@tanstack/react-query';",
163
+ "import { mutationKeys } from './generated';",
164
+ '',
165
+ '// Check if any user mutations are in progress',
166
+ 'const isMutatingUser = useIsMutating({ mutationKey: mutationKeys.user.all });',
167
+ '',
168
+ '// Check if a specific user is being updated',
169
+ 'const isUpdating = useIsMutating({ mutationKey: mutationKeys.user.update(userId) });',
170
+ '```',
171
+ ]);
172
+ return decl;
173
+ }
174
+ /**
175
+ * Generate the complete mutation-keys.ts file
176
+ */
177
+ function generateMutationKeysFile(options) {
178
+ const { tables, customMutations, config } = options;
179
+ const { relationships } = config;
180
+ const statements = [];
181
+ // Generate entity mutation keys
182
+ for (const table of tables) {
183
+ statements.push(generateEntityMutationKeysDeclaration(table, relationships));
184
+ }
185
+ // Generate custom mutation keys
186
+ const mutationOperations = customMutations.filter((op) => op.kind === 'mutation');
187
+ const customKeysDecl = generateCustomMutationKeysDeclaration(mutationOperations);
188
+ if (customKeysDecl) {
189
+ statements.push(customKeysDecl);
190
+ }
191
+ // Generate unified store
192
+ statements.push(generateUnifiedMutationStoreDeclaration(tables, mutationOperations.length > 0));
193
+ // Generate code from AST
194
+ const code = (0, babel_ast_1.generateCode)(statements);
195
+ // Build final content with header and section comments
196
+ const header = (0, utils_1.getGeneratedFileHeader)('Centralized mutation key factory');
197
+ const description = `// ============================================================================
198
+ // Mutation keys for tracking in-flight mutations
199
+ //
200
+ // Benefits:
201
+ // - Track mutation state with useIsMutating
202
+ // - Implement optimistic updates with proper rollback
203
+ // - Deduplicate identical mutations
204
+ // - Coordinate related mutations
205
+ // ============================================================================`;
206
+ let content = `${header}
207
+
208
+ ${description}
209
+
210
+ // ============================================================================
211
+ // Entity Mutation Keys
212
+ // ============================================================================
213
+
214
+ `;
215
+ // Insert section comments into the generated code
216
+ const codeLines = code.split('\n');
217
+ let addedCustomSection = false;
218
+ let addedUnifiedSection = false;
219
+ for (let i = 0; i < codeLines.length; i++) {
220
+ const line = codeLines[i];
221
+ // Detect custom mutation keys section
222
+ if (!addedCustomSection && line.startsWith('export const customMutationKeys')) {
223
+ content += `
224
+ // ============================================================================
225
+ // Custom Mutation Keys
226
+ // ============================================================================
227
+
228
+ `;
229
+ addedCustomSection = true;
230
+ }
231
+ // Detect unified store section
232
+ if (!addedUnifiedSection && line.includes('* Unified mutation key store')) {
233
+ content += `
234
+ // ============================================================================
235
+ // Unified Mutation Key Store
236
+ // ============================================================================
237
+
238
+ `;
239
+ addedUnifiedSection = true;
240
+ }
241
+ content += line + '\n';
242
+ }
243
+ return {
244
+ fileName: 'mutation-keys.ts',
245
+ content,
246
+ };
247
+ }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Mutation hook generators using AST-based code generation
2
+ * Mutation hook generators using Babel AST-based code generation
3
3
  *
4
4
  * Output structure:
5
5
  * mutations/
@@ -13,28 +13,14 @@ export interface GeneratedMutationFile {
13
13
  content: string;
14
14
  }
15
15
  export interface MutationGeneratorOptions {
16
- /** Whether to generate React Query hooks (default: true for backwards compatibility) */
17
16
  reactQueryEnabled?: boolean;
18
- /** Enum type names that are available from schema-types.ts */
19
17
  enumsFromSchemaTypes?: string[];
18
+ useCentralizedKeys?: boolean;
19
+ hasRelationships?: boolean;
20
+ /** All table type names for determining which types to import from types.ts vs schema-types.ts */
21
+ tableTypeNames?: Set<string>;
20
22
  }
21
- /**
22
- * Generate create mutation hook file content using AST
23
- * When reactQueryEnabled is false, returns null since mutations require React Query
24
- */
25
23
  export declare function generateCreateMutationHook(table: CleanTable, options?: MutationGeneratorOptions): GeneratedMutationFile | null;
26
- /**
27
- * Generate update mutation hook file content using AST
28
- * When reactQueryEnabled is false, returns null since mutations require React Query
29
- */
30
24
  export declare function generateUpdateMutationHook(table: CleanTable, options?: MutationGeneratorOptions): GeneratedMutationFile | null;
31
- /**
32
- * Generate delete mutation hook file content using AST
33
- * When reactQueryEnabled is false, returns null since mutations require React Query
34
- */
35
25
  export declare function generateDeleteMutationHook(table: CleanTable, options?: MutationGeneratorOptions): GeneratedMutationFile | null;
36
- /**
37
- * Generate all mutation hook files for all tables
38
- * When reactQueryEnabled is false, returns empty array since mutations require React Query
39
- */
40
26
  export declare function generateAllMutationHooks(tables: CleanTable[], options?: MutationGeneratorOptions): GeneratedMutationFile[];