@constructive-io/graphql-codegen 3.2.0 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/README.md +25 -30
  2. package/cli/index.js +36 -41
  3. package/cli/shared.d.ts +15 -17
  4. package/cli/shared.js +113 -21
  5. package/client/error.js +31 -9
  6. package/client/execute.js +2 -2
  7. package/client/index.d.ts +3 -3
  8. package/client/index.js +6 -6
  9. package/core/ast.d.ts +1 -1
  10. package/core/ast.js +1 -1
  11. package/core/codegen/babel-ast.d.ts +1 -1
  12. package/core/codegen/babel-ast.js +2 -2
  13. package/core/codegen/barrel.d.ts +0 -6
  14. package/core/codegen/barrel.js +22 -19
  15. package/core/codegen/client.d.ts +2 -12
  16. package/core/codegen/client.js +7 -21
  17. package/core/codegen/custom-mutations.d.ts +0 -14
  18. package/core/codegen/custom-mutations.js +139 -88
  19. package/core/codegen/custom-queries.d.ts +0 -14
  20. package/core/codegen/custom-queries.js +483 -193
  21. package/core/codegen/hooks-ast.d.ts +75 -0
  22. package/core/codegen/hooks-ast.js +522 -0
  23. package/core/codegen/index.d.ts +16 -18
  24. package/core/codegen/index.js +42 -88
  25. package/core/codegen/invalidation.d.ts +1 -7
  26. package/core/codegen/invalidation.js +50 -16
  27. package/core/codegen/mutation-keys.d.ts +1 -10
  28. package/core/codegen/mutation-keys.js +22 -8
  29. package/core/codegen/mutations.d.ts +0 -13
  30. package/core/codegen/mutations.js +301 -366
  31. package/core/codegen/orm/barrel.d.ts +0 -5
  32. package/core/codegen/orm/barrel.js +5 -0
  33. package/core/codegen/orm/client-generator.d.ts +0 -5
  34. package/core/codegen/orm/client-generator.js +7 -2
  35. package/core/codegen/orm/client.js +3 -1
  36. package/core/codegen/orm/custom-ops-generator.d.ts +0 -6
  37. package/core/codegen/orm/custom-ops-generator.js +104 -51
  38. package/core/codegen/orm/index.d.ts +4 -4
  39. package/core/codegen/orm/index.js +28 -15
  40. package/core/codegen/orm/input-types-generator.d.ts +1 -13
  41. package/core/codegen/orm/input-types-generator.js +85 -23
  42. package/core/codegen/orm/model-generator.d.ts +0 -5
  43. package/core/codegen/orm/model-generator.js +309 -131
  44. package/core/codegen/orm/select-types.d.ts +19 -14
  45. package/core/codegen/queries.d.ts +0 -8
  46. package/core/codegen/queries.js +360 -559
  47. package/core/codegen/query-keys.d.ts +1 -1
  48. package/core/codegen/query-keys.js +37 -23
  49. package/core/codegen/scalars.js +3 -1
  50. package/core/codegen/schema-types-generator.d.ts +1 -1
  51. package/core/codegen/schema-types-generator.js +17 -2
  52. package/core/codegen/select-helpers.d.ts +19 -0
  53. package/core/codegen/select-helpers.js +40 -0
  54. package/core/codegen/selection.d.ts +4 -0
  55. package/core/codegen/selection.js +65 -0
  56. package/core/codegen/shared/index.d.ts +2 -15
  57. package/core/codegen/shared/index.js +17 -4
  58. package/core/codegen/templates/hooks-client.ts +49 -0
  59. package/core/codegen/templates/hooks-selection.ts +58 -0
  60. package/core/codegen/templates/orm-client.ts +8 -6
  61. package/core/codegen/templates/query-builder.ts +250 -46
  62. package/core/codegen/templates/select-types.ts +31 -14
  63. package/core/codegen/type-resolver.d.ts +1 -5
  64. package/core/codegen/type-resolver.js +0 -22
  65. package/core/codegen/types.d.ts +0 -3
  66. package/core/codegen/types.js +71 -14
  67. package/core/codegen/utils.d.ts +1 -4
  68. package/core/codegen/utils.js +4 -1
  69. package/core/config/index.d.ts +1 -1
  70. package/core/config/resolver.js +1 -3
  71. package/core/generate.js +38 -50
  72. package/core/index.d.ts +3 -3
  73. package/core/index.js +3 -4
  74. package/core/introspect/index.d.ts +6 -6
  75. package/core/introspect/index.js +5 -8
  76. package/core/introspect/infer-tables.d.ts +0 -14
  77. package/core/introspect/infer-tables.js +15 -1
  78. package/core/introspect/source/database.js +1 -1
  79. package/core/introspect/source/endpoint.d.ts +0 -6
  80. package/core/introspect/source/endpoint.js +7 -1
  81. package/core/introspect/source/index.d.ts +4 -4
  82. package/core/introspect/source/index.js +5 -9
  83. package/core/introspect/source/pgpm-module.js +3 -3
  84. package/core/introspect/transform-schema.d.ts +2 -2
  85. package/core/introspect/transform-schema.js +2 -2
  86. package/core/output/index.d.ts +1 -1
  87. package/core/output/index.js +2 -2
  88. package/core/output/writer.d.ts +3 -0
  89. package/core/output/writer.js +20 -1
  90. package/core/pipeline/index.d.ts +2 -2
  91. package/core/query-builder.d.ts +2 -2
  92. package/core/query-builder.js +1 -1
  93. package/core/watch/index.d.ts +4 -4
  94. package/core/watch/index.js +9 -9
  95. package/core/watch/orchestrator.js +5 -3
  96. package/esm/cli/index.js +37 -42
  97. package/esm/cli/shared.d.ts +15 -17
  98. package/esm/cli/shared.js +103 -20
  99. package/esm/client/error.js +31 -9
  100. package/esm/client/execute.js +2 -2
  101. package/esm/client/index.d.ts +3 -3
  102. package/esm/client/index.js +3 -3
  103. package/esm/core/ast.d.ts +1 -1
  104. package/esm/core/ast.js +1 -1
  105. package/esm/core/codegen/babel-ast.d.ts +1 -1
  106. package/esm/core/codegen/babel-ast.js +2 -2
  107. package/esm/core/codegen/barrel.d.ts +0 -6
  108. package/esm/core/codegen/barrel.js +23 -20
  109. package/esm/core/codegen/client.d.ts +2 -12
  110. package/esm/core/codegen/client.js +7 -21
  111. package/esm/core/codegen/custom-mutations.d.ts +0 -14
  112. package/esm/core/codegen/custom-mutations.js +141 -90
  113. package/esm/core/codegen/custom-queries.d.ts +0 -14
  114. package/esm/core/codegen/custom-queries.js +486 -196
  115. package/esm/core/codegen/hooks-ast.d.ts +75 -0
  116. package/esm/core/codegen/hooks-ast.js +424 -0
  117. package/esm/core/codegen/index.d.ts +16 -18
  118. package/esm/core/codegen/index.js +26 -71
  119. package/esm/core/codegen/invalidation.d.ts +1 -7
  120. package/esm/core/codegen/invalidation.js +51 -17
  121. package/esm/core/codegen/mutation-keys.d.ts +1 -10
  122. package/esm/core/codegen/mutation-keys.js +23 -9
  123. package/esm/core/codegen/mutations.d.ts +0 -13
  124. package/esm/core/codegen/mutations.js +302 -367
  125. package/esm/core/codegen/orm/barrel.d.ts +0 -5
  126. package/esm/core/codegen/orm/barrel.js +6 -1
  127. package/esm/core/codegen/orm/client-generator.d.ts +0 -5
  128. package/esm/core/codegen/orm/client-generator.js +7 -2
  129. package/esm/core/codegen/orm/client.js +3 -1
  130. package/esm/core/codegen/orm/custom-ops-generator.d.ts +0 -6
  131. package/esm/core/codegen/orm/custom-ops-generator.js +103 -50
  132. package/esm/core/codegen/orm/index.d.ts +4 -4
  133. package/esm/core/codegen/orm/index.js +25 -12
  134. package/esm/core/codegen/orm/input-types-generator.d.ts +1 -13
  135. package/esm/core/codegen/orm/input-types-generator.js +85 -23
  136. package/esm/core/codegen/orm/model-generator.d.ts +0 -5
  137. package/esm/core/codegen/orm/model-generator.js +310 -132
  138. package/esm/core/codegen/orm/select-types.d.ts +19 -14
  139. package/esm/core/codegen/queries.d.ts +0 -8
  140. package/esm/core/codegen/queries.js +362 -561
  141. package/esm/core/codegen/query-keys.d.ts +1 -1
  142. package/esm/core/codegen/query-keys.js +38 -24
  143. package/esm/core/codegen/scalars.js +3 -1
  144. package/esm/core/codegen/schema-types-generator.d.ts +1 -1
  145. package/esm/core/codegen/schema-types-generator.js +17 -2
  146. package/esm/core/codegen/select-helpers.d.ts +19 -0
  147. package/esm/core/codegen/select-helpers.js +35 -0
  148. package/esm/core/codegen/selection.d.ts +4 -0
  149. package/esm/core/codegen/selection.js +29 -0
  150. package/esm/core/codegen/shared/index.d.ts +2 -15
  151. package/esm/core/codegen/shared/index.js +16 -3
  152. package/esm/core/codegen/type-resolver.d.ts +1 -5
  153. package/esm/core/codegen/type-resolver.js +1 -22
  154. package/esm/core/codegen/types.d.ts +0 -3
  155. package/esm/core/codegen/types.js +72 -15
  156. package/esm/core/codegen/utils.d.ts +1 -4
  157. package/esm/core/codegen/utils.js +4 -1
  158. package/esm/core/config/index.d.ts +1 -1
  159. package/esm/core/config/resolver.js +2 -4
  160. package/esm/core/generate.js +38 -50
  161. package/esm/core/index.d.ts +3 -3
  162. package/esm/core/index.js +2 -3
  163. package/esm/core/introspect/index.d.ts +6 -6
  164. package/esm/core/introspect/index.js +3 -6
  165. package/esm/core/introspect/infer-tables.d.ts +0 -14
  166. package/esm/core/introspect/infer-tables.js +16 -2
  167. package/esm/core/introspect/source/database.js +2 -2
  168. package/esm/core/introspect/source/endpoint.d.ts +0 -6
  169. package/esm/core/introspect/source/endpoint.js +7 -1
  170. package/esm/core/introspect/source/index.d.ts +4 -4
  171. package/esm/core/introspect/source/index.js +6 -10
  172. package/esm/core/introspect/source/pgpm-module.js +3 -3
  173. package/esm/core/introspect/transform-schema.d.ts +2 -2
  174. package/esm/core/introspect/transform-schema.js +2 -2
  175. package/esm/core/output/index.d.ts +1 -1
  176. package/esm/core/output/index.js +1 -1
  177. package/esm/core/output/writer.d.ts +3 -0
  178. package/esm/core/output/writer.js +20 -1
  179. package/esm/core/pipeline/index.d.ts +2 -2
  180. package/esm/core/pipeline/index.js +2 -2
  181. package/esm/core/query-builder.d.ts +2 -2
  182. package/esm/core/query-builder.js +2 -2
  183. package/esm/core/watch/index.d.ts +4 -4
  184. package/esm/core/watch/index.js +3 -3
  185. package/esm/core/watch/orchestrator.js +5 -3
  186. package/esm/generators/index.d.ts +3 -3
  187. package/esm/generators/index.js +3 -3
  188. package/esm/generators/mutations.d.ts +1 -1
  189. package/esm/generators/select.d.ts +1 -1
  190. package/esm/index.d.ts +3 -3
  191. package/esm/index.js +1 -4
  192. package/esm/types/config.d.ts +0 -10
  193. package/esm/types/config.js +0 -2
  194. package/esm/types/index.d.ts +6 -6
  195. package/esm/types/index.js +1 -1
  196. package/generators/index.d.ts +3 -3
  197. package/generators/index.js +8 -8
  198. package/generators/mutations.d.ts +1 -1
  199. package/generators/select.d.ts +1 -1
  200. package/index.d.ts +3 -3
  201. package/index.js +11 -5
  202. package/package.json +11 -11
  203. package/types/config.d.ts +0 -10
  204. package/types/config.js +0 -2
  205. package/types/index.d.ts +6 -6
  206. package/types/index.js +2 -2
  207. package/core/codegen/gql-ast.d.ts +0 -41
  208. package/core/codegen/gql-ast.js +0 -353
  209. package/core/codegen/schema-gql-ast.d.ts +0 -51
  210. package/core/codegen/schema-gql-ast.js +0 -385
  211. package/core/codegen/templates/client.browser.ts +0 -271
  212. package/core/codegen/templates/client.node.ts +0 -337
  213. package/esm/core/codegen/gql-ast.d.ts +0 -41
  214. package/esm/core/codegen/gql-ast.js +0 -312
  215. package/esm/core/codegen/schema-gql-ast.d.ts +0 -51
  216. package/esm/core/codegen/schema-gql-ast.js +0 -343
