@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.
Files changed (301) hide show
  1. package/README.md +1818 -113
  2. package/__tests__/codegen/input-types-generator.test.d.ts +1 -0
  3. package/__tests__/codegen/input-types-generator.test.js +635 -0
  4. package/cli/codegen/barrel.d.ts +27 -0
  5. package/cli/codegen/barrel.js +163 -0
  6. package/cli/codegen/client.d.ts +4 -0
  7. package/cli/codegen/client.js +170 -0
  8. package/cli/codegen/custom-mutations.d.ts +38 -0
  9. package/cli/codegen/custom-mutations.js +149 -0
  10. package/cli/codegen/custom-queries.d.ts +38 -0
  11. package/cli/codegen/custom-queries.js +358 -0
  12. package/cli/codegen/filters.d.ts +27 -0
  13. package/cli/codegen/filters.js +357 -0
  14. package/cli/codegen/gql-ast.d.ts +41 -0
  15. package/cli/codegen/gql-ast.js +329 -0
  16. package/cli/codegen/index.d.ts +71 -0
  17. package/cli/codegen/index.js +147 -0
  18. package/cli/codegen/mutations.d.ts +30 -0
  19. package/cli/codegen/mutations.js +410 -0
  20. package/cli/codegen/orm/barrel.d.ts +18 -0
  21. package/cli/codegen/orm/barrel.js +48 -0
  22. package/cli/codegen/orm/client-generator.d.ts +45 -0
  23. package/cli/codegen/orm/client-generator.js +646 -0
  24. package/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
  25. package/cli/codegen/orm/custom-ops-generator.js +350 -0
  26. package/cli/codegen/orm/index.d.ts +38 -0
  27. package/cli/codegen/orm/index.js +88 -0
  28. package/cli/codegen/orm/input-types-generator.d.ts +21 -0
  29. package/cli/codegen/orm/input-types-generator.js +705 -0
  30. package/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
  31. package/cli/codegen/orm/input-types-generator.test.js +75 -0
  32. package/cli/codegen/orm/model-generator.d.ts +32 -0
  33. package/cli/codegen/orm/model-generator.js +264 -0
  34. package/cli/codegen/orm/query-builder.d.ts +161 -0
  35. package/cli/codegen/orm/query-builder.js +366 -0
  36. package/cli/codegen/orm/select-types.d.ts +169 -0
  37. package/cli/codegen/orm/select-types.js +16 -0
  38. package/cli/codegen/orm/select-types.test.d.ts +11 -0
  39. package/cli/codegen/orm/select-types.test.js +22 -0
  40. package/cli/codegen/queries.d.ts +25 -0
  41. package/cli/codegen/queries.js +438 -0
  42. package/cli/codegen/scalars.d.ts +12 -0
  43. package/cli/codegen/scalars.js +71 -0
  44. package/cli/codegen/schema-gql-ast.d.ts +51 -0
  45. package/cli/codegen/schema-gql-ast.js +385 -0
  46. package/cli/codegen/ts-ast.d.ts +122 -0
  47. package/cli/codegen/ts-ast.js +280 -0
  48. package/cli/codegen/type-resolver.d.ts +96 -0
  49. package/cli/codegen/type-resolver.js +246 -0
  50. package/cli/codegen/types.d.ts +12 -0
  51. package/cli/codegen/types.js +69 -0
  52. package/cli/codegen/utils.d.ts +163 -0
  53. package/cli/codegen/utils.js +326 -0
  54. package/cli/commands/generate-orm.d.ts +37 -0
  55. package/cli/commands/generate-orm.js +195 -0
  56. package/cli/commands/generate.d.ts +39 -0
  57. package/cli/commands/generate.js +299 -0
  58. package/cli/commands/index.d.ts +7 -0
  59. package/cli/commands/index.js +12 -0
  60. package/cli/commands/init.d.ts +35 -0
  61. package/cli/commands/init.js +176 -0
  62. package/cli/index.d.ts +4 -0
  63. package/cli/index.js +291 -0
  64. package/cli/introspect/fetch-meta.d.ts +31 -0
  65. package/cli/introspect/fetch-meta.js +108 -0
  66. package/cli/introspect/fetch-schema.d.ts +21 -0
  67. package/cli/introspect/fetch-schema.js +86 -0
  68. package/cli/introspect/index.d.ts +8 -0
  69. package/cli/introspect/index.js +16 -0
  70. package/cli/introspect/meta-query.d.ts +111 -0
  71. package/cli/introspect/meta-query.js +191 -0
  72. package/cli/introspect/schema-query.d.ts +20 -0
  73. package/cli/introspect/schema-query.js +123 -0
  74. package/cli/introspect/transform-schema.d.ts +74 -0
  75. package/cli/introspect/transform-schema.js +269 -0
  76. package/cli/introspect/transform-schema.test.d.ts +1 -0
  77. package/cli/introspect/transform-schema.test.js +67 -0
  78. package/cli/introspect/transform.d.ts +21 -0
  79. package/cli/introspect/transform.js +216 -0
  80. package/cli/watch/cache.d.ts +45 -0
  81. package/cli/watch/cache.js +111 -0
  82. package/cli/watch/debounce.d.ts +19 -0
  83. package/cli/watch/debounce.js +89 -0
  84. package/cli/watch/hash.d.ts +17 -0
  85. package/cli/watch/hash.js +48 -0
  86. package/cli/watch/index.d.ts +10 -0
  87. package/cli/watch/index.js +22 -0
  88. package/cli/watch/orchestrator.d.ts +63 -0
  89. package/cli/watch/orchestrator.js +228 -0
  90. package/cli/watch/poller.d.ts +65 -0
  91. package/cli/watch/poller.js +203 -0
  92. package/cli/watch/types.d.ts +67 -0
  93. package/cli/watch/types.js +5 -0
  94. package/client/error.d.ts +95 -0
  95. package/client/error.js +255 -0
  96. package/client/execute.d.ts +57 -0
  97. package/client/execute.js +124 -0
  98. package/client/index.d.ts +6 -0
  99. package/client/index.js +18 -0
  100. package/client/typed-document.d.ts +31 -0
  101. package/client/typed-document.js +44 -0
  102. package/core/ast.d.ts +10 -0
  103. package/core/ast.js +593 -0
  104. package/core/custom-ast.d.ts +35 -0
  105. package/core/custom-ast.js +204 -0
  106. package/core/index.d.ts +8 -0
  107. package/core/index.js +33 -0
  108. package/core/meta-object/convert.d.ts +65 -0
  109. package/core/meta-object/convert.js +63 -0
  110. package/core/meta-object/format.json +93 -0
  111. package/core/meta-object/index.d.ts +2 -0
  112. package/core/meta-object/index.js +18 -0
  113. package/core/meta-object/validate.d.ts +9 -0
  114. package/core/meta-object/validate.js +34 -0
  115. package/core/query-builder.d.ts +46 -0
  116. package/core/query-builder.js +412 -0
  117. package/core/types.d.ts +139 -0
  118. package/core/types.js +28 -0
  119. package/esm/__tests__/codegen/input-types-generator.test.d.ts +1 -0
  120. package/esm/__tests__/codegen/input-types-generator.test.js +633 -0
  121. package/esm/cli/codegen/barrel.d.ts +27 -0
  122. package/esm/cli/codegen/barrel.js +156 -0
  123. package/esm/cli/codegen/client.d.ts +4 -0
  124. package/esm/cli/codegen/client.js +167 -0
  125. package/esm/cli/codegen/custom-mutations.d.ts +38 -0
  126. package/esm/cli/codegen/custom-mutations.js +145 -0
  127. package/esm/cli/codegen/custom-queries.d.ts +38 -0
  128. package/esm/cli/codegen/custom-queries.js +354 -0
  129. package/esm/cli/codegen/filters.d.ts +27 -0
  130. package/esm/cli/codegen/filters.js +351 -0
  131. package/esm/cli/codegen/gql-ast.d.ts +41 -0
  132. package/esm/cli/codegen/gql-ast.js +288 -0
  133. package/esm/cli/codegen/index.d.ts +71 -0
  134. package/esm/cli/codegen/index.js +124 -0
  135. package/esm/cli/codegen/mutations.d.ts +30 -0
  136. package/esm/cli/codegen/mutations.js +404 -0
  137. package/esm/cli/codegen/orm/barrel.d.ts +18 -0
  138. package/esm/cli/codegen/orm/barrel.js +44 -0
  139. package/esm/cli/codegen/orm/client-generator.d.ts +45 -0
  140. package/esm/cli/codegen/orm/client-generator.js +640 -0
  141. package/esm/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
  142. package/esm/cli/codegen/orm/custom-ops-generator.js +346 -0
  143. package/esm/cli/codegen/orm/index.d.ts +38 -0
  144. package/esm/cli/codegen/orm/index.js +75 -0
  145. package/esm/cli/codegen/orm/input-types-generator.d.ts +21 -0
  146. package/esm/cli/codegen/orm/input-types-generator.js +700 -0
  147. package/esm/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
  148. package/esm/cli/codegen/orm/input-types-generator.test.js +73 -0
  149. package/esm/cli/codegen/orm/model-generator.d.ts +32 -0
  150. package/esm/cli/codegen/orm/model-generator.js +260 -0
  151. package/esm/cli/codegen/orm/query-builder.d.ts +161 -0
  152. package/esm/cli/codegen/orm/query-builder.js +353 -0
  153. package/esm/cli/codegen/orm/select-types.d.ts +169 -0
  154. package/esm/cli/codegen/orm/select-types.js +15 -0
  155. package/esm/cli/codegen/orm/select-types.test.d.ts +11 -0
  156. package/esm/cli/codegen/orm/select-types.test.js +21 -0
  157. package/esm/cli/codegen/queries.d.ts +25 -0
  158. package/esm/cli/codegen/queries.js +433 -0
  159. package/esm/cli/codegen/scalars.d.ts +12 -0
  160. package/esm/cli/codegen/scalars.js +66 -0
  161. package/esm/cli/codegen/schema-gql-ast.d.ts +51 -0
  162. package/esm/cli/codegen/schema-gql-ast.js +343 -0
  163. package/esm/cli/codegen/ts-ast.d.ts +122 -0
  164. package/esm/cli/codegen/ts-ast.js +260 -0
  165. package/esm/cli/codegen/type-resolver.d.ts +96 -0
  166. package/esm/cli/codegen/type-resolver.js +224 -0
  167. package/esm/cli/codegen/types.d.ts +12 -0
  168. package/esm/cli/codegen/types.js +65 -0
  169. package/esm/cli/codegen/utils.d.ts +163 -0
  170. package/esm/cli/codegen/utils.js +288 -0
  171. package/esm/cli/commands/generate-orm.d.ts +37 -0
  172. package/esm/cli/commands/generate-orm.js +192 -0
  173. package/esm/cli/commands/generate.d.ts +39 -0
  174. package/esm/cli/commands/generate.js +262 -0
  175. package/esm/cli/commands/index.d.ts +7 -0
  176. package/esm/cli/commands/index.js +5 -0
  177. package/esm/cli/commands/init.d.ts +35 -0
  178. package/esm/cli/commands/init.js +138 -0
  179. package/esm/cli/index.d.ts +4 -0
  180. package/esm/cli/index.js +256 -0
  181. package/esm/cli/introspect/fetch-meta.d.ts +31 -0
  182. package/esm/cli/introspect/fetch-meta.js +104 -0
  183. package/esm/cli/introspect/fetch-schema.d.ts +21 -0
  184. package/esm/cli/introspect/fetch-schema.js +83 -0
  185. package/esm/cli/introspect/index.d.ts +8 -0
  186. package/esm/cli/introspect/index.js +6 -0
  187. package/esm/cli/introspect/meta-query.d.ts +111 -0
  188. package/esm/cli/introspect/meta-query.js +188 -0
  189. package/esm/cli/introspect/schema-query.d.ts +20 -0
  190. package/esm/cli/introspect/schema-query.js +120 -0
  191. package/esm/cli/introspect/transform-schema.d.ts +74 -0
  192. package/esm/cli/introspect/transform-schema.js +259 -0
  193. package/esm/cli/introspect/transform-schema.test.d.ts +1 -0
  194. package/esm/cli/introspect/transform-schema.test.js +65 -0
  195. package/esm/cli/introspect/transform.d.ts +21 -0
  196. package/esm/cli/introspect/transform.js +210 -0
  197. package/esm/cli/watch/cache.d.ts +45 -0
  198. package/esm/cli/watch/cache.js +73 -0
  199. package/esm/cli/watch/debounce.d.ts +19 -0
  200. package/esm/cli/watch/debounce.js +85 -0
  201. package/esm/cli/watch/hash.d.ts +17 -0
  202. package/esm/cli/watch/hash.js +43 -0
  203. package/esm/cli/watch/index.d.ts +10 -0
  204. package/esm/cli/watch/index.js +8 -0
  205. package/esm/cli/watch/orchestrator.d.ts +63 -0
  206. package/esm/cli/watch/orchestrator.js +223 -0
  207. package/esm/cli/watch/poller.d.ts +65 -0
  208. package/esm/cli/watch/poller.js +198 -0
  209. package/esm/cli/watch/types.d.ts +67 -0
  210. package/esm/cli/watch/types.js +4 -0
  211. package/esm/client/error.d.ts +95 -0
  212. package/esm/client/error.js +249 -0
  213. package/esm/client/execute.d.ts +57 -0
  214. package/esm/client/execute.js +120 -0
  215. package/esm/client/index.d.ts +6 -0
  216. package/esm/client/index.js +6 -0
  217. package/esm/client/typed-document.d.ts +31 -0
  218. package/esm/client/typed-document.js +40 -0
  219. package/esm/core/ast.d.ts +10 -0
  220. package/esm/core/ast.js +549 -0
  221. package/esm/core/custom-ast.d.ts +35 -0
  222. package/esm/core/custom-ast.js +161 -0
  223. package/esm/core/index.d.ts +8 -0
  224. package/esm/core/index.js +12 -0
  225. package/esm/core/meta-object/convert.d.ts +65 -0
  226. package/esm/core/meta-object/convert.js +60 -0
  227. package/esm/core/meta-object/format.json +93 -0
  228. package/esm/core/meta-object/index.d.ts +2 -0
  229. package/esm/core/meta-object/index.js +2 -0
  230. package/esm/core/meta-object/validate.d.ts +9 -0
  231. package/esm/core/meta-object/validate.js +28 -0
  232. package/esm/core/query-builder.d.ts +46 -0
  233. package/esm/core/query-builder.js +375 -0
  234. package/esm/core/types.d.ts +139 -0
  235. package/esm/core/types.js +24 -0
  236. package/esm/generators/field-selector.d.ts +30 -0
  237. package/esm/generators/field-selector.js +355 -0
  238. package/esm/generators/index.d.ts +6 -0
  239. package/esm/generators/index.js +9 -0
  240. package/esm/generators/mutations.d.ts +31 -0
  241. package/esm/generators/mutations.js +197 -0
  242. package/esm/generators/select.d.ts +50 -0
  243. package/esm/generators/select.js +636 -0
  244. package/esm/index.d.ts +12 -0
  245. package/esm/index.js +17 -3
  246. package/esm/react/index.d.ts +5 -0
  247. package/esm/react/index.js +6 -0
  248. package/esm/types/config.d.ts +199 -0
  249. package/esm/types/config.js +106 -0
  250. package/esm/types/index.d.ts +9 -0
  251. package/esm/types/index.js +4 -0
  252. package/esm/types/introspection.d.ts +121 -0
  253. package/esm/types/introspection.js +54 -0
  254. package/esm/types/mutation.d.ts +45 -0
  255. package/esm/types/mutation.js +4 -0
  256. package/esm/types/query.d.ts +82 -0
  257. package/esm/types/query.js +4 -0
  258. package/esm/types/schema.d.ts +253 -0
  259. package/esm/types/schema.js +5 -0
  260. package/esm/types/selection.d.ts +43 -0
  261. package/esm/types/selection.js +4 -0
  262. package/esm/utils/index.d.ts +4 -0
  263. package/esm/utils/index.js +4 -0
  264. package/generators/field-selector.d.ts +30 -0
  265. package/generators/field-selector.js +361 -0
  266. package/generators/index.d.ts +6 -0
  267. package/generators/index.js +27 -0
  268. package/generators/mutations.d.ts +31 -0
  269. package/generators/mutations.js +235 -0
  270. package/generators/select.d.ts +50 -0
  271. package/generators/select.js +679 -0
  272. package/index.d.ts +12 -3
  273. package/index.js +19 -3
  274. package/package.json +59 -38
  275. package/react/index.d.ts +5 -0
  276. package/react/index.js +9 -0
  277. package/types/config.d.ts +199 -0
  278. package/types/config.js +111 -0
  279. package/types/index.d.ts +9 -0
  280. package/types/index.js +10 -0
  281. package/types/introspection.d.ts +121 -0
  282. package/types/introspection.js +62 -0
  283. package/types/mutation.d.ts +45 -0
  284. package/types/mutation.js +5 -0
  285. package/types/query.d.ts +82 -0
  286. package/types/query.js +5 -0
  287. package/types/schema.d.ts +253 -0
  288. package/types/schema.js +6 -0
  289. package/types/selection.d.ts +43 -0
  290. package/types/selection.js +5 -0
  291. package/utils/index.d.ts +4 -0
  292. package/utils/index.js +7 -0
  293. package/codegen.d.ts +0 -13
  294. package/codegen.js +0 -293
  295. package/esm/codegen.js +0 -253
  296. package/esm/gql.js +0 -939
  297. package/esm/options.js +0 -27
  298. package/gql.d.ts +0 -188
  299. package/gql.js +0 -992
  300. package/options.d.ts +0 -45
  301. package/options.js +0 -31
