@constructive-io/graphql-codegen 2.19.0 → 2.20.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/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 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Jest globals - no import needed
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const input_types_generator_1 = require("./input-types-generator");
|
|
5
|
+
const uuidType = { gqlType: 'UUID', isArray: false };
|
|
6
|
+
const stringType = { gqlType: 'String', isArray: false };
|
|
7
|
+
function createTable(table) {
|
|
8
|
+
return {
|
|
9
|
+
name: table.name,
|
|
10
|
+
fields: table.fields ?? [],
|
|
11
|
+
relations: table.relations ?? {
|
|
12
|
+
belongsTo: [],
|
|
13
|
+
hasOne: [],
|
|
14
|
+
hasMany: [],
|
|
15
|
+
manyToMany: [],
|
|
16
|
+
},
|
|
17
|
+
query: table.query,
|
|
18
|
+
inflection: table.inflection,
|
|
19
|
+
constraints: table.constraints,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
describe('input-types-generator', () => {
|
|
23
|
+
it('emits relation helpers and select types with related names', () => {
|
|
24
|
+
const userTable = createTable({
|
|
25
|
+
name: 'User',
|
|
26
|
+
fields: [
|
|
27
|
+
{ name: 'id', type: uuidType },
|
|
28
|
+
{ name: 'name', type: stringType },
|
|
29
|
+
],
|
|
30
|
+
relations: {
|
|
31
|
+
belongsTo: [],
|
|
32
|
+
hasOne: [],
|
|
33
|
+
hasMany: [
|
|
34
|
+
{
|
|
35
|
+
fieldName: 'posts',
|
|
36
|
+
isUnique: false,
|
|
37
|
+
referencedByTable: 'Post',
|
|
38
|
+
type: null,
|
|
39
|
+
keys: [],
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
manyToMany: [],
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
const postTable = createTable({
|
|
46
|
+
name: 'Post',
|
|
47
|
+
fields: [
|
|
48
|
+
{ name: 'id', type: uuidType },
|
|
49
|
+
{ name: 'title', type: stringType },
|
|
50
|
+
],
|
|
51
|
+
relations: {
|
|
52
|
+
belongsTo: [
|
|
53
|
+
{
|
|
54
|
+
fieldName: 'author',
|
|
55
|
+
isUnique: false,
|
|
56
|
+
referencesTable: 'User',
|
|
57
|
+
type: null,
|
|
58
|
+
keys: [],
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
hasOne: [],
|
|
62
|
+
hasMany: [],
|
|
63
|
+
manyToMany: [],
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
const result = (0, input_types_generator_1.generateInputTypesFile)(new Map(), new Set(), [userTable, postTable]);
|
|
67
|
+
expect(result.content).toContain('export interface ConnectionResult<T>');
|
|
68
|
+
expect(result.content).toContain('export interface UserRelations');
|
|
69
|
+
expect(result.content).toContain('posts?: ConnectionResult<Post>');
|
|
70
|
+
expect(result.content).toContain('export type UserWithRelations = User & UserRelations;');
|
|
71
|
+
expect(result.content).toContain('posts?: boolean | {');
|
|
72
|
+
expect(result.content).toContain('orderBy?: PostsOrderBy[];');
|
|
73
|
+
expect(result.content).toContain('author?: boolean | { select?: UserSelect };');
|
|
74
|
+
});
|
|
75
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model class generator for ORM client
|
|
3
|
+
*
|
|
4
|
+
* Generates per-table model classes with findMany, findFirst, create, update, delete methods.
|
|
5
|
+
*
|
|
6
|
+
* Example output:
|
|
7
|
+
* ```typescript
|
|
8
|
+
* export class UserModel {
|
|
9
|
+
* constructor(private client: OrmClient) {}
|
|
10
|
+
*
|
|
11
|
+
* findMany<S extends UserSelect>(args?: FindManyArgs<S, UserWhereInput, UserOrderBy>) {
|
|
12
|
+
* return new QueryBuilder<...>({ ... });
|
|
13
|
+
* }
|
|
14
|
+
* // ...
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
import type { CleanTable } from '../../../types/schema';
|
|
19
|
+
export interface GeneratedModelFile {
|
|
20
|
+
fileName: string;
|
|
21
|
+
content: string;
|
|
22
|
+
modelName: string;
|
|
23
|
+
tableName: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Generate a model class file for a table
|
|
27
|
+
*/
|
|
28
|
+
export declare function generateModelFile(table: CleanTable, _useSharedTypes: boolean): GeneratedModelFile;
|
|
29
|
+
/**
|
|
30
|
+
* Generate all model files for a list of tables
|
|
31
|
+
*/
|
|
32
|
+
export declare function generateAllModelFiles(tables: CleanTable[], useSharedTypes: boolean): GeneratedModelFile[];
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateModelFile = generateModelFile;
|
|
4
|
+
exports.generateAllModelFiles = generateAllModelFiles;
|
|
5
|
+
const ts_ast_1 = require("../ts-ast");
|
|
6
|
+
const utils_1 = require("../utils");
|
|
7
|
+
/**
|
|
8
|
+
* Generate a model class file for a table
|
|
9
|
+
*/
|
|
10
|
+
function generateModelFile(table, _useSharedTypes) {
|
|
11
|
+
const project = (0, ts_ast_1.createProject)();
|
|
12
|
+
const { typeName, singularName, pluralName } = (0, utils_1.getTableNames)(table);
|
|
13
|
+
const modelName = `${typeName}Model`;
|
|
14
|
+
const fileName = `${(0, utils_1.lcFirst)(typeName)}.ts`;
|
|
15
|
+
const entityLower = singularName;
|
|
16
|
+
// Type names for this entity
|
|
17
|
+
const selectTypeName = `${typeName}Select`;
|
|
18
|
+
const relationTypeName = `${typeName}WithRelations`;
|
|
19
|
+
const whereTypeName = `${typeName}Filter`;
|
|
20
|
+
const orderByTypeName = `${typeName}sOrderBy`; // PostGraphile uses plural
|
|
21
|
+
const createInputTypeName = `Create${typeName}Input`;
|
|
22
|
+
const updateInputTypeName = `Update${typeName}Input`;
|
|
23
|
+
const deleteInputTypeName = `Delete${typeName}Input`;
|
|
24
|
+
const patchTypeName = `${typeName}Patch`;
|
|
25
|
+
const createDataType = `${createInputTypeName}['${singularName}']`;
|
|
26
|
+
// Query names from PostGraphile
|
|
27
|
+
const pluralQueryName = table.query?.all ?? pluralName;
|
|
28
|
+
const createMutationName = table.query?.create ?? `create${typeName}`;
|
|
29
|
+
const updateMutationName = table.query?.update;
|
|
30
|
+
const deleteMutationName = table.query?.delete;
|
|
31
|
+
const sourceFile = (0, ts_ast_1.createSourceFile)(project, fileName);
|
|
32
|
+
// Add file header
|
|
33
|
+
sourceFile.insertText(0, (0, ts_ast_1.createFileHeader)(`${typeName} model for ORM client`) + '\n\n');
|
|
34
|
+
// Add imports - import types from respective modules
|
|
35
|
+
sourceFile.addImportDeclarations([
|
|
36
|
+
(0, ts_ast_1.createImport)({
|
|
37
|
+
moduleSpecifier: '../client',
|
|
38
|
+
namedImports: ['OrmClient'],
|
|
39
|
+
}),
|
|
40
|
+
(0, ts_ast_1.createImport)({
|
|
41
|
+
moduleSpecifier: '../query-builder',
|
|
42
|
+
namedImports: [
|
|
43
|
+
'QueryBuilder',
|
|
44
|
+
'buildFindManyDocument',
|
|
45
|
+
'buildFindFirstDocument',
|
|
46
|
+
'buildCreateDocument',
|
|
47
|
+
'buildUpdateDocument',
|
|
48
|
+
'buildDeleteDocument',
|
|
49
|
+
],
|
|
50
|
+
}),
|
|
51
|
+
(0, ts_ast_1.createImport)({
|
|
52
|
+
moduleSpecifier: '../select-types',
|
|
53
|
+
typeOnlyNamedImports: [
|
|
54
|
+
'ConnectionResult',
|
|
55
|
+
'FindManyArgs',
|
|
56
|
+
'FindFirstArgs',
|
|
57
|
+
'CreateArgs',
|
|
58
|
+
'UpdateArgs',
|
|
59
|
+
'DeleteArgs',
|
|
60
|
+
'InferSelectResult',
|
|
61
|
+
],
|
|
62
|
+
}),
|
|
63
|
+
]);
|
|
64
|
+
// Build complete set of input-types imports
|
|
65
|
+
// Select types are now centralized in input-types.ts with relations included
|
|
66
|
+
const inputTypeImports = new Set([
|
|
67
|
+
typeName,
|
|
68
|
+
relationTypeName,
|
|
69
|
+
selectTypeName,
|
|
70
|
+
whereTypeName,
|
|
71
|
+
orderByTypeName,
|
|
72
|
+
createInputTypeName,
|
|
73
|
+
updateInputTypeName,
|
|
74
|
+
patchTypeName,
|
|
75
|
+
]);
|
|
76
|
+
// Add single combined import from input-types
|
|
77
|
+
sourceFile.addImportDeclaration((0, ts_ast_1.createImport)({
|
|
78
|
+
moduleSpecifier: '../input-types',
|
|
79
|
+
typeOnlyNamedImports: Array.from(inputTypeImports),
|
|
80
|
+
}));
|
|
81
|
+
// Add Model class
|
|
82
|
+
sourceFile.addStatements('\n// ============================================================================');
|
|
83
|
+
sourceFile.addStatements('// Model Class');
|
|
84
|
+
sourceFile.addStatements('// ============================================================================\n');
|
|
85
|
+
// Generate the model class
|
|
86
|
+
const classDeclaration = sourceFile.addClass({
|
|
87
|
+
name: modelName,
|
|
88
|
+
isExported: true,
|
|
89
|
+
});
|
|
90
|
+
// Constructor
|
|
91
|
+
classDeclaration.addConstructor({
|
|
92
|
+
parameters: [
|
|
93
|
+
{
|
|
94
|
+
name: 'client',
|
|
95
|
+
type: 'OrmClient',
|
|
96
|
+
scope: 'private',
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
});
|
|
100
|
+
// findMany method - uses const generic for proper literal type inference
|
|
101
|
+
classDeclaration.addMethod({
|
|
102
|
+
name: 'findMany',
|
|
103
|
+
typeParameters: [`const S extends ${selectTypeName}`],
|
|
104
|
+
parameters: [
|
|
105
|
+
{
|
|
106
|
+
name: 'args',
|
|
107
|
+
type: `FindManyArgs<S, ${whereTypeName}, ${orderByTypeName}>`,
|
|
108
|
+
hasQuestionToken: true,
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
returnType: `QueryBuilder<{ ${pluralQueryName}: ConnectionResult<InferSelectResult<${relationTypeName}, S>> }>`,
|
|
112
|
+
statements: `const { document, variables } = buildFindManyDocument(
|
|
113
|
+
'${typeName}',
|
|
114
|
+
'${pluralQueryName}',
|
|
115
|
+
args?.select,
|
|
116
|
+
{
|
|
117
|
+
where: args?.where,
|
|
118
|
+
orderBy: args?.orderBy as string[] | undefined,
|
|
119
|
+
first: args?.first,
|
|
120
|
+
last: args?.last,
|
|
121
|
+
after: args?.after,
|
|
122
|
+
before: args?.before,
|
|
123
|
+
offset: args?.offset,
|
|
124
|
+
},
|
|
125
|
+
'${whereTypeName}'
|
|
126
|
+
);
|
|
127
|
+
return new QueryBuilder({
|
|
128
|
+
client: this.client,
|
|
129
|
+
operation: 'query',
|
|
130
|
+
operationName: '${typeName}',
|
|
131
|
+
fieldName: '${pluralQueryName}',
|
|
132
|
+
document,
|
|
133
|
+
variables,
|
|
134
|
+
});`,
|
|
135
|
+
});
|
|
136
|
+
// findFirst method - uses const generic for proper literal type inference
|
|
137
|
+
classDeclaration.addMethod({
|
|
138
|
+
name: 'findFirst',
|
|
139
|
+
typeParameters: [`const S extends ${selectTypeName}`],
|
|
140
|
+
parameters: [
|
|
141
|
+
{
|
|
142
|
+
name: 'args',
|
|
143
|
+
type: `FindFirstArgs<S, ${whereTypeName}>`,
|
|
144
|
+
hasQuestionToken: true,
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
returnType: `QueryBuilder<{ ${pluralQueryName}: { nodes: InferSelectResult<${relationTypeName}, S>[] } }>`,
|
|
148
|
+
statements: `const { document, variables } = buildFindFirstDocument(
|
|
149
|
+
'${typeName}',
|
|
150
|
+
'${pluralQueryName}',
|
|
151
|
+
args?.select,
|
|
152
|
+
{ where: args?.where },
|
|
153
|
+
'${whereTypeName}'
|
|
154
|
+
);
|
|
155
|
+
return new QueryBuilder({
|
|
156
|
+
client: this.client,
|
|
157
|
+
operation: 'query',
|
|
158
|
+
operationName: '${typeName}',
|
|
159
|
+
fieldName: '${pluralQueryName}',
|
|
160
|
+
document,
|
|
161
|
+
variables,
|
|
162
|
+
});`,
|
|
163
|
+
});
|
|
164
|
+
// create method - uses const generic for proper literal type inference
|
|
165
|
+
classDeclaration.addMethod({
|
|
166
|
+
name: 'create',
|
|
167
|
+
typeParameters: [`const S extends ${selectTypeName}`],
|
|
168
|
+
parameters: [
|
|
169
|
+
{
|
|
170
|
+
name: 'args',
|
|
171
|
+
type: `CreateArgs<S, ${createDataType}>`,
|
|
172
|
+
},
|
|
173
|
+
],
|
|
174
|
+
returnType: `QueryBuilder<{ ${createMutationName}: { ${entityLower}: InferSelectResult<${relationTypeName}, S> } }>`,
|
|
175
|
+
statements: `const { document, variables } = buildCreateDocument(
|
|
176
|
+
'${typeName}',
|
|
177
|
+
'${createMutationName}',
|
|
178
|
+
'${entityLower}',
|
|
179
|
+
args.select,
|
|
180
|
+
args.data,
|
|
181
|
+
'${createInputTypeName}'
|
|
182
|
+
);
|
|
183
|
+
return new QueryBuilder({
|
|
184
|
+
client: this.client,
|
|
185
|
+
operation: 'mutation',
|
|
186
|
+
operationName: '${typeName}',
|
|
187
|
+
fieldName: '${createMutationName}',
|
|
188
|
+
document,
|
|
189
|
+
variables,
|
|
190
|
+
});`,
|
|
191
|
+
});
|
|
192
|
+
// update method (if available) - uses const generic for proper literal type inference
|
|
193
|
+
if (updateMutationName) {
|
|
194
|
+
classDeclaration.addMethod({
|
|
195
|
+
name: 'update',
|
|
196
|
+
typeParameters: [`const S extends ${selectTypeName}`],
|
|
197
|
+
parameters: [
|
|
198
|
+
{
|
|
199
|
+
name: 'args',
|
|
200
|
+
type: `UpdateArgs<S, { id: string }, ${patchTypeName}>`,
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
returnType: `QueryBuilder<{ ${updateMutationName}: { ${entityLower}: InferSelectResult<${relationTypeName}, S> } }>`,
|
|
204
|
+
statements: `const { document, variables } = buildUpdateDocument(
|
|
205
|
+
'${typeName}',
|
|
206
|
+
'${updateMutationName}',
|
|
207
|
+
'${entityLower}',
|
|
208
|
+
args.select,
|
|
209
|
+
args.where,
|
|
210
|
+
args.data,
|
|
211
|
+
'${updateInputTypeName}'
|
|
212
|
+
);
|
|
213
|
+
return new QueryBuilder({
|
|
214
|
+
client: this.client,
|
|
215
|
+
operation: 'mutation',
|
|
216
|
+
operationName: '${typeName}',
|
|
217
|
+
fieldName: '${updateMutationName}',
|
|
218
|
+
document,
|
|
219
|
+
variables,
|
|
220
|
+
});`,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
// delete method (if available)
|
|
224
|
+
if (deleteMutationName) {
|
|
225
|
+
classDeclaration.addMethod({
|
|
226
|
+
name: 'delete',
|
|
227
|
+
parameters: [
|
|
228
|
+
{
|
|
229
|
+
name: 'args',
|
|
230
|
+
type: `DeleteArgs<{ id: string }>`,
|
|
231
|
+
},
|
|
232
|
+
],
|
|
233
|
+
returnType: `QueryBuilder<{ ${deleteMutationName}: { ${entityLower}: { id: string } } }>`,
|
|
234
|
+
statements: `const { document, variables } = buildDeleteDocument(
|
|
235
|
+
'${typeName}',
|
|
236
|
+
'${deleteMutationName}',
|
|
237
|
+
'${entityLower}',
|
|
238
|
+
args.where,
|
|
239
|
+
'${deleteInputTypeName}'
|
|
240
|
+
);
|
|
241
|
+
return new QueryBuilder({
|
|
242
|
+
client: this.client,
|
|
243
|
+
operation: 'mutation',
|
|
244
|
+
operationName: '${typeName}',
|
|
245
|
+
fieldName: '${deleteMutationName}',
|
|
246
|
+
document,
|
|
247
|
+
variables,
|
|
248
|
+
});`,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
fileName,
|
|
253
|
+
content: (0, ts_ast_1.getFormattedOutput)(sourceFile),
|
|
254
|
+
modelName,
|
|
255
|
+
tableName: table.name,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
// Select types with relations are now generated in input-types.ts
|
|
259
|
+
/**
|
|
260
|
+
* Generate all model files for a list of tables
|
|
261
|
+
*/
|
|
262
|
+
function generateAllModelFiles(tables, useSharedTypes) {
|
|
263
|
+
return tables.map((table) => generateModelFile(table, useSharedTypes));
|
|
264
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime query builder for ORM client
|
|
3
|
+
*
|
|
4
|
+
* This module provides the runtime functionality that builds GraphQL
|
|
5
|
+
* queries/mutations from the fluent API calls and executes them.
|
|
6
|
+
*
|
|
7
|
+
* This file will be copied to the generated output (not generated via AST)
|
|
8
|
+
* since it's runtime code that doesn't change based on schema.
|
|
9
|
+
*/
|
|
10
|
+
import type { QueryResult } from './select-types';
|
|
11
|
+
/**
|
|
12
|
+
* ORM client configuration
|
|
13
|
+
*/
|
|
14
|
+
export interface OrmClientConfig {
|
|
15
|
+
endpoint: string;
|
|
16
|
+
headers?: Record<string, string>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Internal client state
|
|
20
|
+
*/
|
|
21
|
+
export declare class OrmClient {
|
|
22
|
+
private endpoint;
|
|
23
|
+
private headers;
|
|
24
|
+
constructor(config: OrmClientConfig);
|
|
25
|
+
/**
|
|
26
|
+
* Execute a GraphQL query/mutation
|
|
27
|
+
*/
|
|
28
|
+
execute<T>(document: string, variables?: Record<string, unknown>): Promise<QueryResult<T>>;
|
|
29
|
+
/**
|
|
30
|
+
* Update headers (e.g., for auth token refresh)
|
|
31
|
+
*/
|
|
32
|
+
setHeaders(headers: Record<string, string>): void;
|
|
33
|
+
/**
|
|
34
|
+
* Get current endpoint
|
|
35
|
+
*/
|
|
36
|
+
getEndpoint(): string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Configuration for building a query
|
|
40
|
+
*/
|
|
41
|
+
export interface QueryBuilderConfig {
|
|
42
|
+
client: OrmClient;
|
|
43
|
+
operation: 'query' | 'mutation';
|
|
44
|
+
operationName: string;
|
|
45
|
+
fieldName: string;
|
|
46
|
+
document: string;
|
|
47
|
+
variables?: Record<string, unknown>;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Query builder that holds the query configuration and executes it
|
|
51
|
+
*
|
|
52
|
+
* Usage:
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const result = await new QueryBuilder(config).execute();
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare class QueryBuilder<TResult> {
|
|
58
|
+
private config;
|
|
59
|
+
constructor(config: QueryBuilderConfig);
|
|
60
|
+
/**
|
|
61
|
+
* Execute the query and return the result
|
|
62
|
+
*/
|
|
63
|
+
execute(): Promise<QueryResult<TResult>>;
|
|
64
|
+
/**
|
|
65
|
+
* Get the GraphQL document (useful for debugging)
|
|
66
|
+
*/
|
|
67
|
+
toGraphQL(): string;
|
|
68
|
+
/**
|
|
69
|
+
* Get the variables (useful for debugging)
|
|
70
|
+
*/
|
|
71
|
+
getVariables(): Record<string, unknown> | undefined;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Build field selections from a select object
|
|
75
|
+
*
|
|
76
|
+
* Converts:
|
|
77
|
+
* { id: true, name: true, posts: { select: { title: true } } }
|
|
78
|
+
*
|
|
79
|
+
* To:
|
|
80
|
+
* id
|
|
81
|
+
* name
|
|
82
|
+
* posts {
|
|
83
|
+
* nodes { title }
|
|
84
|
+
* totalCount
|
|
85
|
+
* pageInfo { hasNextPage hasPreviousPage startCursor endCursor }
|
|
86
|
+
* }
|
|
87
|
+
*/
|
|
88
|
+
export declare function buildSelections(select: Record<string, unknown> | undefined, fieldMeta?: Record<string, FieldMeta>): string;
|
|
89
|
+
/**
|
|
90
|
+
* Field metadata for determining connection vs direct relation
|
|
91
|
+
*/
|
|
92
|
+
export interface FieldMeta {
|
|
93
|
+
isConnection: boolean;
|
|
94
|
+
isNullable: boolean;
|
|
95
|
+
typeName: string;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Build a findMany query document
|
|
99
|
+
*/
|
|
100
|
+
export declare function buildFindManyDocument(operationName: string, queryField: string, select: Record<string, unknown> | undefined, args: {
|
|
101
|
+
where?: Record<string, unknown>;
|
|
102
|
+
orderBy?: string[];
|
|
103
|
+
first?: number;
|
|
104
|
+
last?: number;
|
|
105
|
+
after?: string;
|
|
106
|
+
before?: string;
|
|
107
|
+
offset?: number;
|
|
108
|
+
}, filterTypeName: string): {
|
|
109
|
+
document: string;
|
|
110
|
+
variables: Record<string, unknown>;
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Build a findFirst query document
|
|
114
|
+
*/
|
|
115
|
+
export declare function buildFindFirstDocument(operationName: string, queryField: string, select: Record<string, unknown> | undefined, args: {
|
|
116
|
+
where?: Record<string, unknown>;
|
|
117
|
+
}, filterTypeName: string): {
|
|
118
|
+
document: string;
|
|
119
|
+
variables: Record<string, unknown>;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Build a create mutation document
|
|
123
|
+
*/
|
|
124
|
+
export declare function buildCreateDocument(operationName: string, mutationField: string, entityField: string, select: Record<string, unknown> | undefined, data: Record<string, unknown>, inputTypeName: string): {
|
|
125
|
+
document: string;
|
|
126
|
+
variables: Record<string, unknown>;
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Build an update mutation document
|
|
130
|
+
*/
|
|
131
|
+
export declare function buildUpdateDocument(operationName: string, mutationField: string, entityField: string, select: Record<string, unknown> | undefined, where: Record<string, unknown>, data: Record<string, unknown>, inputTypeName: string): {
|
|
132
|
+
document: string;
|
|
133
|
+
variables: Record<string, unknown>;
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* Build a delete mutation document
|
|
137
|
+
*/
|
|
138
|
+
export declare function buildDeleteDocument(operationName: string, mutationField: string, entityField: string, where: Record<string, unknown>, inputTypeName: string): {
|
|
139
|
+
document: string;
|
|
140
|
+
variables: Record<string, unknown>;
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Build a custom query document
|
|
144
|
+
*/
|
|
145
|
+
export declare function buildCustomQueryDocument(operationName: string, queryField: string, select: Record<string, unknown> | undefined, args: Record<string, unknown> | undefined, variableDefinitions: Array<{
|
|
146
|
+
name: string;
|
|
147
|
+
type: string;
|
|
148
|
+
}>): {
|
|
149
|
+
document: string;
|
|
150
|
+
variables: Record<string, unknown>;
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* Build a custom mutation document
|
|
154
|
+
*/
|
|
155
|
+
export declare function buildCustomMutationDocument(operationName: string, mutationField: string, select: Record<string, unknown> | undefined, args: Record<string, unknown> | undefined, variableDefinitions: Array<{
|
|
156
|
+
name: string;
|
|
157
|
+
type: string;
|
|
158
|
+
}>): {
|
|
159
|
+
document: string;
|
|
160
|
+
variables: Record<string, unknown>;
|
|
161
|
+
};
|