@@ -36,12 +36,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.collectInputTypeNames = collectInputTypeNames;
37
37
  exports.collectPayloadTypeNames = collectPayloadTypeNames;
38
38
  exports.generateInputTypesFile = generateInputTypesFile;
39
+ /**
40
+ * Input types generator for ORM client (Babel AST-based)
41
+ *
42
+ * Generates TypeScript interfaces for:
43
+ * 1. Scalar filter types (StringFilter, IntFilter, UUIDFilter, etc.)
44
+ * 2. Entity interfaces (User, Order, etc.)
45
+ * 3. Table filter types (UserFilter, OrderFilter, etc.)
46
+ * 4. OrderBy enums (UsersOrderBy, OrdersOrderBy, etc.)
47
+ * 5. Input types (LoginInput, CreateUserInput, etc.)
48
+ *
49
+ * Uses Babel AST for robust code generation.
50
+ */
39
51
  const t = __importStar(require("@babel/types"));
40
- const babel_ast_1 = require("../babel-ast");
41
- const utils_1 = require("../utils");
42
52
  const inflekt_1 = require("inflekt");
43
- const type_resolver_1 = require("../type-resolver");
53
+ const babel_ast_1 = require("../babel-ast");
44
54
  const scalars_1 = require("../scalars");
55
+ const type_resolver_1 = require("../type-resolver");
56
+ const utils_1 = require("../utils");
45
57
  // ============================================================================
