@constructive-io/graphql-codegen 2.19.0 → 2.20.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.
- package/README.md +1818 -113
- package/__tests__/codegen/input-types-generator.test.d.ts +1 -0
- package/__tests__/codegen/input-types-generator.test.js +635 -0
- package/cli/codegen/barrel.d.ts +27 -0
- package/cli/codegen/barrel.js +163 -0
- package/cli/codegen/client.d.ts +4 -0
- package/cli/codegen/client.js +170 -0
- package/cli/codegen/custom-mutations.d.ts +38 -0
- package/cli/codegen/custom-mutations.js +149 -0
- package/cli/codegen/custom-queries.d.ts +38 -0
- package/cli/codegen/custom-queries.js +358 -0
- package/cli/codegen/filters.d.ts +27 -0
- package/cli/codegen/filters.js +357 -0
- package/cli/codegen/gql-ast.d.ts +41 -0
- package/cli/codegen/gql-ast.js +329 -0
- package/cli/codegen/index.d.ts +71 -0
- package/cli/codegen/index.js +147 -0
- package/cli/codegen/mutations.d.ts +30 -0
- package/cli/codegen/mutations.js +410 -0
- package/cli/codegen/orm/barrel.d.ts +18 -0
- package/cli/codegen/orm/barrel.js +48 -0
- package/cli/codegen/orm/client-generator.d.ts +45 -0
- package/cli/codegen/orm/client-generator.js +646 -0
- package/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
- package/cli/codegen/orm/custom-ops-generator.js +350 -0
- package/cli/codegen/orm/index.d.ts +38 -0
- package/cli/codegen/orm/index.js +88 -0
- package/cli/codegen/orm/input-types-generator.d.ts +21 -0
- package/cli/codegen/orm/input-types-generator.js +705 -0
- package/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
- package/cli/codegen/orm/input-types-generator.test.js +75 -0
- package/cli/codegen/orm/model-generator.d.ts +32 -0
- package/cli/codegen/orm/model-generator.js +264 -0
- package/cli/codegen/orm/query-builder.d.ts +161 -0
- package/cli/codegen/orm/query-builder.js +366 -0
- package/cli/codegen/orm/select-types.d.ts +169 -0
- package/cli/codegen/orm/select-types.js +16 -0
- package/cli/codegen/orm/select-types.test.d.ts +11 -0
- package/cli/codegen/orm/select-types.test.js +22 -0
- package/cli/codegen/queries.d.ts +25 -0
- package/cli/codegen/queries.js +438 -0
- package/cli/codegen/scalars.d.ts +12 -0
- package/cli/codegen/scalars.js +71 -0
- package/cli/codegen/schema-gql-ast.d.ts +51 -0
- package/cli/codegen/schema-gql-ast.js +385 -0
- package/cli/codegen/ts-ast.d.ts +122 -0
- package/cli/codegen/ts-ast.js +280 -0
- package/cli/codegen/type-resolver.d.ts +96 -0
- package/cli/codegen/type-resolver.js +246 -0
- package/cli/codegen/types.d.ts +12 -0
- package/cli/codegen/types.js +69 -0
- package/cli/codegen/utils.d.ts +163 -0
- package/cli/codegen/utils.js +326 -0
- package/cli/commands/generate-orm.d.ts +37 -0
- package/cli/commands/generate-orm.js +195 -0
- package/cli/commands/generate.d.ts +39 -0
- package/cli/commands/generate.js +299 -0
- package/cli/commands/index.d.ts +7 -0
- package/cli/commands/index.js +12 -0
- package/cli/commands/init.d.ts +35 -0
- package/cli/commands/init.js +176 -0
- package/cli/index.d.ts +4 -0
- package/cli/index.js +291 -0
- package/cli/introspect/fetch-meta.d.ts +31 -0
- package/cli/introspect/fetch-meta.js +108 -0
- package/cli/introspect/fetch-schema.d.ts +21 -0
- package/cli/introspect/fetch-schema.js +86 -0
- package/cli/introspect/index.d.ts +8 -0
- package/cli/introspect/index.js +16 -0
- package/cli/introspect/meta-query.d.ts +111 -0
- package/cli/introspect/meta-query.js +191 -0
- package/cli/introspect/schema-query.d.ts +20 -0
- package/cli/introspect/schema-query.js +123 -0
- package/cli/introspect/transform-schema.d.ts +74 -0
- package/cli/introspect/transform-schema.js +269 -0
- package/cli/introspect/transform-schema.test.d.ts +1 -0
- package/cli/introspect/transform-schema.test.js +67 -0
- package/cli/introspect/transform.d.ts +21 -0
- package/cli/introspect/transform.js +216 -0
- package/cli/watch/cache.d.ts +45 -0
- package/cli/watch/cache.js +111 -0
- package/cli/watch/debounce.d.ts +19 -0
- package/cli/watch/debounce.js +89 -0
- package/cli/watch/hash.d.ts +17 -0
- package/cli/watch/hash.js +48 -0
- package/cli/watch/index.d.ts +10 -0
- package/cli/watch/index.js +22 -0
- package/cli/watch/orchestrator.d.ts +63 -0
- package/cli/watch/orchestrator.js +228 -0
- package/cli/watch/poller.d.ts +65 -0
- package/cli/watch/poller.js +203 -0
- package/cli/watch/types.d.ts +67 -0
- package/cli/watch/types.js +5 -0
- package/client/error.d.ts +95 -0
- package/client/error.js +255 -0
- package/client/execute.d.ts +57 -0
- package/client/execute.js +124 -0
- package/client/index.d.ts +6 -0
- package/client/index.js +18 -0
- package/client/typed-document.d.ts +31 -0
- package/client/typed-document.js +44 -0
- package/core/ast.d.ts +10 -0
- package/core/ast.js +593 -0
- package/core/custom-ast.d.ts +35 -0
- package/core/custom-ast.js +204 -0
- package/core/index.d.ts +8 -0
- package/core/index.js +33 -0
- package/core/meta-object/convert.d.ts +65 -0
- package/core/meta-object/convert.js +63 -0
- package/core/meta-object/format.json +93 -0
- package/core/meta-object/index.d.ts +2 -0
- package/core/meta-object/index.js +18 -0
- package/core/meta-object/validate.d.ts +9 -0
- package/core/meta-object/validate.js +34 -0
- package/core/query-builder.d.ts +46 -0
- package/core/query-builder.js +412 -0
- package/core/types.d.ts +139 -0
- package/core/types.js +28 -0
- package/esm/__tests__/codegen/input-types-generator.test.d.ts +1 -0
- package/esm/__tests__/codegen/input-types-generator.test.js +633 -0
- package/esm/cli/codegen/barrel.d.ts +27 -0
- package/esm/cli/codegen/barrel.js +156 -0
- package/esm/cli/codegen/client.d.ts +4 -0
- package/esm/cli/codegen/client.js +167 -0
- package/esm/cli/codegen/custom-mutations.d.ts +38 -0
- package/esm/cli/codegen/custom-mutations.js +145 -0
- package/esm/cli/codegen/custom-queries.d.ts +38 -0
- package/esm/cli/codegen/custom-queries.js +354 -0
- package/esm/cli/codegen/filters.d.ts +27 -0
- package/esm/cli/codegen/filters.js +351 -0
- package/esm/cli/codegen/gql-ast.d.ts +41 -0
- package/esm/cli/codegen/gql-ast.js +288 -0
- package/esm/cli/codegen/index.d.ts +71 -0
- package/esm/cli/codegen/index.js +124 -0
- package/esm/cli/codegen/mutations.d.ts +30 -0
- package/esm/cli/codegen/mutations.js +404 -0
- package/esm/cli/codegen/orm/barrel.d.ts +18 -0
- package/esm/cli/codegen/orm/barrel.js +44 -0
- package/esm/cli/codegen/orm/client-generator.d.ts +45 -0
- package/esm/cli/codegen/orm/client-generator.js +640 -0
- package/esm/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
- package/esm/cli/codegen/orm/custom-ops-generator.js +346 -0
- package/esm/cli/codegen/orm/index.d.ts +38 -0
- package/esm/cli/codegen/orm/index.js +75 -0
- package/esm/cli/codegen/orm/input-types-generator.d.ts +21 -0
- package/esm/cli/codegen/orm/input-types-generator.js +700 -0
- package/esm/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
- package/esm/cli/codegen/orm/input-types-generator.test.js +73 -0
- package/esm/cli/codegen/orm/model-generator.d.ts +32 -0
- package/esm/cli/codegen/orm/model-generator.js +260 -0
- package/esm/cli/codegen/orm/query-builder.d.ts +161 -0
- package/esm/cli/codegen/orm/query-builder.js +353 -0
- package/esm/cli/codegen/orm/select-types.d.ts +169 -0
- package/esm/cli/codegen/orm/select-types.js +15 -0
- package/esm/cli/codegen/orm/select-types.test.d.ts +11 -0
- package/esm/cli/codegen/orm/select-types.test.js +21 -0
- package/esm/cli/codegen/queries.d.ts +25 -0
- package/esm/cli/codegen/queries.js +433 -0
- package/esm/cli/codegen/scalars.d.ts +12 -0
- package/esm/cli/codegen/scalars.js +66 -0
- package/esm/cli/codegen/schema-gql-ast.d.ts +51 -0
- package/esm/cli/codegen/schema-gql-ast.js +343 -0
- package/esm/cli/codegen/ts-ast.d.ts +122 -0
- package/esm/cli/codegen/ts-ast.js +260 -0
- package/esm/cli/codegen/type-resolver.d.ts +96 -0
- package/esm/cli/codegen/type-resolver.js +224 -0
- package/esm/cli/codegen/types.d.ts +12 -0
- package/esm/cli/codegen/types.js +65 -0
- package/esm/cli/codegen/utils.d.ts +163 -0
- package/esm/cli/codegen/utils.js +288 -0
- package/esm/cli/commands/generate-orm.d.ts +37 -0
- package/esm/cli/commands/generate-orm.js +192 -0
- package/esm/cli/commands/generate.d.ts +39 -0
- package/esm/cli/commands/generate.js +262 -0
- package/esm/cli/commands/index.d.ts +7 -0
- package/esm/cli/commands/index.js +5 -0
- package/esm/cli/commands/init.d.ts +35 -0
- package/esm/cli/commands/init.js +138 -0
- package/esm/cli/index.d.ts +4 -0
- package/esm/cli/index.js +256 -0
- package/esm/cli/introspect/fetch-meta.d.ts +31 -0
- package/esm/cli/introspect/fetch-meta.js +104 -0
- package/esm/cli/introspect/fetch-schema.d.ts +21 -0
- package/esm/cli/introspect/fetch-schema.js +83 -0
- package/esm/cli/introspect/index.d.ts +8 -0
- package/esm/cli/introspect/index.js +6 -0
- package/esm/cli/introspect/meta-query.d.ts +111 -0
- package/esm/cli/introspect/meta-query.js +188 -0
- package/esm/cli/introspect/schema-query.d.ts +20 -0
- package/esm/cli/introspect/schema-query.js +120 -0
- package/esm/cli/introspect/transform-schema.d.ts +74 -0
- package/esm/cli/introspect/transform-schema.js +259 -0
- package/esm/cli/introspect/transform-schema.test.d.ts +1 -0
- package/esm/cli/introspect/transform-schema.test.js +65 -0
- package/esm/cli/introspect/transform.d.ts +21 -0
- package/esm/cli/introspect/transform.js +210 -0
- package/esm/cli/watch/cache.d.ts +45 -0
- package/esm/cli/watch/cache.js +73 -0
- package/esm/cli/watch/debounce.d.ts +19 -0
- package/esm/cli/watch/debounce.js +85 -0
- package/esm/cli/watch/hash.d.ts +17 -0
- package/esm/cli/watch/hash.js +43 -0
- package/esm/cli/watch/index.d.ts +10 -0
- package/esm/cli/watch/index.js +8 -0
- package/esm/cli/watch/orchestrator.d.ts +63 -0
- package/esm/cli/watch/orchestrator.js +223 -0
- package/esm/cli/watch/poller.d.ts +65 -0
- package/esm/cli/watch/poller.js +198 -0
- package/esm/cli/watch/types.d.ts +67 -0
- package/esm/cli/watch/types.js +4 -0
- package/esm/client/error.d.ts +95 -0
- package/esm/client/error.js +249 -0
- package/esm/client/execute.d.ts +57 -0
- package/esm/client/execute.js +120 -0
- package/esm/client/index.d.ts +6 -0
- package/esm/client/index.js +6 -0
- package/esm/client/typed-document.d.ts +31 -0
- package/esm/client/typed-document.js +40 -0
- package/esm/core/ast.d.ts +10 -0
- package/esm/core/ast.js +549 -0
- package/esm/core/custom-ast.d.ts +35 -0
- package/esm/core/custom-ast.js +161 -0
- package/esm/core/index.d.ts +8 -0
- package/esm/core/index.js +12 -0
- package/esm/core/meta-object/convert.d.ts +65 -0
- package/esm/core/meta-object/convert.js +60 -0
- package/esm/core/meta-object/format.json +93 -0
- package/esm/core/meta-object/index.d.ts +2 -0
- package/esm/core/meta-object/index.js +2 -0
- package/esm/core/meta-object/validate.d.ts +9 -0
- package/esm/core/meta-object/validate.js +28 -0
- package/esm/core/query-builder.d.ts +46 -0
- package/esm/core/query-builder.js +375 -0
- package/esm/core/types.d.ts +139 -0
- package/esm/core/types.js +24 -0
- package/esm/generators/field-selector.d.ts +30 -0
- package/esm/generators/field-selector.js +355 -0
- package/esm/generators/index.d.ts +6 -0
- package/esm/generators/index.js +9 -0
- package/esm/generators/mutations.d.ts +31 -0
- package/esm/generators/mutations.js +197 -0
- package/esm/generators/select.d.ts +50 -0
- package/esm/generators/select.js +636 -0
- package/esm/index.d.ts +12 -0
- package/esm/index.js +17 -3
- package/esm/react/index.d.ts +5 -0
- package/esm/react/index.js +6 -0
- package/esm/types/config.d.ts +199 -0
- package/esm/types/config.js +106 -0
- package/esm/types/index.d.ts +9 -0
- package/esm/types/index.js +4 -0
- package/esm/types/introspection.d.ts +121 -0
- package/esm/types/introspection.js +54 -0
- package/esm/types/mutation.d.ts +45 -0
- package/esm/types/mutation.js +4 -0
- package/esm/types/query.d.ts +82 -0
- package/esm/types/query.js +4 -0
- package/esm/types/schema.d.ts +253 -0
- package/esm/types/schema.js +5 -0
- package/esm/types/selection.d.ts +43 -0
- package/esm/types/selection.js +4 -0
- package/esm/utils/index.d.ts +4 -0
- package/esm/utils/index.js +4 -0
- package/generators/field-selector.d.ts +30 -0
- package/generators/field-selector.js +361 -0
- package/generators/index.d.ts +6 -0
- package/generators/index.js +27 -0
- package/generators/mutations.d.ts +31 -0
- package/generators/mutations.js +235 -0
- package/generators/select.d.ts +50 -0
- package/generators/select.js +679 -0
- package/index.d.ts +12 -3
- package/index.js +19 -3
- package/package.json +59 -38
- package/react/index.d.ts +5 -0
- package/react/index.js +9 -0
- package/types/config.d.ts +199 -0
- package/types/config.js +111 -0
- package/types/index.d.ts +9 -0
- package/types/index.js +10 -0
- package/types/introspection.d.ts +121 -0
- package/types/introspection.js +62 -0
- package/types/mutation.d.ts +45 -0
- package/types/mutation.js +5 -0
- package/types/query.d.ts +82 -0
- package/types/query.js +5 -0
- package/types/schema.d.ts +253 -0
- package/types/schema.js +6 -0
- package/types/selection.d.ts +43 -0
- package/types/selection.js +5 -0
- package/utils/index.d.ts +4 -0
- package/utils/index.js +7 -0
- package/codegen.d.ts +0 -13
- package/codegen.js +0 -293
- package/esm/codegen.js +0 -253
- package/esm/gql.js +0 -939
- package/esm/options.js +0 -27
- package/gql.d.ts +0 -188
- package/gql.js +0 -992
- package/options.d.ts +0 -45
- package/options.js +0 -31
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateCustomQueryOpsFile = generateCustomQueryOpsFile;
|
|
4
|
+
exports.generateCustomMutationOpsFile = generateCustomMutationOpsFile;
|
|
5
|
+
const ts_ast_1 = require("../ts-ast");
|
|
6
|
+
const utils_1 = require("../utils");
|
|
7
|
+
const type_resolver_1 = require("../type-resolver");
|
|
8
|
+
const scalars_1 = require("../scalars");
|
|
9
|
+
/**
|
|
10
|
+
* Collect all input type names used by operations
|
|
11
|
+
*/
|
|
12
|
+
function collectInputTypeNamesFromOps(operations) {
|
|
13
|
+
const inputTypes = new Set();
|
|
14
|
+
for (const op of operations) {
|
|
15
|
+
for (const arg of op.args) {
|
|
16
|
+
const baseName = (0, type_resolver_1.getTypeBaseName)(arg.type);
|
|
17
|
+
if (baseName && (baseName.endsWith('Input') || baseName.endsWith('Filter'))) {
|
|
18
|
+
inputTypes.add(baseName);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return Array.from(inputTypes);
|
|
23
|
+
}
|
|
24
|
+
// Types that don't need Select types
|
|
25
|
+
const NON_SELECT_TYPES = new Set([...scalars_1.SCALAR_NAMES, 'Query', 'Mutation']);
|
|
26
|
+
/**
|
|
27
|
+
* Collect all payload/return type names from operations (for Select types)
|
|
28
|
+
* Filters out scalar types
|
|
29
|
+
*/
|
|
30
|
+
function collectPayloadTypeNamesFromOps(operations) {
|
|
31
|
+
const payloadTypes = new Set();
|
|
32
|
+
for (const op of operations) {
|
|
33
|
+
const baseName = (0, type_resolver_1.getTypeBaseName)(op.returnType);
|
|
34
|
+
if (baseName &&
|
|
35
|
+
!baseName.endsWith('Connection') &&
|
|
36
|
+
baseName !== 'Query' &&
|
|
37
|
+
baseName !== 'Mutation' &&
|
|
38
|
+
!NON_SELECT_TYPES.has(baseName)) {
|
|
39
|
+
payloadTypes.add(baseName);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return Array.from(payloadTypes);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get the Select type name for a return type
|
|
46
|
+
* Returns null for scalar types, Connection types (no select needed)
|
|
47
|
+
*/
|
|
48
|
+
function getSelectTypeName(returnType) {
|
|
49
|
+
const baseName = (0, type_resolver_1.getTypeBaseName)(returnType);
|
|
50
|
+
if (baseName &&
|
|
51
|
+
!NON_SELECT_TYPES.has(baseName) &&
|
|
52
|
+
baseName !== 'Query' &&
|
|
53
|
+
baseName !== 'Mutation' &&
|
|
54
|
+
!baseName.endsWith('Connection')) {
|
|
55
|
+
return `${baseName}Select`;
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Generate the query/index.ts file for custom query operations
|
|
61
|
+
*/
|
|
62
|
+
function generateCustomQueryOpsFile(operations) {
|
|
63
|
+
const project = (0, ts_ast_1.createProject)();
|
|
64
|
+
const sourceFile = (0, ts_ast_1.createSourceFile)(project, 'index.ts');
|
|
65
|
+
// Collect all input type names and payload type names
|
|
66
|
+
const inputTypeNames = collectInputTypeNamesFromOps(operations);
|
|
67
|
+
const payloadTypeNames = collectPayloadTypeNamesFromOps(operations);
|
|
68
|
+
// Generate Select type names for payloads
|
|
69
|
+
const selectTypeNames = payloadTypeNames.map((p) => `${p}Select`);
|
|
70
|
+
// Combine all type imports
|
|
71
|
+
const allTypeImports = [...new Set([...inputTypeNames, ...payloadTypeNames, ...selectTypeNames])];
|
|
72
|
+
// Add file header
|
|
73
|
+
sourceFile.insertText(0, (0, ts_ast_1.createFileHeader)('Custom query operations') + '\n\n');
|
|
74
|
+
// Add imports
|
|
75
|
+
sourceFile.addImportDeclarations([
|
|
76
|
+
(0, ts_ast_1.createImport)({
|
|
77
|
+
moduleSpecifier: '../client',
|
|
78
|
+
namedImports: ['OrmClient'],
|
|
79
|
+
}),
|
|
80
|
+
(0, ts_ast_1.createImport)({
|
|
81
|
+
moduleSpecifier: '../query-builder',
|
|
82
|
+
namedImports: ['QueryBuilder', 'buildCustomDocument'],
|
|
83
|
+
}),
|
|
84
|
+
(0, ts_ast_1.createImport)({
|
|
85
|
+
moduleSpecifier: '../select-types',
|
|
86
|
+
typeOnlyNamedImports: ['InferSelectResult'],
|
|
87
|
+
}),
|
|
88
|
+
]);
|
|
89
|
+
// Import types from input-types if we have any
|
|
90
|
+
if (allTypeImports.length > 0) {
|
|
91
|
+
sourceFile.addImportDeclarations([
|
|
92
|
+
(0, ts_ast_1.createImport)({
|
|
93
|
+
moduleSpecifier: '../input-types',
|
|
94
|
+
typeOnlyNamedImports: allTypeImports,
|
|
95
|
+
}),
|
|
96
|
+
]);
|
|
97
|
+
}
|
|
98
|
+
// Generate variable definitions type for each operation
|
|
99
|
+
sourceFile.addStatements('\n// ============================================================================');
|
|
100
|
+
sourceFile.addStatements('// Variable Types');
|
|
101
|
+
sourceFile.addStatements('// ============================================================================\n');
|
|
102
|
+
for (const op of operations) {
|
|
103
|
+
if (op.args.length > 0) {
|
|
104
|
+
const varTypeName = `${(0, utils_1.ucFirst)(op.name)}Variables`;
|
|
105
|
+
const props = op.args.map((arg) => {
|
|
106
|
+
const optional = !(0, type_resolver_1.isTypeRequired)(arg.type);
|
|
107
|
+
return `${arg.name}${optional ? '?' : ''}: ${(0, type_resolver_1.typeRefToTsType)(arg.type)};`;
|
|
108
|
+
});
|
|
109
|
+
sourceFile.addStatements(`export interface ${varTypeName} {\n ${props.join('\n ')}\n}\n`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// Generate factory function
|
|
113
|
+
sourceFile.addStatements('\n// ============================================================================');
|
|
114
|
+
sourceFile.addStatements('// Query Operations Factory');
|
|
115
|
+
sourceFile.addStatements('// ============================================================================\n');
|
|
116
|
+
// Build the operations object
|
|
117
|
+
const operationMethods = operations.map((op) => {
|
|
118
|
+
const hasArgs = op.args.length > 0;
|
|
119
|
+
const varTypeName = `${(0, utils_1.ucFirst)(op.name)}Variables`;
|
|
120
|
+
const varDefs = op.args.map((arg) => ({
|
|
121
|
+
name: arg.name,
|
|
122
|
+
type: formatGraphQLType(arg.type),
|
|
123
|
+
}));
|
|
124
|
+
const varDefsJson = JSON.stringify(varDefs);
|
|
125
|
+
// Get Select type for return type
|
|
126
|
+
const selectTypeName = getSelectTypeName(op.returnType);
|
|
127
|
+
const payloadTypeName = (0, type_resolver_1.getTypeBaseName)(op.returnType);
|
|
128
|
+
// Use typed select if available, otherwise fall back to Record<string, unknown>
|
|
129
|
+
const selectType = selectTypeName ?? 'Record<string, unknown>';
|
|
130
|
+
const returnTypePart = selectTypeName && payloadTypeName
|
|
131
|
+
? `{ ${op.name}: InferSelectResult<${payloadTypeName}, S> }`
|
|
132
|
+
: 'unknown';
|
|
133
|
+
if (hasArgs) {
|
|
134
|
+
if (selectTypeName) {
|
|
135
|
+
return `${op.name}: <const S extends ${selectType}>(args: ${varTypeName}, options?: { select?: S }) =>
|
|
136
|
+
new QueryBuilder<${returnTypePart}>({
|
|
137
|
+
client,
|
|
138
|
+
operation: 'query',
|
|
139
|
+
operationName: '${(0, utils_1.ucFirst)(op.name)}',
|
|
140
|
+
fieldName: '${op.name}',
|
|
141
|
+
...buildCustomDocument('query', '${(0, utils_1.ucFirst)(op.name)}', '${op.name}', options?.select, args, ${varDefsJson}),
|
|
142
|
+
})`;
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
return `${op.name}: (args: ${varTypeName}, options?: { select?: Record<string, unknown> }) =>
|
|
146
|
+
new QueryBuilder({
|
|
147
|
+
client,
|
|
148
|
+
operation: 'query',
|
|
149
|
+
operationName: '${(0, utils_1.ucFirst)(op.name)}',
|
|
150
|
+
fieldName: '${op.name}',
|
|
151
|
+
...buildCustomDocument('query', '${(0, utils_1.ucFirst)(op.name)}', '${op.name}', options?.select, args, ${varDefsJson}),
|
|
152
|
+
})`;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
// No args - still provide typed select
|
|
157
|
+
if (selectTypeName) {
|
|
158
|
+
return `${op.name}: <const S extends ${selectType}>(options?: { select?: S }) =>
|
|
159
|
+
new QueryBuilder<${returnTypePart}>({
|
|
160
|
+
client,
|
|
161
|
+
operation: 'query',
|
|
162
|
+
operationName: '${(0, utils_1.ucFirst)(op.name)}',
|
|
163
|
+
fieldName: '${op.name}',
|
|
164
|
+
...buildCustomDocument('query', '${(0, utils_1.ucFirst)(op.name)}', '${op.name}', options?.select, undefined, []),
|
|
165
|
+
})`;
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
return `${op.name}: (options?: { select?: Record<string, unknown> }) =>
|
|
169
|
+
new QueryBuilder({
|
|
170
|
+
client,
|
|
171
|
+
operation: 'query',
|
|
172
|
+
operationName: '${(0, utils_1.ucFirst)(op.name)}',
|
|
173
|
+
fieldName: '${op.name}',
|
|
174
|
+
...buildCustomDocument('query', '${(0, utils_1.ucFirst)(op.name)}', '${op.name}', options?.select, undefined, []),
|
|
175
|
+
})`;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
sourceFile.addFunction({
|
|
180
|
+
name: 'createQueryOperations',
|
|
181
|
+
isExported: true,
|
|
182
|
+
parameters: [{ name: 'client', type: 'OrmClient' }],
|
|
183
|
+
statements: `return {
|
|
184
|
+
${operationMethods.join(',\n ')},
|
|
185
|
+
};`,
|
|
186
|
+
});
|
|
187
|
+
return {
|
|
188
|
+
fileName: 'query/index.ts',
|
|
189
|
+
content: (0, ts_ast_1.getFormattedOutput)(sourceFile),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Generate the mutation/index.ts file for custom mutation operations
|
|
194
|
+
*/
|
|
195
|
+
function generateCustomMutationOpsFile(operations) {
|
|
196
|
+
const project = (0, ts_ast_1.createProject)();
|
|
197
|
+
const sourceFile = (0, ts_ast_1.createSourceFile)(project, 'index.ts');
|
|
198
|
+
// Collect all input type names and payload type names
|
|
199
|
+
const inputTypeNames = collectInputTypeNamesFromOps(operations);
|
|
200
|
+
const payloadTypeNames = collectPayloadTypeNamesFromOps(operations);
|
|
201
|
+
// Generate Select type names for payloads
|
|
202
|
+
const selectTypeNames = payloadTypeNames.map((p) => `${p}Select`);
|
|
203
|
+
// Combine all type imports
|
|
204
|
+
const allTypeImports = [...new Set([...inputTypeNames, ...payloadTypeNames, ...selectTypeNames])];
|
|
205
|
+
// Add file header
|
|
206
|
+
sourceFile.insertText(0, (0, ts_ast_1.createFileHeader)('Custom mutation operations') + '\n\n');
|
|
207
|
+
// Add imports
|
|
208
|
+
sourceFile.addImportDeclarations([
|
|
209
|
+
(0, ts_ast_1.createImport)({
|
|
210
|
+
moduleSpecifier: '../client',
|
|
211
|
+
namedImports: ['OrmClient'],
|
|
212
|
+
}),
|
|
213
|
+
(0, ts_ast_1.createImport)({
|
|
214
|
+
moduleSpecifier: '../query-builder',
|
|
215
|
+
namedImports: ['QueryBuilder', 'buildCustomDocument'],
|
|
216
|
+
}),
|
|
217
|
+
(0, ts_ast_1.createImport)({
|
|
218
|
+
moduleSpecifier: '../select-types',
|
|
219
|
+
typeOnlyNamedImports: ['InferSelectResult'],
|
|
220
|
+
}),
|
|
221
|
+
]);
|
|
222
|
+
// Import types from input-types if we have any
|
|
223
|
+
if (allTypeImports.length > 0) {
|
|
224
|
+
sourceFile.addImportDeclarations([
|
|
225
|
+
(0, ts_ast_1.createImport)({
|
|
226
|
+
moduleSpecifier: '../input-types',
|
|
227
|
+
typeOnlyNamedImports: allTypeImports,
|
|
228
|
+
}),
|
|
229
|
+
]);
|
|
230
|
+
}
|
|
231
|
+
// Generate variable definitions type for each operation
|
|
232
|
+
sourceFile.addStatements('\n// ============================================================================');
|
|
233
|
+
sourceFile.addStatements('// Variable Types');
|
|
234
|
+
sourceFile.addStatements('// ============================================================================\n');
|
|
235
|
+
for (const op of operations) {
|
|
236
|
+
if (op.args.length > 0) {
|
|
237
|
+
const varTypeName = `${(0, utils_1.ucFirst)(op.name)}Variables`;
|
|
238
|
+
const props = op.args.map((arg) => {
|
|
239
|
+
const optional = !(0, type_resolver_1.isTypeRequired)(arg.type);
|
|
240
|
+
return `${arg.name}${optional ? '?' : ''}: ${(0, type_resolver_1.typeRefToTsType)(arg.type)};`;
|
|
241
|
+
});
|
|
242
|
+
sourceFile.addStatements(`export interface ${varTypeName} {\n ${props.join('\n ')}\n}\n`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
// Generate factory function
|
|
246
|
+
sourceFile.addStatements('\n// ============================================================================');
|
|
247
|
+
sourceFile.addStatements('// Mutation Operations Factory');
|
|
248
|
+
sourceFile.addStatements('// ============================================================================\n');
|
|
249
|
+
// Build the operations object
|
|
250
|
+
const operationMethods = operations.map((op) => {
|
|
251
|
+
const hasArgs = op.args.length > 0;
|
|
252
|
+
const varTypeName = `${(0, utils_1.ucFirst)(op.name)}Variables`;
|
|
253
|
+
const varDefs = op.args.map((arg) => ({
|
|
254
|
+
name: arg.name,
|
|
255
|
+
type: formatGraphQLType(arg.type),
|
|
256
|
+
}));
|
|
257
|
+
const varDefsJson = JSON.stringify(varDefs);
|
|
258
|
+
// Get Select type for return type
|
|
259
|
+
const selectTypeName = getSelectTypeName(op.returnType);
|
|
260
|
+
const payloadTypeName = (0, type_resolver_1.getTypeBaseName)(op.returnType);
|
|
261
|
+
// Use typed select if available, otherwise fall back to Record<string, unknown>
|
|
262
|
+
const selectType = selectTypeName ?? 'Record<string, unknown>';
|
|
263
|
+
const returnTypePart = selectTypeName && payloadTypeName
|
|
264
|
+
? `{ ${op.name}: InferSelectResult<${payloadTypeName}, S> }`
|
|
265
|
+
: 'unknown';
|
|
266
|
+
if (hasArgs) {
|
|
267
|
+
if (selectTypeName) {
|
|
268
|
+
return `${op.name}: <const S extends ${selectType}>(args: ${varTypeName}, options?: { select?: S }) =>
|
|
269
|
+
new QueryBuilder<${returnTypePart}>({
|
|
270
|
+
client,
|
|
271
|
+
operation: 'mutation',
|
|
272
|
+
operationName: '${(0, utils_1.ucFirst)(op.name)}',
|
|
273
|
+
fieldName: '${op.name}',
|
|
274
|
+
...buildCustomDocument('mutation', '${(0, utils_1.ucFirst)(op.name)}', '${op.name}', options?.select, args, ${varDefsJson}),
|
|
275
|
+
})`;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
return `${op.name}: (args: ${varTypeName}, options?: { select?: Record<string, unknown> }) =>
|
|
279
|
+
new QueryBuilder({
|
|
280
|
+
client,
|
|
281
|
+
operation: 'mutation',
|
|
282
|
+
operationName: '${(0, utils_1.ucFirst)(op.name)}',
|
|
283
|
+
fieldName: '${op.name}',
|
|
284
|
+
...buildCustomDocument('mutation', '${(0, utils_1.ucFirst)(op.name)}', '${op.name}', options?.select, args, ${varDefsJson}),
|
|
285
|
+
})`;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
// No args - still provide typed select
|
|
290
|
+
if (selectTypeName) {
|
|
291
|
+
return `${op.name}: <const S extends ${selectType}>(options?: { select?: S }) =>
|
|
292
|
+
new QueryBuilder<${returnTypePart}>({
|
|
293
|
+
client,
|
|
294
|
+
operation: 'mutation',
|
|
295
|
+
operationName: '${(0, utils_1.ucFirst)(op.name)}',
|
|
296
|
+
fieldName: '${op.name}',
|
|
297
|
+
...buildCustomDocument('mutation', '${(0, utils_1.ucFirst)(op.name)}', '${op.name}', options?.select, undefined, []),
|
|
298
|
+
})`;
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
return `${op.name}: (options?: { select?: Record<string, unknown> }) =>
|
|
302
|
+
new QueryBuilder({
|
|
303
|
+
client,
|
|
304
|
+
operation: 'mutation',
|
|
305
|
+
operationName: '${(0, utils_1.ucFirst)(op.name)}',
|
|
306
|
+
fieldName: '${op.name}',
|
|
307
|
+
...buildCustomDocument('mutation', '${(0, utils_1.ucFirst)(op.name)}', '${op.name}', options?.select, undefined, []),
|
|
308
|
+
})`;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
sourceFile.addFunction({
|
|
313
|
+
name: 'createMutationOperations',
|
|
314
|
+
isExported: true,
|
|
315
|
+
parameters: [{ name: 'client', type: 'OrmClient' }],
|
|
316
|
+
statements: `return {
|
|
317
|
+
${operationMethods.join(',\n ')},
|
|
318
|
+
};`,
|
|
319
|
+
});
|
|
320
|
+
return {
|
|
321
|
+
fileName: 'mutation/index.ts',
|
|
322
|
+
content: (0, ts_ast_1.getFormattedOutput)(sourceFile),
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Format a CleanTypeRef to GraphQL type string
|
|
327
|
+
*/
|
|
328
|
+
function formatGraphQLType(typeRef) {
|
|
329
|
+
let result = '';
|
|
330
|
+
if (typeRef.kind === 'NON_NULL') {
|
|
331
|
+
if (typeRef.ofType) {
|
|
332
|
+
result = formatGraphQLType(typeRef.ofType) + '!';
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
result = (typeRef.name ?? 'String') + '!';
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
else if (typeRef.kind === 'LIST') {
|
|
339
|
+
if (typeRef.ofType) {
|
|
340
|
+
result = `[${formatGraphQLType(typeRef.ofType)}]`;
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
result = '[String]';
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
result = typeRef.name ?? 'String';
|
|
348
|
+
}
|
|
349
|
+
return result;
|
|
350
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ORM Generator Orchestrator
|
|
3
|
+
*
|
|
4
|
+
* Main entry point for ORM code generation. Coordinates all generators
|
|
5
|
+
* and produces the complete ORM client output.
|
|
6
|
+
*/
|
|
7
|
+
import type { CleanTable, CleanOperation, TypeRegistry } from '../../../types/schema';
|
|
8
|
+
import type { ResolvedConfig } from '../../../types/config';
|
|
9
|
+
export interface GeneratedFile {
|
|
10
|
+
path: string;
|
|
11
|
+
content: string;
|
|
12
|
+
}
|
|
13
|
+
export interface GenerateOrmOptions {
|
|
14
|
+
tables: CleanTable[];
|
|
15
|
+
customOperations?: {
|
|
16
|
+
queries: CleanOperation[];
|
|
17
|
+
mutations: CleanOperation[];
|
|
18
|
+
typeRegistry?: TypeRegistry;
|
|
19
|
+
};
|
|
20
|
+
config: ResolvedConfig;
|
|
21
|
+
}
|
|
22
|
+
export interface GenerateOrmResult {
|
|
23
|
+
files: GeneratedFile[];
|
|
24
|
+
stats: {
|
|
25
|
+
tables: number;
|
|
26
|
+
customQueries: number;
|
|
27
|
+
customMutations: number;
|
|
28
|
+
totalFiles: number;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Generate all ORM client files
|
|
33
|
+
*/
|
|
34
|
+
export declare function generateOrm(options: GenerateOrmOptions): GenerateOrmResult;
|
|
35
|
+
export { generateOrmClientFile, generateQueryBuilderFile, generateSelectTypesFile } from './client-generator';
|
|
36
|
+
export { generateModelFile, generateAllModelFiles } from './model-generator';
|
|
37
|
+
export { generateCustomQueryOpsFile, generateCustomMutationOpsFile } from './custom-ops-generator';
|
|
38
|
+
export { generateModelsBarrel, generateTypesBarrel } from './barrel';
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateTypesBarrel = exports.generateModelsBarrel = exports.generateCustomMutationOpsFile = exports.generateCustomQueryOpsFile = exports.generateAllModelFiles = exports.generateModelFile = exports.generateSelectTypesFile = exports.generateQueryBuilderFile = exports.generateOrmClientFile = void 0;
|
|
4
|
+
exports.generateOrm = generateOrm;
|
|
5
|
+
const client_generator_1 = require("./client-generator");
|
|
6
|
+
const model_generator_1 = require("./model-generator");
|
|
7
|
+
const custom_ops_generator_1 = require("./custom-ops-generator");
|
|
8
|
+
const barrel_1 = require("./barrel");
|
|
9
|
+
const input_types_generator_1 = require("./input-types-generator");
|
|
10
|
+
/**
|
|
11
|
+
* Generate all ORM client files
|
|
12
|
+
*/
|
|
13
|
+
function generateOrm(options) {
|
|
14
|
+
const { tables, customOperations, config } = options;
|
|
15
|
+
const files = [];
|
|
16
|
+
const useSharedTypes = config.orm?.useSharedTypes ?? true;
|
|
17
|
+
const hasCustomQueries = (customOperations?.queries.length ?? 0) > 0;
|
|
18
|
+
const hasCustomMutations = (customOperations?.mutations.length ?? 0) > 0;
|
|
19
|
+
const typeRegistry = customOperations?.typeRegistry;
|
|
20
|
+
// 1. Generate runtime files (client, query-builder, select-types)
|
|
21
|
+
const clientFile = (0, client_generator_1.generateOrmClientFile)();
|
|
22
|
+
files.push({ path: clientFile.fileName, content: clientFile.content });
|
|
23
|
+
const queryBuilderFile = (0, client_generator_1.generateQueryBuilderFile)();
|
|
24
|
+
files.push({ path: queryBuilderFile.fileName, content: queryBuilderFile.content });
|
|
25
|
+
const selectTypesFile = (0, client_generator_1.generateSelectTypesFile)();
|
|
26
|
+
files.push({ path: selectTypesFile.fileName, content: selectTypesFile.content });
|
|
27
|
+
// 2. Generate model files
|
|
28
|
+
const modelFiles = (0, model_generator_1.generateAllModelFiles)(tables, useSharedTypes);
|
|
29
|
+
for (const modelFile of modelFiles) {
|
|
30
|
+
files.push({
|
|
31
|
+
path: `models/${modelFile.fileName}`,
|
|
32
|
+
content: modelFile.content,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
// 3. Generate models barrel
|
|
36
|
+
const modelsBarrel = (0, barrel_1.generateModelsBarrel)(tables);
|
|
37
|
+
files.push({ path: modelsBarrel.fileName, content: modelsBarrel.content });
|
|
38
|
+
// 4. Generate comprehensive input types (entities, filters, orderBy, CRUD inputs, custom inputs, payload types)
|
|
39
|
+
// Always generate if we have tables or custom operations
|
|
40
|
+
if (tables.length > 0 || (typeRegistry && (hasCustomQueries || hasCustomMutations))) {
|
|
41
|
+
const allOps = [
|
|
42
|
+
...(customOperations?.queries ?? []),
|
|
43
|
+
...(customOperations?.mutations ?? []),
|
|
44
|
+
];
|
|
45
|
+
const usedInputTypes = (0, input_types_generator_1.collectInputTypeNames)(allOps);
|
|
46
|
+
const usedPayloadTypes = (0, input_types_generator_1.collectPayloadTypeNames)(allOps);
|
|
47
|
+
const inputTypesFile = (0, input_types_generator_1.generateInputTypesFile)(typeRegistry ?? new Map(), usedInputTypes, tables, usedPayloadTypes);
|
|
48
|
+
files.push({ path: inputTypesFile.fileName, content: inputTypesFile.content });
|
|
49
|
+
}
|
|
50
|
+
// 5. Generate custom operations (if any)
|
|
51
|
+
if (hasCustomQueries && customOperations?.queries) {
|
|
52
|
+
const queryOpsFile = (0, custom_ops_generator_1.generateCustomQueryOpsFile)(customOperations.queries);
|
|
53
|
+
files.push({ path: queryOpsFile.fileName, content: queryOpsFile.content });
|
|
54
|
+
}
|
|
55
|
+
if (hasCustomMutations && customOperations?.mutations) {
|
|
56
|
+
const mutationOpsFile = (0, custom_ops_generator_1.generateCustomMutationOpsFile)(customOperations.mutations);
|
|
57
|
+
files.push({ path: mutationOpsFile.fileName, content: mutationOpsFile.content });
|
|
58
|
+
}
|
|
59
|
+
// 6. Generate types barrel
|
|
60
|
+
const typesBarrel = (0, barrel_1.generateTypesBarrel)(useSharedTypes);
|
|
61
|
+
files.push({ path: typesBarrel.fileName, content: typesBarrel.content });
|
|
62
|
+
// 7. Generate main index.ts with createClient
|
|
63
|
+
const indexFile = (0, client_generator_1.generateCreateClientFile)(tables, hasCustomQueries, hasCustomMutations);
|
|
64
|
+
files.push({ path: indexFile.fileName, content: indexFile.content });
|
|
65
|
+
return {
|
|
66
|
+
files,
|
|
67
|
+
stats: {
|
|
68
|
+
tables: tables.length,
|
|
69
|
+
customQueries: customOperations?.queries.length ?? 0,
|
|
70
|
+
customMutations: customOperations?.mutations.length ?? 0,
|
|
71
|
+
totalFiles: files.length,
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
// Re-export generators for direct use
|
|
76
|
+
var client_generator_2 = require("./client-generator");
|
|
77
|
+
Object.defineProperty(exports, "generateOrmClientFile", { enumerable: true, get: function () { return client_generator_2.generateOrmClientFile; } });
|
|
78
|
+
Object.defineProperty(exports, "generateQueryBuilderFile", { enumerable: true, get: function () { return client_generator_2.generateQueryBuilderFile; } });
|
|
79
|
+
Object.defineProperty(exports, "generateSelectTypesFile", { enumerable: true, get: function () { return client_generator_2.generateSelectTypesFile; } });
|
|
80
|
+
var model_generator_2 = require("./model-generator");
|
|
81
|
+
Object.defineProperty(exports, "generateModelFile", { enumerable: true, get: function () { return model_generator_2.generateModelFile; } });
|
|
82
|
+
Object.defineProperty(exports, "generateAllModelFiles", { enumerable: true, get: function () { return model_generator_2.generateAllModelFiles; } });
|
|
83
|
+
var custom_ops_generator_2 = require("./custom-ops-generator");
|
|
84
|
+
Object.defineProperty(exports, "generateCustomQueryOpsFile", { enumerable: true, get: function () { return custom_ops_generator_2.generateCustomQueryOpsFile; } });
|
|
85
|
+
Object.defineProperty(exports, "generateCustomMutationOpsFile", { enumerable: true, get: function () { return custom_ops_generator_2.generateCustomMutationOpsFile; } });
|
|
86
|
+
var barrel_2 = require("./barrel");
|
|
87
|
+
Object.defineProperty(exports, "generateModelsBarrel", { enumerable: true, get: function () { return barrel_2.generateModelsBarrel; } });
|
|
88
|
+
Object.defineProperty(exports, "generateTypesBarrel", { enumerable: true, get: function () { return barrel_2.generateTypesBarrel; } });
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { TypeRegistry, CleanArgument, CleanTable } from '../../../types/schema';
|
|
2
|
+
export interface GeneratedInputTypesFile {
|
|
3
|
+
fileName: string;
|
|
4
|
+
content: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Collect all input type names used by operations
|
|
8
|
+
*/
|
|
9
|
+
export declare function collectInputTypeNames(operations: Array<{
|
|
10
|
+
args: CleanArgument[];
|
|
11
|
+
}>): Set<string>;
|
|
12
|
+
/**
|
|
13
|
+
* Collect all payload type names from operation return types
|
|
14
|
+
*/
|
|
15
|
+
export declare function collectPayloadTypeNames(operations: Array<{
|
|
16
|
+
returnType: CleanArgument['type'];
|
|
17
|
+
}>): Set<string>;
|
|
18
|
+
/**
|
|
19
|
+
* Generate comprehensive input-types.ts file using ts-morph AST
|
|
20
|
+
*/
|
|
21
|
+
export declare function generateInputTypesFile(typeRegistry: TypeRegistry, usedInputTypes: Set<string>, tables?: CleanTable[], usedPayloadTypes?: Set<string>): GeneratedInputTypesFile;
|