@constructive-io/graphql-codegen 3.2.1 → 3.3.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.
- package/cli/index.js +36 -43
- package/cli/shared.d.ts +15 -19
- package/cli/shared.js +104 -25
- package/client/error.js +31 -9
- package/client/execute.js +2 -2
- package/client/index.d.ts +3 -3
- package/client/index.js +6 -6
- package/core/ast.d.ts +1 -1
- package/core/ast.js +1 -1
- package/core/codegen/babel-ast.d.ts +1 -1
- package/core/codegen/babel-ast.js +2 -2
- package/core/codegen/barrel.d.ts +0 -6
- package/core/codegen/barrel.js +22 -19
- package/core/codegen/client.d.ts +2 -12
- package/core/codegen/client.js +7 -21
- package/core/codegen/custom-mutations.d.ts +0 -14
- package/core/codegen/custom-mutations.js +139 -88
- package/core/codegen/custom-queries.d.ts +0 -14
- package/core/codegen/custom-queries.js +483 -193
- package/core/codegen/hooks-ast.d.ts +75 -0
- package/core/codegen/hooks-ast.js +522 -0
- package/core/codegen/index.d.ts +16 -18
- package/core/codegen/index.js +42 -88
- package/core/codegen/invalidation.d.ts +1 -7
- package/core/codegen/invalidation.js +50 -16
- package/core/codegen/mutation-keys.d.ts +1 -10
- package/core/codegen/mutation-keys.js +22 -8
- package/core/codegen/mutations.d.ts +0 -13
- package/core/codegen/mutations.js +301 -366
- package/core/codegen/orm/barrel.d.ts +0 -5
- package/core/codegen/orm/barrel.js +5 -0
- package/core/codegen/orm/client-generator.d.ts +0 -5
- package/core/codegen/orm/client-generator.js +7 -2
- package/core/codegen/orm/client.js +3 -1
- package/core/codegen/orm/custom-ops-generator.d.ts +0 -6
- package/core/codegen/orm/custom-ops-generator.js +104 -51
- package/core/codegen/orm/index.d.ts +4 -4
- package/core/codegen/orm/index.js +28 -15
- package/core/codegen/orm/input-types-generator.d.ts +1 -13
- package/core/codegen/orm/input-types-generator.js +85 -23
- package/core/codegen/orm/model-generator.d.ts +0 -5
- package/core/codegen/orm/model-generator.js +309 -131
- package/core/codegen/orm/select-types.d.ts +19 -14
- package/core/codegen/queries.d.ts +0 -8
- package/core/codegen/queries.js +360 -559
- package/core/codegen/query-keys.d.ts +1 -1
- package/core/codegen/query-keys.js +37 -23
- package/core/codegen/scalars.js +3 -1
- package/core/codegen/schema-types-generator.d.ts +1 -1
- package/core/codegen/schema-types-generator.js +17 -2
- package/core/codegen/select-helpers.d.ts +19 -0
- package/core/codegen/select-helpers.js +40 -0
- package/core/codegen/selection.d.ts +4 -0
- package/core/codegen/selection.js +65 -0
- package/core/codegen/shared/index.d.ts +2 -15
- package/core/codegen/shared/index.js +17 -4
- package/core/codegen/templates/hooks-client.ts +49 -0
- package/core/codegen/templates/hooks-selection.ts +58 -0
- package/core/codegen/templates/orm-client.ts +8 -6
- package/core/codegen/templates/query-builder.ts +250 -46
- package/core/codegen/templates/select-types.ts +31 -14
- package/core/codegen/type-resolver.d.ts +1 -5
- package/core/codegen/type-resolver.js +0 -22
- package/core/codegen/types.d.ts +0 -3
- package/core/codegen/types.js +71 -14
- package/core/codegen/utils.d.ts +1 -4
- package/core/codegen/utils.js +4 -1
- package/core/config/index.d.ts +1 -1
- package/core/config/resolver.js +1 -3
- package/core/generate.js +38 -50
- package/core/index.d.ts +3 -3
- package/core/index.js +3 -4
- package/core/introspect/index.d.ts +6 -6
- package/core/introspect/index.js +5 -8
- package/core/introspect/infer-tables.d.ts +0 -14
- package/core/introspect/infer-tables.js +15 -1
- package/core/introspect/source/database.js +1 -1
- package/core/introspect/source/endpoint.d.ts +0 -6
- package/core/introspect/source/endpoint.js +7 -1
- package/core/introspect/source/index.d.ts +4 -4
- package/core/introspect/source/index.js +5 -9
- package/core/introspect/source/pgpm-module.js +3 -3
- package/core/introspect/transform-schema.d.ts +2 -2
- package/core/introspect/transform-schema.js +2 -2
- package/core/output/index.d.ts +1 -1
- package/core/output/index.js +2 -2
- package/core/output/writer.d.ts +3 -0
- package/core/output/writer.js +20 -1
- package/core/pipeline/index.d.ts +2 -2
- package/core/query-builder.d.ts +2 -2
- package/core/query-builder.js +1 -1
- package/core/watch/index.d.ts +4 -4
- package/core/watch/index.js +9 -9
- package/core/watch/orchestrator.js +5 -3
- package/esm/cli/index.js +37 -44
- package/esm/cli/shared.d.ts +15 -19
- package/esm/cli/shared.js +94 -23
- package/esm/client/error.js +31 -9
- package/esm/client/execute.js +2 -2
- package/esm/client/index.d.ts +3 -3
- package/esm/client/index.js +3 -3
- package/esm/core/ast.d.ts +1 -1
- package/esm/core/ast.js +1 -1
- package/esm/core/codegen/babel-ast.d.ts +1 -1
- package/esm/core/codegen/babel-ast.js +2 -2
- package/esm/core/codegen/barrel.d.ts +0 -6
- package/esm/core/codegen/barrel.js +23 -20
- package/esm/core/codegen/client.d.ts +2 -12
- package/esm/core/codegen/client.js +7 -21
- package/esm/core/codegen/custom-mutations.d.ts +0 -14
- package/esm/core/codegen/custom-mutations.js +141 -90
- package/esm/core/codegen/custom-queries.d.ts +0 -14
- package/esm/core/codegen/custom-queries.js +486 -196
- package/esm/core/codegen/hooks-ast.d.ts +75 -0
- package/esm/core/codegen/hooks-ast.js +424 -0
- package/esm/core/codegen/index.d.ts +16 -18
- package/esm/core/codegen/index.js +26 -71
- package/esm/core/codegen/invalidation.d.ts +1 -7
- package/esm/core/codegen/invalidation.js +51 -17
- package/esm/core/codegen/mutation-keys.d.ts +1 -10
- package/esm/core/codegen/mutation-keys.js +23 -9
- package/esm/core/codegen/mutations.d.ts +0 -13
- package/esm/core/codegen/mutations.js +302 -367
- package/esm/core/codegen/orm/barrel.d.ts +0 -5
- package/esm/core/codegen/orm/barrel.js +6 -1
- package/esm/core/codegen/orm/client-generator.d.ts +0 -5
- package/esm/core/codegen/orm/client-generator.js +7 -2
- package/esm/core/codegen/orm/client.js +3 -1
- package/esm/core/codegen/orm/custom-ops-generator.d.ts +0 -6
- package/esm/core/codegen/orm/custom-ops-generator.js +103 -50
- package/esm/core/codegen/orm/index.d.ts +4 -4
- package/esm/core/codegen/orm/index.js +25 -12
- package/esm/core/codegen/orm/input-types-generator.d.ts +1 -13
- package/esm/core/codegen/orm/input-types-generator.js +85 -23
- package/esm/core/codegen/orm/model-generator.d.ts +0 -5
- package/esm/core/codegen/orm/model-generator.js +310 -132
- package/esm/core/codegen/orm/select-types.d.ts +19 -14
- package/esm/core/codegen/queries.d.ts +0 -8
- package/esm/core/codegen/queries.js +362 -561
- package/esm/core/codegen/query-keys.d.ts +1 -1
- package/esm/core/codegen/query-keys.js +38 -24
- package/esm/core/codegen/scalars.js +3 -1
- package/esm/core/codegen/schema-types-generator.d.ts +1 -1
- package/esm/core/codegen/schema-types-generator.js +17 -2
- package/esm/core/codegen/select-helpers.d.ts +19 -0
- package/esm/core/codegen/select-helpers.js +35 -0
- package/esm/core/codegen/selection.d.ts +4 -0
- package/esm/core/codegen/selection.js +29 -0
- package/esm/core/codegen/shared/index.d.ts +2 -15
- package/esm/core/codegen/shared/index.js +16 -3
- package/esm/core/codegen/type-resolver.d.ts +1 -5
- package/esm/core/codegen/type-resolver.js +1 -22
- package/esm/core/codegen/types.d.ts +0 -3
- package/esm/core/codegen/types.js +72 -15
- package/esm/core/codegen/utils.d.ts +1 -4
- package/esm/core/codegen/utils.js +4 -1
- package/esm/core/config/index.d.ts +1 -1
- package/esm/core/config/resolver.js +2 -4
- package/esm/core/generate.js +38 -50
- package/esm/core/index.d.ts +3 -3
- package/esm/core/index.js +2 -3
- package/esm/core/introspect/index.d.ts +6 -6
- package/esm/core/introspect/index.js +3 -6
- package/esm/core/introspect/infer-tables.d.ts +0 -14
- package/esm/core/introspect/infer-tables.js +16 -2
- package/esm/core/introspect/source/database.js +2 -2
- package/esm/core/introspect/source/endpoint.d.ts +0 -6
- package/esm/core/introspect/source/endpoint.js +7 -1
- package/esm/core/introspect/source/index.d.ts +4 -4
- package/esm/core/introspect/source/index.js +6 -10
- package/esm/core/introspect/source/pgpm-module.js +3 -3
- package/esm/core/introspect/transform-schema.d.ts +2 -2
- package/esm/core/introspect/transform-schema.js +2 -2
- package/esm/core/output/index.d.ts +1 -1
- package/esm/core/output/index.js +1 -1
- package/esm/core/output/writer.d.ts +3 -0
- package/esm/core/output/writer.js +20 -1
- package/esm/core/pipeline/index.d.ts +2 -2
- package/esm/core/pipeline/index.js +2 -2
- package/esm/core/query-builder.d.ts +2 -2
- package/esm/core/query-builder.js +2 -2
- package/esm/core/watch/index.d.ts +4 -4
- package/esm/core/watch/index.js +3 -3
- package/esm/core/watch/orchestrator.js +5 -3
- package/esm/generators/index.d.ts +3 -3
- package/esm/generators/index.js +3 -3
- package/esm/generators/mutations.d.ts +1 -1
- package/esm/generators/select.d.ts +1 -1
- package/esm/index.d.ts +3 -3
- package/esm/index.js +1 -4
- package/esm/types/config.d.ts +0 -10
- package/esm/types/config.js +0 -2
- package/esm/types/index.d.ts +6 -6
- package/esm/types/index.js +1 -1
- package/generators/index.d.ts +3 -3
- package/generators/index.js +8 -8
- package/generators/mutations.d.ts +1 -1
- package/generators/select.d.ts +1 -1
- package/index.d.ts +3 -3
- package/index.js +11 -6
- package/package.json +10 -10
- package/types/config.d.ts +0 -10
- package/types/config.js +0 -2
- package/types/index.d.ts +6 -6
- package/types/index.js +2 -2
- package/core/codegen/gql-ast.d.ts +0 -41
- package/core/codegen/gql-ast.js +0 -353
- package/core/codegen/schema-gql-ast.d.ts +0 -51
- package/core/codegen/schema-gql-ast.js +0 -385
- package/core/codegen/templates/client.browser.ts +0 -271
- package/core/codegen/templates/client.node.ts +0 -337
- package/esm/core/codegen/gql-ast.d.ts +0 -41
- package/esm/core/codegen/gql-ast.js +0 -312
- package/esm/core/codegen/schema-gql-ast.d.ts +0 -51
- package/esm/core/codegen/schema-gql-ast.js +0 -343
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { DEFAULT_QUERY_KEY_CONFIG } from '../../types/config';
|
|
2
|
+
import { generateCustomMutationsBarrel, generateCustomQueriesBarrel, generateMainBarrel, generateMutationsBarrel, generateQueriesBarrel, } from './barrel';
|
|
2
3
|
import { generateClientFile } from './client';
|
|
3
|
-
import { generateTypesFile } from './types';
|
|
4
|
-
import { generateSchemaTypesFile } from './schema-types-generator';
|
|
5
|
-
import { generateAllQueryHooks } from './queries';
|
|
6
|
-
import { generateAllMutationHooks } from './mutations';
|
|
7
|
-
import { generateAllCustomQueryHooks } from './custom-queries';
|
|
8
4
|
import { generateAllCustomMutationHooks } from './custom-mutations';
|
|
9
|
-
import {
|
|
10
|
-
import { generateMutationKeysFile } from './mutation-keys';
|
|
5
|
+
import { generateAllCustomQueryHooks } from './custom-queries';
|
|
11
6
|
import { generateInvalidationFile } from './invalidation';
|
|
12
|
-
import {
|
|
7
|
+
import { generateMutationKeysFile } from './mutation-keys';
|
|
8
|
+
import { generateAllMutationHooks } from './mutations';
|
|
9
|
+
import { generateAllQueryHooks } from './queries';
|
|
10
|
+
import { generateQueryKeysFile } from './query-keys';
|
|
11
|
+
import { generateSelectionFile } from './selection';
|
|
13
12
|
import { getTableNames } from './utils';
|
|
14
13
|
// ============================================================================
|
|
15
14
|
// Main orchestrator
|
|
@@ -27,68 +26,30 @@ export function generateAllFiles(tables, config) {
|
|
|
27
26
|
* (they're expected to exist in the shared types directory).
|
|
28
27
|
*/
|
|
29
28
|
export function generate(options) {
|
|
30
|
-
const { tables, customOperations, config
|
|
29
|
+
const { tables, customOperations, config } = options;
|
|
31
30
|
const files = [];
|
|
32
31
|
// Extract codegen options
|
|
33
|
-
const maxDepth = config.codegen.maxFieldDepth;
|
|
34
32
|
const skipQueryField = config.codegen.skipQueryField;
|
|
35
33
|
const reactQueryEnabled = config.reactQuery;
|
|
36
34
|
// Query key configuration (use defaults if not provided)
|
|
37
35
|
const queryKeyConfig = config.queryKeys ?? DEFAULT_QUERY_KEY_CONFIG;
|
|
38
36
|
const useCentralizedKeys = queryKeyConfig.generateScopedKeys;
|
|
39
37
|
const hasRelationships = Object.keys(queryKeyConfig.relationships).length > 0;
|
|
40
|
-
// 1. Generate client.ts
|
|
38
|
+
// 1. Generate client.ts (ORM client wrapper)
|
|
41
39
|
files.push({
|
|
42
40
|
path: 'client.ts',
|
|
43
|
-
content: generateClientFile(
|
|
44
|
-
|
|
45
|
-
|
|
41
|
+
content: generateClientFile(),
|
|
42
|
+
});
|
|
43
|
+
// 1b. Generate selection.ts (shared selection adapters for hooks)
|
|
44
|
+
files.push({
|
|
45
|
+
path: 'selection.ts',
|
|
46
|
+
content: generateSelectionFile(),
|
|
46
47
|
});
|
|
47
48
|
// Collect table type names for import path resolution
|
|
48
49
|
const tableTypeNames = new Set(tables.map((t) => getTableNames(t).typeName));
|
|
49
|
-
//
|
|
50
|
-
//
|
|
51
|
-
|
|
52
|
-
let generatedEnumNames = [];
|
|
53
|
-
if (sharedTypesPath) {
|
|
54
|
-
// Using shared types - check if schema-types would be generated
|
|
55
|
-
if (customOperations && customOperations.typeRegistry) {
|
|
56
|
-
const schemaTypesResult = generateSchemaTypesFile({
|
|
57
|
-
typeRegistry: customOperations.typeRegistry,
|
|
58
|
-
tableTypeNames,
|
|
59
|
-
});
|
|
60
|
-
if (schemaTypesResult.content.split('\n').length > 10) {
|
|
61
|
-
hasSchemaTypes = true;
|
|
62
|
-
generatedEnumNames = schemaTypesResult.generatedEnums || [];
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
// 2. Generate schema-types.ts for custom operations (if any)
|
|
68
|
-
// NOTE: This must come BEFORE types.ts so that types.ts can import enum types
|
|
69
|
-
if (customOperations && customOperations.typeRegistry) {
|
|
70
|
-
const schemaTypesResult = generateSchemaTypesFile({
|
|
71
|
-
typeRegistry: customOperations.typeRegistry,
|
|
72
|
-
tableTypeNames,
|
|
73
|
-
});
|
|
74
|
-
// Only include if there's meaningful content
|
|
75
|
-
if (schemaTypesResult.content.split('\n').length > 10) {
|
|
76
|
-
files.push({
|
|
77
|
-
path: 'schema-types.ts',
|
|
78
|
-
content: schemaTypesResult.content,
|
|
79
|
-
});
|
|
80
|
-
hasSchemaTypes = true;
|
|
81
|
-
generatedEnumNames = schemaTypesResult.generatedEnums || [];
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
// 3. Generate types.ts (can now import enums from schema-types)
|
|
85
|
-
files.push({
|
|
86
|
-
path: 'types.ts',
|
|
87
|
-
content: generateTypesFile(tables, {
|
|
88
|
-
enumsFromSchemaTypes: generatedEnumNames,
|
|
89
|
-
}),
|
|
90
|
-
});
|
|
91
|
-
}
|
|
50
|
+
// NOTE: types.ts and schema-types.ts are no longer generated here.
|
|
51
|
+
// Hooks now import types directly from the ORM's input-types.ts,
|
|
52
|
+
// which serves as the single source of truth for all types.
|
|
92
53
|
// 3b. Generate centralized query keys (query-keys.ts)
|
|
93
54
|
let hasQueryKeys = false;
|
|
94
55
|
if (useCentralizedKeys) {
|
|
@@ -148,7 +109,6 @@ export function generate(options) {
|
|
|
148
109
|
customQueryHooks = generateAllCustomQueryHooks({
|
|
149
110
|
operations: customOperations.queries,
|
|
150
111
|
typeRegistry: customOperations.typeRegistry,
|
|
151
|
-
maxDepth,
|
|
152
112
|
skipQueryField,
|
|
153
113
|
reactQueryEnabled,
|
|
154
114
|
tableTypeNames,
|
|
@@ -171,10 +131,7 @@ export function generate(options) {
|
|
|
171
131
|
// 6. Generate table-based mutation hooks (mutations/*.ts)
|
|
172
132
|
const mutationHooks = generateAllMutationHooks(tables, {
|
|
173
133
|
reactQueryEnabled,
|
|
174
|
-
enumsFromSchemaTypes: generatedEnumNames,
|
|
175
134
|
useCentralizedKeys,
|
|
176
|
-
hasRelationships,
|
|
177
|
-
tableTypeNames,
|
|
178
135
|
});
|
|
179
136
|
for (const hook of mutationHooks) {
|
|
180
137
|
files.push({
|
|
@@ -188,7 +145,6 @@ export function generate(options) {
|
|
|
188
145
|
customMutationHooks = generateAllCustomMutationHooks({
|
|
189
146
|
operations: customOperations.mutations,
|
|
190
147
|
typeRegistry: customOperations.typeRegistry,
|
|
191
|
-
maxDepth,
|
|
192
148
|
skipQueryField,
|
|
193
149
|
reactQueryEnabled,
|
|
194
150
|
tableTypeNames,
|
|
@@ -212,11 +168,11 @@ export function generate(options) {
|
|
|
212
168
|
: generateMutationsBarrel(tables),
|
|
213
169
|
});
|
|
214
170
|
}
|
|
215
|
-
// 9. Generate main index.ts barrel
|
|
171
|
+
// 9. Generate main index.ts barrel
|
|
172
|
+
// No longer includes types.ts or schema-types.ts - hooks import from ORM directly
|
|
216
173
|
files.push({
|
|
217
174
|
path: 'index.ts',
|
|
218
175
|
content: generateMainBarrel(tables, {
|
|
219
|
-
hasSchemaTypes,
|
|
220
176
|
hasMutations,
|
|
221
177
|
hasQueryKeys,
|
|
222
178
|
hasMutationKeys,
|
|
@@ -238,13 +194,12 @@ export function generate(options) {
|
|
|
238
194
|
// ============================================================================
|
|
239
195
|
// Re-exports for convenience
|
|
240
196
|
// ============================================================================
|
|
197
|
+
export { generateCustomMutationsBarrel, generateCustomQueriesBarrel, generateMainBarrel, generateMutationsBarrel, generateQueriesBarrel, } from './barrel';
|
|
241
198
|
export { generateClientFile } from './client';
|
|
242
|
-
export { generateTypesFile } from './types';
|
|
243
|
-
export { generateAllQueryHooks, generateListQueryHook, generateSingleQueryHook, } from './queries';
|
|
244
|
-
export { generateAllMutationHooks, generateCreateMutationHook, generateUpdateMutationHook, generateDeleteMutationHook, } from './mutations';
|
|
245
|
-
export { generateAllCustomQueryHooks, generateCustomQueryHook, } from './custom-queries';
|
|
246
199
|
export { generateAllCustomMutationHooks, generateCustomMutationHook, } from './custom-mutations';
|
|
247
|
-
export {
|
|
248
|
-
export { generateQueryKeysFile } from './query-keys';
|
|
249
|
-
export { generateMutationKeysFile } from './mutation-keys';
|
|
200
|
+
export { generateAllCustomQueryHooks, generateCustomQueryHook, } from './custom-queries';
|
|
250
201
|
export { generateInvalidationFile } from './invalidation';
|
|
202
|
+
export { generateMutationKeysFile } from './mutation-keys';
|
|
203
|
+
export { generateAllMutationHooks, generateCreateMutationHook, generateDeleteMutationHook, generateUpdateMutationHook, } from './mutations';
|
|
204
|
+
export { generateAllQueryHooks, generateListQueryHook, generateSingleQueryHook, } from './queries';
|
|
205
|
+
export { generateQueryKeysFile } from './query-keys';
|
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cache invalidation helpers generator
|
|
3
|
-
*
|
|
4
|
-
* Generates type-safe cache invalidation utilities with cascade support
|
|
5
|
-
* for parent-child entity relationships.
|
|
6
|
-
*/
|
|
7
|
-
import type { CleanTable } from '../../types/schema';
|
|
8
1
|
import type { QueryKeyConfig } from '../../types/config';
|
|
2
|
+
import type { CleanTable } from '../../types/schema';
|
|
9
3
|
export interface InvalidationGeneratorOptions {
|
|
10
4
|
tables: CleanTable[];
|
|
11
5
|
config: QueryKeyConfig;
|
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Cache invalidation helpers generator
|
|
3
|
+
*
|
|
4
|
+
* Generates type-safe cache invalidation utilities with cascade support
|
|
5
|
+
* for parent-child entity relationships.
|
|
6
|
+
*/
|
|
2
7
|
import * as t from '@babel/types';
|
|
3
|
-
import {
|
|
8
|
+
import { addJSDocComment, addLineComment, asConst, generateCode, typedParam, } from './babel-ast';
|
|
9
|
+
import { getGeneratedFileHeader, getTableNames, lcFirst, ucFirst, } from './utils';
|
|
4
10
|
/**
|
|
5
11
|
* Build a map of parent -> children for cascade invalidation
|
|
6
12
|
*/
|
|
@@ -47,7 +53,11 @@ function buildEntityInvalidateProperty(table, relationships, childrenMap, allTab
|
|
|
47
53
|
const queryClientTypeRef = () => t.tsTypeReference(t.identifier('QueryClient'));
|
|
48
54
|
const stringOrNumberType = () => t.tsUnionType([t.tsStringKeyword(), t.tsNumberKeyword()]);
|
|
49
55
|
// Helper to create queryClient.invalidateQueries({ queryKey: ... })
|
|
50
|
-
const invalidateCall = (queryKeyExpr) => t.callExpression(t.memberExpression(t.identifier('queryClient'), t.identifier('invalidateQueries')), [
|
|
56
|
+
const invalidateCall = (queryKeyExpr) => t.callExpression(t.memberExpression(t.identifier('queryClient'), t.identifier('invalidateQueries')), [
|
|
57
|
+
t.objectExpression([
|
|
58
|
+
t.objectProperty(t.identifier('queryKey'), queryKeyExpr),
|
|
59
|
+
]),
|
|
60
|
+
]);
|
|
51
61
|
// all property
|
|
52
62
|
const allArrowFn = t.arrowFunctionExpression([typedParam('queryClient', queryClientTypeRef())], invalidateCall(t.memberExpression(t.identifier(keysName), t.identifier('all'))));
|
|
53
63
|
const allProp = t.objectProperty(t.identifier('all'), allArrowFn);
|
|
@@ -72,11 +82,18 @@ function buildEntityInvalidateProperty(table, relationships, childrenMap, allTab
|
|
|
72
82
|
if (hasParent) {
|
|
73
83
|
const scopeTypeName = `${typeName}Scope`;
|
|
74
84
|
const scopeParam = typedParam('scope', t.tsTypeReference(t.identifier(scopeTypeName)), true);
|
|
75
|
-
const detailArrowFn = t.arrowFunctionExpression([
|
|
85
|
+
const detailArrowFn = t.arrowFunctionExpression([
|
|
86
|
+
typedParam('queryClient', queryClientTypeRef()),
|
|
87
|
+
typedParam('id', stringOrNumberType()),
|
|
88
|
+
scopeParam,
|
|
89
|
+
], invalidateCall(t.callExpression(t.memberExpression(t.identifier(keysName), t.identifier('detail')), [t.identifier('id'), t.identifier('scope')])));
|
|
76
90
|
detailProp = t.objectProperty(t.identifier('detail'), detailArrowFn);
|
|
77
91
|
}
|
|
78
92
|
else {
|
|
79
|
-
const detailArrowFn = t.arrowFunctionExpression([
|
|
93
|
+
const detailArrowFn = t.arrowFunctionExpression([
|
|
94
|
+
typedParam('queryClient', queryClientTypeRef()),
|
|
95
|
+
typedParam('id', stringOrNumberType()),
|
|
96
|
+
], invalidateCall(t.callExpression(t.memberExpression(t.identifier(keysName), t.identifier('detail')), [t.identifier('id')])));
|
|
80
97
|
detailProp = t.objectProperty(t.identifier('detail'), detailArrowFn);
|
|
81
98
|
}
|
|
82
99
|
addJSDocComment(detailProp, [`Invalidate a specific ${singularName}`]);
|
|
@@ -120,7 +137,10 @@ function buildEntityInvalidateProperty(table, relationships, childrenMap, allTab
|
|
|
120
137
|
}
|
|
121
138
|
}
|
|
122
139
|
}
|
|
123
|
-
const withChildrenArrowFn = t.arrowFunctionExpression([
|
|
140
|
+
const withChildrenArrowFn = t.arrowFunctionExpression([
|
|
141
|
+
typedParam('queryClient', queryClientTypeRef()),
|
|
142
|
+
typedParam('id', stringOrNumberType()),
|
|
143
|
+
], t.blockStatement(cascadeStatements));
|
|
124
144
|
const withChildrenProp = t.objectProperty(t.identifier('withChildren'), withChildrenArrowFn);
|
|
125
145
|
addJSDocComment(withChildrenProp, [
|
|
126
146
|
`Invalidate ${singularName} and all child entities`,
|
|
@@ -143,19 +163,30 @@ function buildEntityRemoveProperty(table, relationships) {
|
|
|
143
163
|
const queryClientTypeRef = () => t.tsTypeReference(t.identifier('QueryClient'));
|
|
144
164
|
const stringOrNumberType = () => t.tsUnionType([t.tsStringKeyword(), t.tsNumberKeyword()]);
|
|
145
165
|
// Helper to create queryClient.removeQueries({ queryKey: ... })
|
|
146
|
-
const removeCall = (queryKeyExpr) => t.callExpression(t.memberExpression(t.identifier('queryClient'), t.identifier('removeQueries')), [
|
|
166
|
+
const removeCall = (queryKeyExpr) => t.callExpression(t.memberExpression(t.identifier('queryClient'), t.identifier('removeQueries')), [
|
|
167
|
+
t.objectExpression([
|
|
168
|
+
t.objectProperty(t.identifier('queryKey'), queryKeyExpr),
|
|
169
|
+
]),
|
|
170
|
+
]);
|
|
147
171
|
let removeProp;
|
|
148
172
|
if (relationship) {
|
|
149
173
|
const scopeTypeName = `${typeName}Scope`;
|
|
150
174
|
const scopeParam = typedParam('scope', t.tsTypeReference(t.identifier(scopeTypeName)), true);
|
|
151
|
-
const removeArrowFn = t.arrowFunctionExpression([
|
|
152
|
-
|
|
175
|
+
const removeArrowFn = t.arrowFunctionExpression([
|
|
176
|
+
typedParam('queryClient', queryClientTypeRef()),
|
|
177
|
+
typedParam('id', stringOrNumberType()),
|
|
178
|
+
scopeParam,
|
|
179
|
+
], t.blockStatement([
|
|
180
|
+
t.expressionStatement(removeCall(t.callExpression(t.memberExpression(t.identifier(keysName), t.identifier('detail')), [t.identifier('id'), t.identifier('scope')]))),
|
|
153
181
|
]));
|
|
154
182
|
removeProp = t.objectProperty(t.identifier(singularName), removeArrowFn);
|
|
155
183
|
}
|
|
156
184
|
else {
|
|
157
|
-
const removeArrowFn = t.arrowFunctionExpression([
|
|
158
|
-
|
|
185
|
+
const removeArrowFn = t.arrowFunctionExpression([
|
|
186
|
+
typedParam('queryClient', queryClientTypeRef()),
|
|
187
|
+
typedParam('id', stringOrNumberType()),
|
|
188
|
+
], t.blockStatement([
|
|
189
|
+
t.expressionStatement(removeCall(t.callExpression(t.memberExpression(t.identifier(keysName), t.identifier('detail')), [t.identifier('id')]))),
|
|
159
190
|
]));
|
|
160
191
|
removeProp = t.objectProperty(t.identifier(singularName), removeArrowFn);
|
|
161
192
|
}
|
|
@@ -171,7 +202,9 @@ export function generateInvalidationFile(options) {
|
|
|
171
202
|
const childrenMap = buildChildrenMap(relationships);
|
|
172
203
|
const statements = [];
|
|
173
204
|
// Import QueryClient type
|
|
174
|
-
const queryClientImport = t.importDeclaration([
|
|
205
|
+
const queryClientImport = t.importDeclaration([
|
|
206
|
+
t.importSpecifier(t.identifier('QueryClient'), t.identifier('QueryClient')),
|
|
207
|
+
], t.stringLiteral('@tanstack/react-query'));
|
|
175
208
|
queryClientImport.importKind = 'type';
|
|
176
209
|
statements.push(queryClientImport);
|
|
177
210
|
// Import query keys
|
|
@@ -180,7 +213,7 @@ export function generateInvalidationFile(options) {
|
|
|
180
213
|
const { typeName } = getTableNames(table);
|
|
181
214
|
keyImports.push(`${lcFirst(typeName)}Keys`);
|
|
182
215
|
}
|
|
183
|
-
statements.push(t.importDeclaration(keyImports.map(name => t.importSpecifier(t.identifier(name), t.identifier(name))), t.stringLiteral('./query-keys')));
|
|
216
|
+
statements.push(t.importDeclaration(keyImports.map((name) => t.importSpecifier(t.identifier(name), t.identifier(name))), t.stringLiteral('./query-keys')));
|
|
184
217
|
// Import scope types if needed
|
|
185
218
|
const scopeTypes = [];
|
|
186
219
|
for (const table of tables) {
|
|
@@ -190,7 +223,7 @@ export function generateInvalidationFile(options) {
|
|
|
190
223
|
}
|
|
191
224
|
}
|
|
192
225
|
if (scopeTypes.length > 0) {
|
|
193
|
-
const scopeImport = t.importDeclaration(scopeTypes.map(name => t.importSpecifier(t.identifier(name), t.identifier(name))), t.stringLiteral('./query-keys'));
|
|
226
|
+
const scopeImport = t.importDeclaration(scopeTypes.map((name) => t.importSpecifier(t.identifier(name), t.identifier(name))), t.stringLiteral('./query-keys'));
|
|
194
227
|
scopeImport.importKind = 'type';
|
|
195
228
|
statements.push(scopeImport);
|
|
196
229
|
}
|
|
@@ -200,7 +233,7 @@ export function generateInvalidationFile(options) {
|
|
|
200
233
|
invalidateProperties.push(buildEntityInvalidateProperty(table, relationships, childrenMap, tables));
|
|
201
234
|
}
|
|
202
235
|
const invalidateDecl = t.exportNamedDeclaration(t.variableDeclaration('const', [
|
|
203
|
-
t.variableDeclarator(t.identifier('invalidate'), asConst(t.objectExpression(invalidateProperties)))
|
|
236
|
+
t.variableDeclarator(t.identifier('invalidate'), asConst(t.objectExpression(invalidateProperties))),
|
|
204
237
|
]));
|
|
205
238
|
// Build JSDoc for invalidate
|
|
206
239
|
const invalidateDocLines = [
|
|
@@ -231,7 +264,7 @@ export function generateInvalidationFile(options) {
|
|
|
231
264
|
removeProperties.push(buildEntityRemoveProperty(table, relationships));
|
|
232
265
|
}
|
|
233
266
|
const removeDecl = t.exportNamedDeclaration(t.variableDeclaration('const', [
|
|
234
|
-
t.variableDeclarator(t.identifier('remove'), asConst(t.objectExpression(removeProperties)))
|
|
267
|
+
t.variableDeclarator(t.identifier('remove'), asConst(t.objectExpression(removeProperties))),
|
|
235
268
|
]));
|
|
236
269
|
addJSDocComment(removeDecl, [
|
|
237
270
|
'Remove queries from cache (for delete operations)',
|
|
@@ -264,7 +297,8 @@ ${description}
|
|
|
264
297
|
for (let i = 0; i < codeLines.length; i++) {
|
|
265
298
|
const line = codeLines[i];
|
|
266
299
|
// Detect invalidation section (after imports)
|
|
267
|
-
if (!addedInvalidationSection &&
|
|
300
|
+
if (!addedInvalidationSection &&
|
|
301
|
+
line.includes('* Type-safe query invalidation helpers')) {
|
|
268
302
|
content += `// ============================================================================
|
|
269
303
|
// Invalidation Helpers
|
|
270
304
|
// ============================================================================
|
|
@@ -1,14 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Mutation key factory generator
|
|
3
|
-
*
|
|
4
|
-
* Generates centralized mutation keys for tracking in-flight mutations.
|
|
5
|
-
* Useful for:
|
|
6
|
-
* - Optimistic updates with rollback
|
|
7
|
-
* - Mutation deduplication
|
|
8
|
-
* - Tracking mutation state with useIsMutating
|
|
9
|
-
*/
|
|
10
|
-
import type { CleanTable, CleanOperation } from '../../types/schema';
|
|
11
1
|
import type { QueryKeyConfig } from '../../types/config';
|
|
2
|
+
import type { CleanOperation, CleanTable } from '../../types/schema';
|
|
12
3
|
export interface MutationKeyGeneratorOptions {
|
|
13
4
|
tables: CleanTable[];
|
|
14
5
|
customMutations: CleanOperation[];
|
|
@@ -1,6 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Mutation key factory generator
|
|
3
|
+
*
|
|
4
|
+
* Generates centralized mutation keys for tracking in-flight mutations.
|
|
5
|
+
* Useful for:
|
|
6
|
+
* - Optimistic updates with rollback
|
|
7
|
+
* - Mutation deduplication
|
|
8
|
+
* - Tracking mutation state with useIsMutating
|
|
9
|
+
*/
|
|
2
10
|
import * as t from '@babel/types';
|
|
3
|
-
import {
|
|
11
|
+
import { addJSDocComment, asConst, constArray, generateCode, typedParam, } from './babel-ast';
|
|
12
|
+
import { getGeneratedFileHeader, getTableNames, lcFirst } from './utils';
|
|
4
13
|
/**
|
|
5
14
|
* Generate mutation keys declaration for a single table entity
|
|
6
15
|
*/
|
|
@@ -25,7 +34,7 @@ function generateEntityMutationKeysDeclaration(table, relationships) {
|
|
|
25
34
|
t.stringLiteral(entityKey),
|
|
26
35
|
t.stringLiteral('create'),
|
|
27
36
|
t.objectExpression([
|
|
28
|
-
t.objectProperty(t.identifier(relationship.foreignKey), t.identifier(relationship.foreignKey), false, true)
|
|
37
|
+
t.objectProperty(t.identifier(relationship.foreignKey), t.identifier(relationship.foreignKey), false, true),
|
|
29
38
|
]),
|
|
30
39
|
]), constArray([
|
|
31
40
|
t.stringLiteral('mutation'),
|
|
@@ -45,7 +54,9 @@ function generateEntityMutationKeysDeclaration(table, relationships) {
|
|
|
45
54
|
addJSDocComment(createProp, [`Create ${singularName} mutation key`]);
|
|
46
55
|
properties.push(createProp);
|
|
47
56
|
// update property
|
|
48
|
-
const updateArrowFn = t.arrowFunctionExpression([
|
|
57
|
+
const updateArrowFn = t.arrowFunctionExpression([
|
|
58
|
+
typedParam('id', t.tsUnionType([t.tsStringKeyword(), t.tsNumberKeyword()])),
|
|
59
|
+
], constArray([
|
|
49
60
|
t.stringLiteral('mutation'),
|
|
50
61
|
t.stringLiteral(entityKey),
|
|
51
62
|
t.stringLiteral('update'),
|
|
@@ -55,7 +66,9 @@ function generateEntityMutationKeysDeclaration(table, relationships) {
|
|
|
55
66
|
addJSDocComment(updateProp, [`Update ${singularName} mutation key`]);
|
|
56
67
|
properties.push(updateProp);
|
|
57
68
|
// delete property
|
|
58
|
-
const deleteArrowFn = t.arrowFunctionExpression([
|
|
69
|
+
const deleteArrowFn = t.arrowFunctionExpression([
|
|
70
|
+
typedParam('id', t.tsUnionType([t.tsStringKeyword(), t.tsNumberKeyword()])),
|
|
71
|
+
], constArray([
|
|
59
72
|
t.stringLiteral('mutation'),
|
|
60
73
|
t.stringLiteral(entityKey),
|
|
61
74
|
t.stringLiteral('delete'),
|
|
@@ -65,7 +78,7 @@ function generateEntityMutationKeysDeclaration(table, relationships) {
|
|
|
65
78
|
addJSDocComment(deleteProp, [`Delete ${singularName} mutation key`]);
|
|
66
79
|
properties.push(deleteProp);
|
|
67
80
|
return t.exportNamedDeclaration(t.variableDeclaration('const', [
|
|
68
|
-
t.variableDeclarator(t.identifier(keysName), asConst(t.objectExpression(properties)))
|
|
81
|
+
t.variableDeclarator(t.identifier(keysName), asConst(t.objectExpression(properties))),
|
|
69
82
|
]));
|
|
70
83
|
}
|
|
71
84
|
/**
|
|
@@ -97,7 +110,7 @@ function generateCustomMutationKeysDeclaration(operations) {
|
|
|
97
110
|
properties.push(prop);
|
|
98
111
|
}
|
|
99
112
|
return t.exportNamedDeclaration(t.variableDeclaration('const', [
|
|
100
|
-
t.variableDeclarator(t.identifier('customMutationKeys'), asConst(t.objectExpression(properties)))
|
|
113
|
+
t.variableDeclarator(t.identifier('customMutationKeys'), asConst(t.objectExpression(properties))),
|
|
101
114
|
]));
|
|
102
115
|
}
|
|
103
116
|
/**
|
|
@@ -114,7 +127,7 @@ function generateUnifiedMutationStoreDeclaration(tables, hasCustomMutations) {
|
|
|
114
127
|
properties.push(t.objectProperty(t.identifier('custom'), t.identifier('customMutationKeys')));
|
|
115
128
|
}
|
|
116
129
|
const decl = t.exportNamedDeclaration(t.variableDeclaration('const', [
|
|
117
|
-
t.variableDeclarator(t.identifier('mutationKeys'), asConst(t.objectExpression(properties)))
|
|
130
|
+
t.variableDeclarator(t.identifier('mutationKeys'), asConst(t.objectExpression(properties))),
|
|
118
131
|
]));
|
|
119
132
|
addJSDocComment(decl, [
|
|
120
133
|
'Unified mutation key store',
|
|
@@ -183,7 +196,8 @@ ${description}
|
|
|
183
196
|
for (let i = 0; i < codeLines.length; i++) {
|
|
184
197
|
const line = codeLines[i];
|
|
185
198
|
// Detect custom mutation keys section
|
|
186
|
-
if (!addedCustomSection &&
|
|
199
|
+
if (!addedCustomSection &&
|
|
200
|
+
line.startsWith('export const customMutationKeys')) {
|
|
187
201
|
content += `
|
|
188
202
|
// ============================================================================
|
|
189
203
|
// Custom Mutation Keys
|
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Mutation hook generators using Babel AST-based code generation
|
|
3
|
-
*
|
|
4
|
-
* Output structure:
|
|
5
|
-
* mutations/
|
|
6
|
-
* useCreateCarMutation.ts
|
|
7
|
-
* useUpdateCarMutation.ts
|
|
8
|
-
* useDeleteCarMutation.ts
|
|
9
|
-
*/
|
|
10
1
|
import type { CleanTable } from '../../types/schema';
|
|
11
2
|
export interface GeneratedMutationFile {
|
|
12
3
|
fileName: string;
|
|
@@ -14,11 +5,7 @@ export interface GeneratedMutationFile {
|
|
|
14
5
|
}
|
|
15
6
|
export interface MutationGeneratorOptions {
|
|
16
7
|
reactQueryEnabled?: boolean;
|
|
17
|
-
enumsFromSchemaTypes?: string[];
|
|
18
8
|
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>;
|
|
22
9
|
}
|
|
23
10
|
export declare function generateCreateMutationHook(table: CleanTable, options?: MutationGeneratorOptions): GeneratedMutationFile | null;
|
|
24
11
|
export declare function generateUpdateMutationHook(table: CleanTable, options?: MutationGeneratorOptions): GeneratedMutationFile | null;
|