46
58
  // Constants
47
59
  // ============================================================================
@@ -118,7 +130,9 @@ function parseTypeString(typeStr) {
118
130
  const match = typeStr.match(/^([^<]+)<(.+)>$/);
119
131
  if (match) {
120
132
  const [, baseName, params] = match;
121
- const typeParams = params.split(',').map((p) => parseTypeString(p.trim()));
133
+ const typeParams = params
134
+ .split(',')
135
+ .map((p) => parseTypeString(p.trim()));
122
136
  return t.tsTypeReference(t.identifier(baseName), t.tsTypeParameterInstantiation(typeParams));
123
137
  }
124
138
  }
@@ -402,9 +416,7 @@ function generateRelationHelperTypes() {
402
416
  createPropertySignature('pageInfo', 'PageInfo', false),
403
417
  ];
404
418
  const connectionResultBody = t.tsInterfaceBody(connectionResultProps);
405
- const connectionResultDecl = t.tsInterfaceDeclaration(t.identifier('ConnectionResult'), t.tsTypeParameterDeclaration([
406
- t.tsTypeParameter(null, null, 'T'),
407
- ]), null, connectionResultBody);
419
+ const connectionResultDecl = t.tsInterfaceDeclaration(t.identifier('ConnectionResult'), t.tsTypeParameterDeclaration([t.tsTypeParameter(null, null, 'T')]), null, connectionResultBody);
408
420
  statements.push(t.exportNamedDeclaration(connectionResultDecl));