@@ -0,0 +1,73 @@
1
+ // Jest globals - no import needed
2
+ import { generateInputTypesFile } from './input-types-generator';
3
+ const uuidType = { gqlType: 'UUID', isArray: false };
4
+ const stringType = { gqlType: 'String', isArray: false };
5
+ function createTable(table) {
6
+ return {
7
+ name: table.name,
8
+ fields: table.fields ?? [],
9
+ relations: table.relations ?? {
10
+ belongsTo: [],
11
+ hasOne: [],
12
+ hasMany: [],
13
+ manyToMany: [],
14
+ },
15
+ query: table.query,
16
+ inflection: table.inflection,
17
+ constraints: table.constraints,
18
+ };
19
+ }
20
+ describe('input-types-generator', () => {
21
+ it('emits relation helpers and select types with related names', () => {
22
+ const userTable = createTable({
23
+ name: 'User',
24
+ fields: [
25
+ { name: 'id', type: uuidType },
26
+ { name: 'name', type: stringType },
27
+ ],
28
+ relations: {
29
+ belongsTo: [],
30
+ hasOne: [],
31
+ hasMany: [
32
+ {
33
+ fieldName: 'posts',
34
+ isUnique: false,
35
+ referencedByTable: 'Post',
36
+ type: null,
37
+ keys: [],
38
+ },
39
+ ],
40
+ manyToMany: [],
41
+ },
42
+ });
43
+ const postTable = createTable({
44
+ name: 'Post',
45
+ fields: [
46
+ { name: 'id', type: uuidType },
47
+ { name: 'title', type: stringType },
48
+ ],
49
+ relations: {
50
+ belongsTo: [
51
+ {
52
+ fieldName: 'author',
53
+ isUnique: false,
54
+ referencesTable: 'User',
55
+ type: null,
56
+ keys: [],
57
+ },
58
+ ],
59
+ hasOne: [],
60
+ hasMany: [],
61
+ manyToMany: [],
62
+ },
63
+ });
64
+ const result = generateInputTypesFile(new Map(), new Set(), [userTable, postTable]);
65
+ expect(result.content).toContain('export interface ConnectionResult<T>');
66
+ expect(result.content).toContain('export interface UserRelations');
67
+ expect(result.content).toContain('posts?: ConnectionResult<Post>');
68
+ expect(result.content).toContain('export type UserWithRelations = User & UserRelations;');
69
+ expect(result.content).toContain('posts?: boolean | {');
70
+ expect(result.content).toContain('orderBy?: PostsOrderBy[];');
71
+ expect(result.content).toContain('author?: boolean | { select?: UserSelect };');
72
+ });
73
+ });
@@ -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,260 @@
1
+ import { createProject, createSourceFile, getFormattedOutput, createFileHeader, createImport, } from '../ts-ast';
2
+ import { getTableNames, lcFirst } from '../utils';
3
+ /**
4
+ * Generate a model class file for a table
5
+ */
6
+ export function generateModelFile(table, _useSharedTypes) {
7
+ const project = createProject();
8
+ const { typeName, singularName, pluralName } = getTableNames(table);
9
+ const modelName = `${typeName}Model`;
10
+ const fileName = `${lcFirst(typeName)}.ts`;
11
+ const entityLower = singularName;
12
+ // Type names for this entity
13
+ const selectTypeName = `${typeName}Select`;
14
+ const relationTypeName = `${typeName}WithRelations`;
15
+ const whereTypeName = `${typeName}Filter`;
16
+ const orderByTypeName = `${typeName}sOrderBy`; // PostGraphile uses plural
17
+ const createInputTypeName = `Create${typeName}Input`;
18
+ const updateInputTypeName = `Update${typeName}Input`;
19
+ const deleteInputTypeName = `Delete${typeName}Input`;
20
+ const patchTypeName = `${typeName}Patch`;
21
+ const createDataType = `${createInputTypeName}['${singularName}']`;
22
+ // Query names from PostGraphile
23
+ const pluralQueryName = table.query?.all ?? pluralName;
24
+ const createMutationName = table.query?.create ?? `create${typeName}`;
25
+ const updateMutationName = table.query?.update;
26
+ const deleteMutationName = table.query?.delete;
27
+ const sourceFile = createSourceFile(project, fileName);
28
+ // Add file header
29
+ sourceFile.insertText(0, createFileHeader(`${typeName} model for ORM client`) + '\n\n');
30
+ // Add imports - import types from respective modules
31
+ sourceFile.addImportDeclarations([
32
+ createImport({
33
+ moduleSpecifier: '../client',
34
+ namedImports: ['OrmClient'],
35
+ }),
36
+ createImport({
37
+ moduleSpecifier: '../query-builder',
38
+ namedImports: [
39
+ 'QueryBuilder',
40
+ 'buildFindManyDocument',
41
+ 'buildFindFirstDocument',
42
+ 'buildCreateDocument',
43
+ 'buildUpdateDocument',
44
+ 'buildDeleteDocument',
45
+ ],
46
+ }),
47
+ createImport({
48
+ moduleSpecifier: '../select-types',
49
+ typeOnlyNamedImports: [
50
+ 'ConnectionResult',
51
+ 'FindManyArgs',
52
+ 'FindFirstArgs',
53
+ 'CreateArgs',
54
+ 'UpdateArgs',
55
+ 'DeleteArgs',
56
+ 'InferSelectResult',
57
+ ],
58
+ }),
59
+ ]);
60
+ // Build complete set of input-types imports
61
+ // Select types are now centralized in input-types.ts with relations included
62
+ const inputTypeImports = new Set([
63
+ typeName,
64
+ relationTypeName,
65
+ selectTypeName,
66
+ whereTypeName,
67
+ orderByTypeName,
68
+ createInputTypeName,
69
+ updateInputTypeName,
70
+ patchTypeName,
71
+ ]);
72
+ // Add single combined import from input-types
73
+ sourceFile.addImportDeclaration(createImport({
74
+ moduleSpecifier: '../input-types',
75
+ typeOnlyNamedImports: Array.from(inputTypeImports),
76
+ }));
77
+ // Add Model class
78
+ sourceFile.addStatements('\n// ============================================================================');
79
+ sourceFile.addStatements('// Model Class');
80
+ sourceFile.addStatements('// ============================================================================\n');
81
+ // Generate the model class
82
+ const classDeclaration = sourceFile.addClass({
83
+ name: modelName,
84
+ isExported: true,
85
+ });
86
+ // Constructor
87
+ classDeclaration.addConstructor({
88
+ parameters: [
89
+ {
90
+ name: 'client',
91
+ type: 'OrmClient',
92
+ scope: 'private',
93
+ },
94
+ ],
95
+ });
96
+ // findMany method - uses const generic for proper literal type inference
97
+ classDeclaration.addMethod({
98
+ name: 'findMany',
99
+ typeParameters: [`const S extends ${selectTypeName}`],
100
+ parameters: [
101
+ {
102
+ name: 'args',
103
+ type: `FindManyArgs<S, ${whereTypeName}, ${orderByTypeName}>`,
104
+ hasQuestionToken: true,
105
+ },
106
+ ],
107
+ returnType: `QueryBuilder<{ ${pluralQueryName}: ConnectionResult<InferSelectResult<${relationTypeName}, S>> }>`,
108
+ statements: `const { document, variables } = buildFindManyDocument(
109
+ '${typeName}',
110
+ '${pluralQueryName}',
111
+ args?.select,
112
+ {
113
+ where: args?.where,
114
+ orderBy: args?.orderBy as string[] | undefined,
115
+ first: args?.first,
116
+ last: args?.last,
117
+ after: args?.after,
118
+ before: args?.before,
119
+ offset: args?.offset,
120
+ },
121
+ '${whereTypeName}'
122
+ );
123
+ return new QueryBuilder({
124
+ client: this.client,
125
+ operation: 'query',
126
+ operationName: '${typeName}',
127
+ fieldName: '${pluralQueryName}',
128
+ document,
129
+ variables,
130
+ });`,
131
+ });
132
+ // findFirst method - uses const generic for proper literal type inference
133
+ classDeclaration.addMethod({
134
+ name: 'findFirst',
135
+ typeParameters: [`const S extends ${selectTypeName}`],
136
+ parameters: [
137
+ {
138
+ name: 'args',
139
+ type: `FindFirstArgs<S, ${whereTypeName}>`,
140
+ hasQuestionToken: true,
141
+ },
142
+ ],
143
+ returnType: `QueryBuilder<{ ${pluralQueryName}: { nodes: InferSelectResult<${relationTypeName}, S>[] } }>`,
144
+ statements: `const { document, variables } = buildFindFirstDocument(
145
+ '${typeName}',
146
+ '${pluralQueryName}',
147
+ args?.select,
148
+ { where: args?.where },
149
+ '${whereTypeName}'
150
+ );
151
+ return new QueryBuilder({
152
+ client: this.client,
153
+ operation: 'query',
154
+ operationName: '${typeName}',
155
+ fieldName: '${pluralQueryName}',
156
+ document,
157
+ variables,
158
+ });`,
159
+ });
160
+ // create method - uses const generic for proper literal type inference
161
+ classDeclaration.addMethod({
162
+ name: 'create',
163
+ typeParameters: [`const S extends ${selectTypeName}`],
164
+ parameters: [
165
+ {
166
+ name: 'args',
167
+ type: `CreateArgs<S, ${createDataType}>`,
168
+ },
169
+ ],
170
+ returnType: `QueryBuilder<{ ${createMutationName}: { ${entityLower}: InferSelectResult<${relationTypeName}, S> } }>`,
171
+ statements: `const { document, variables } = buildCreateDocument(
172
+ '${typeName}',
173
+ '${createMutationName}',
174
+ '${entityLower}',
175
+ args.select,
176
+ args.data,
177
+ '${createInputTypeName}'
178
+ );
179
+ return new QueryBuilder({
180
+ client: this.client,
181
+ operation: 'mutation',
182
+ operationName: '${typeName}',
183
+ fieldName: '${createMutationName}',
184
+ document,
185
+ variables,
186
+ });`,
187
+ });
188
+ // update method (if available) - uses const generic for proper literal type inference
189
+ if (updateMutationName) {
190
+ classDeclaration.addMethod({
191
+ name: 'update',
192
+ typeParameters: [`const S extends ${selectTypeName}`],
193
+ parameters: [
194
+ {
195
+ name: 'args',
196
+ type: `UpdateArgs<S, { id: string }, ${patchTypeName}>`,
197
+ },
198
+ ],
199
+ returnType: `QueryBuilder<{ ${updateMutationName}: { ${entityLower}: InferSelectResult<${relationTypeName}, S> } }>`,
200
+ statements: `const { document, variables } = buildUpdateDocument(
201
+ '${typeName}',
202
+ '${updateMutationName}',
203
+ '${entityLower}',
204
+ args.select,
205
+ args.where,
206
+ args.data,
207
+ '${updateInputTypeName}'
208
+ );
209
+ return new QueryBuilder({
210
+ client: this.client,
211
+ operation: 'mutation',
212
+ operationName: '${typeName}',
213
+ fieldName: '${updateMutationName}',
214
+ document,
215
+ variables,
216
+ });`,
217
+ });
218
+ }
219
+ // delete method (if available)
220
+ if (deleteMutationName) {
221
+ classDeclaration.addMethod({
222
+ name: 'delete',
223
+ parameters: [
224
+ {
225
+ name: 'args',
226
+ type: `DeleteArgs<{ id: string }>`,
227
+ },
228
+ ],
229
+ returnType: `QueryBuilder<{ ${deleteMutationName}: { ${entityLower}: { id: string } } }>`,
230
+ statements: `const { document, variables } = buildDeleteDocument(
231
+ '${typeName}',
232
+ '${deleteMutationName}',
233
+ '${entityLower}',
234
+ args.where,
235
+ '${deleteInputTypeName}'
236
+ );
237
+ return new QueryBuilder({
238
+ client: this.client,
239
+ operation: 'mutation',
240
+ operationName: '${typeName}',
241
+ fieldName: '${deleteMutationName}',
242
+ document,
243
+ variables,
244
+ });`,
245
+ });
246
+ }
247
+ return {
248
+ fileName,
249
+ content: getFormattedOutput(sourceFile),
250
+ modelName,
251
+ tableName: table.name,
252
+ };
253
+ }
254
+ // Select types with relations are now generated in input-types.ts
255
+ /**
256
+ * Generate all model files for a list of tables
257
+ */
258
+ export function generateAllModelFiles(tables, useSharedTypes) {
259
+ return tables.map((table) => generateModelFile(table, useSharedTypes));
260
+ }
@@ -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
+ };