409
421
  // PageInfo interface
410
422
  statements.push(createExportedInterface('PageInfo', [
@@ -839,6 +851,10 @@ function generateCrudInputTypes(table) {
839
851
  const statements = [];
840
852
  const { typeName } = (0, utils_1.getTableNames)(table);
841
853
  const patchName = `${typeName}Patch`;
854
+ const pkFields = (0, utils_1.getPrimaryKeyInfo)(table);
855
+ const pkField = pkFields[0];
856
+ const pkFieldName = pkField?.name ?? 'id';
857
+ const pkFieldTsType = pkField?.tsType ?? 'string';
842
858
  // Create input
843
859
  statements.push(buildCreateInputInterface(table));
844
860
  // Patch interface
@@ -846,13 +862,13 @@ function generateCrudInputTypes(table) {
846
862
  // Update input
847
863
  statements.push(createExportedInterface(`Update${typeName}Input`, [
848
864
  { name: 'clientMutationId', type: 'string', optional: true },
849
- { name: 'id', type: 'string', optional: false },
865
+ { name: pkFieldName, type: pkFieldTsType, optional: false },
850
866
  { name: 'patch', type: patchName, optional: false },
851
867
  ]));
852
868
  // Delete input
853
869
  statements.push(createExportedInterface(`Delete${typeName}Input`, [
854
870
  { name: 'clientMutationId', type: 'string', optional: true },
855
- { name: 'id', type: 'string', optional: false },
871
+ { name: pkFieldName, type: pkFieldTsType, optional: false },
856
872
  ]));
857
873
  return statements;
858
874
  }
@@ -987,8 +1003,7 @@ function collectPayloadTypeNames(operations) {
987
1003
  const payloadTypes = new Set();
988
1004
  for (const op of operations) {
989
1005
  const baseName = (0, type_resolver_1.getTypeBaseName)(op.returnType);
990
- if (baseName &&
991
- (baseName.endsWith('Payload') || !baseName.endsWith('Connection'))) {
1006
+ if (baseName) {
992
1007
  payloadTypes.add(baseName);
993
1008
  }
994
1009
  }
@@ -1093,6 +1108,48 @@ function generatePayloadTypes(typeRegistry, usedPayloadTypes, alreadyGeneratedTy
1093
1108
  return statements;
1094
1109
  }
1095
1110
  // ============================================================================
1111
+ // Connection Fields Map Generator
1112
+ // ============================================================================
1113
+ /**
1114
+ * Generate a runtime map of entity type → { fieldName: relatedTypeName }
1115
+ * for all hasMany and manyToMany relations. Used by buildSelections()
1116
+ * to detect connection fields that need `nodes { ... }` wrapping.
1117
+ */
1118
+ function generateConnectionFieldsMap(tables, tableByName) {
1119
+ const properties = [];
1120
+ for (const table of tables) {
1121
+ const { typeName } = (0, utils_1.getTableNames)(table);
1122
+ const fieldEntries = [];
1123
+ for (const relation of table.relations.hasMany) {
1124
+ if (!relation.fieldName)
1125
+ continue;
1126
+ const relatedTypeName = getRelatedTypeName(relation.referencedByTable, tableByName);
1127
+ fieldEntries.push(t.objectProperty(t.stringLiteral(relation.fieldName), t.stringLiteral(relatedTypeName)));
1128
+ }
1129
+ for (const relation of table.relations.manyToMany) {
1130
+ if (!relation.fieldName)
1131
+ continue;
1132
+ const relatedTypeName = getRelatedTypeName(relation.rightTable, tableByName);
1133
+ fieldEntries.push(t.objectProperty(t.stringLiteral(relation.fieldName), t.stringLiteral(relatedTypeName)));
1134
+ }
1135
+ if (fieldEntries.length > 0) {
1136
+ properties.push(t.objectProperty(t.stringLiteral(typeName), t.objectExpression(fieldEntries)));
1137
+ }
1138
+ }
1139
+ const decl = t.variableDeclaration('const', [
1140
+ t.variableDeclarator(t.identifier('connectionFieldsMap'), t.tsAsExpression(t.objectExpression(properties), t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
1141
+ t.tsStringKeyword(),
1142
+ t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
1143
+ t.tsStringKeyword(),
1144
+ t.tsStringKeyword(),
1145
+ ])),
1146
+ ])))),
1147
+ ]);
1148
+ const statements = [t.exportNamedDeclaration(decl)];
1149
+ addSectionComment(statements, 'Connection Fields Map');
1150
+ return statements;
1151
+ }
1152
+ // ============================================================================
1096
1153
  // Main Generator (AST-based)
1097
1154
  // ============================================================================
1098
1155
  /**
@@ -1100,30 +1157,35 @@ function generatePayloadTypes(typeRegistry, usedPayloadTypes, alreadyGeneratedTy
1100
1157
  */
1101
1158
  function generateInputTypesFile(typeRegistry, usedInputTypes, tables, usedPayloadTypes) {
1102
1159
  const statements = [];
1160
+ const tablesList = tables ?? [];
1161
+ const hasTables = tablesList.length > 0;
1162
+ const tableByName = new Map(tablesList.map((table) => [table.name, table]));
1103
1163
  // 1. Scalar filter types
1104
1164
  statements.push(...generateScalarFilterTypes());
1105
1165
  // 2. Enum types used by table fields
1106
- if (tables && tables.length > 0) {
1107
- const enumTypes = collectEnumTypesFromTables(tables, typeRegistry);
1166
+ if (hasTables) {
1167
+ const enumTypes = collectEnumTypesFromTables(tablesList, typeRegistry);
1108
1168
  statements.push(...generateEnumTypes(typeRegistry, enumTypes));
1109
1169
  }
1110
1170
  // 3. Entity and relation types (if tables provided)
1111
- if (tables && tables.length > 0) {
1112
- const tableByName = new Map(tables.map((table) => [table.name, table]));
1113
- statements.push(...generateEntityTypes(tables));
1171
+ if (hasTables) {
1172
+ statements.push(...generateEntityTypes(tablesList));
1114
1173
  statements.push(...generateRelationHelperTypes());
1115
- statements.push(...generateEntityRelationTypes(tables, tableByName));
1116
- statements.push(...generateEntityWithRelations(tables));
1117
- statements.push(...generateEntitySelectTypes(tables, tableByName));
1174
+ statements.push(...generateEntityRelationTypes(tablesList, tableByName));
1175
+ statements.push(...generateEntityWithRelations(tablesList));
1176
+ statements.push(...generateEntitySelectTypes(tablesList, tableByName));
1118
1177
  // 4. Table filter types
1119
- statements.push(...generateTableFilterTypes(tables));
1178
+ statements.push(...generateTableFilterTypes(tablesList));
1120
1179
  // 4b. Table condition types (simple equality filter)
1121
- statements.push(...generateTableConditionTypes(tables));
1180
+ statements.push(...generateTableConditionTypes(tablesList));
1122
1181
  // 5. OrderBy types
1123
- statements.push(...generateOrderByTypes(tables));
1182
+ statements.push(...generateOrderByTypes(tablesList));
1124
1183
  // 6. CRUD input types
1125
- statements.push(...generateAllCrudInputTypes(tables));
1184
+ statements.push(...generateAllCrudInputTypes(tablesList));
1126
1185
  }
1186
+ // 6b. Connection fields map (runtime metadata for buildSelections)
1187
+ // Always emit this export so generated model/custom-op imports stay valid.
1188
+ statements.push(...generateConnectionFieldsMap(tablesList, tableByName));
1127
1189
  // 7. Custom input types from TypeRegistry
1128
1190
  const tableCrudTypes = tables ? buildTableCrudTypeNames(tables) : undefined;
1129
1191
  statements.push(...generateCustomInputTypes(typeRegistry, usedInputTypes, tableCrudTypes));
@@ -1,8 +1,3 @@
1
- /**
2
- * Model class generator for ORM client (Babel AST-based)
3
- *
4
- * Generates per-table model classes with findMany, findFirst, create, update, delete methods.
5
- */
6
1
  import type { CleanTable } from '../../../types/schema';
7
2
  export interface GeneratedModelFile {
8
3
  fileName: string;