@constructive-io/graphql-codegen 4.6.0 → 4.7.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 (75) hide show
  1. package/client/error.d.ts +2 -93
  2. package/client/error.js +9 -273
  3. package/client/execute.d.ts +2 -55
  4. package/client/execute.js +5 -120
  5. package/client/typed-document.d.ts +2 -29
  6. package/client/typed-document.js +3 -39
  7. package/core/ast.d.ts +8 -10
  8. package/core/ast.js +17 -592
  9. package/core/custom-ast.d.ts +5 -33
  10. package/core/custom-ast.js +16 -203
  11. package/core/introspect/infer-tables.d.ts +2 -40
  12. package/core/introspect/infer-tables.js +4 -653
  13. package/core/introspect/schema-query.d.ts +3 -18
  14. package/core/introspect/schema-query.js +3 -118
  15. package/core/introspect/transform-schema.d.ts +2 -84
  16. package/core/introspect/transform-schema.js +14 -279
  17. package/core/introspect/transform.d.ts +2 -18
  18. package/core/introspect/transform.js +6 -39
  19. package/core/meta-object/convert.d.ts +2 -63
  20. package/core/meta-object/convert.js +4 -59
  21. package/core/meta-object/validate.d.ts +2 -7
  22. package/core/meta-object/validate.js +4 -30
  23. package/core/query-builder.d.ts +7 -46
  24. package/core/query-builder.js +8 -408
  25. package/core/types.d.ts +9 -139
  26. package/core/types.js +12 -26
  27. package/esm/client/error.d.ts +2 -93
  28. package/esm/client/error.js +2 -269
  29. package/esm/client/execute.d.ts +2 -55
  30. package/esm/client/execute.js +2 -118
  31. package/esm/client/typed-document.d.ts +2 -29
  32. package/esm/client/typed-document.js +2 -38
  33. package/esm/core/ast.d.ts +8 -10
  34. package/esm/core/ast.js +8 -550
  35. package/esm/core/custom-ast.d.ts +5 -33
  36. package/esm/core/custom-ast.js +5 -160
  37. package/esm/core/introspect/infer-tables.d.ts +2 -40
  38. package/esm/core/introspect/infer-tables.js +2 -652
  39. package/esm/core/introspect/schema-query.d.ts +3 -18
  40. package/esm/core/introspect/schema-query.js +2 -118
  41. package/esm/core/introspect/transform-schema.d.ts +2 -84
  42. package/esm/core/introspect/transform-schema.js +2 -269
  43. package/esm/core/introspect/transform.d.ts +2 -18
  44. package/esm/core/introspect/transform.js +2 -36
  45. package/esm/core/meta-object/convert.d.ts +2 -63
  46. package/esm/core/meta-object/convert.js +2 -58
  47. package/esm/core/meta-object/validate.d.ts +2 -7
  48. package/esm/core/meta-object/validate.js +2 -26
  49. package/esm/core/query-builder.d.ts +7 -46
  50. package/esm/core/query-builder.js +5 -373
  51. package/esm/core/types.d.ts +9 -139
  52. package/esm/core/types.js +9 -24
  53. package/esm/generators/field-selector.d.ts +5 -28
  54. package/esm/generators/field-selector.js +5 -354
  55. package/esm/generators/mutations.d.ts +5 -29
  56. package/esm/generators/mutations.js +5 -195
  57. package/esm/generators/naming-helpers.d.ts +6 -0
  58. package/esm/generators/naming-helpers.js +6 -0
  59. package/esm/generators/select.d.ts +6 -48
  60. package/esm/generators/select.js +6 -634
  61. package/esm/types/query.d.ts +9 -0
  62. package/esm/types/schema.d.ts +4 -0
  63. package/generators/field-selector.d.ts +5 -28
  64. package/generators/field-selector.js +12 -360
  65. package/generators/mutations.d.ts +5 -29
  66. package/generators/mutations.js +9 -231
  67. package/generators/naming-helpers.d.ts +6 -0
  68. package/generators/naming-helpers.js +20 -0
  69. package/generators/select.d.ts +6 -48
  70. package/generators/select.js +17 -677
  71. package/package.json +7 -6
  72. package/types/query.d.ts +9 -0
  73. package/types/schema.d.ts +4 -0
  74. package/core/meta-object/format.json +0 -93
  75. package/esm/core/meta-object/format.json +0 -93
@@ -1,679 +1,19 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
2
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.toCamelCasePlural = toCamelCasePlural;
37
- exports.toOrderByTypeName = toOrderByTypeName;
38
- exports.cleanTableToMetaObject = cleanTableToMetaObject;
39
- exports.generateIntrospectionSchema = generateIntrospectionSchema;
40
- exports.createASTQueryBuilder = createASTQueryBuilder;
41
- exports.buildSelect = buildSelect;
42
- exports.buildFindOne = buildFindOne;
43
- exports.buildCount = buildCount;
44
- /**
45
- * Query generators for SELECT, FindOne, and Count operations
46
- * Uses AST-based approach for all query generation
47
- */
48
- const t = __importStar(require("gql-ast"));
49
- const graphql_1 = require("graphql");
50
- const inflekt_1 = require("inflekt");
51
- const typed_document_1 = require("../client/typed-document");
52
- const custom_ast_1 = require("../core/custom-ast");
53
- const query_builder_1 = require("../core/query-builder");
54
- const field_selector_1 = require("./field-selector");
55
- /**
56
- * Convert PascalCase table name to camelCase plural for GraphQL queries
57
- * Uses the inflection library for proper pluralization
58
- * Example: "ActionGoal" -> "actionGoals", "User" -> "users", "Person" -> "people"
59
- */
60
- function toCamelCasePlural(tableName) {
61
- // First convert to camelCase (lowercase first letter)
62
- const camelCase = (0, inflekt_1.camelize)(tableName, true);
63
- // Then pluralize properly
64
- return (0, inflekt_1.pluralize)(camelCase);
65
- }
66
- /**
67
- * Generate the PostGraphile OrderBy enum type name for a table
68
- * PostGraphile uses pluralized PascalCase: "Product" -> "ProductsOrderBy"
69
- * Example: "Product" -> "ProductsOrderBy", "Person" -> "PeopleOrderBy"
70
- */
71
- function toOrderByTypeName(tableName) {
72
- const plural = toCamelCasePlural(tableName); // "products", "people"
73
- // Capitalize first letter for PascalCase
74
- return `${plural.charAt(0).toUpperCase() + plural.slice(1)}OrderBy`;
75
- }
76
- /**
77
- * Convert CleanTable to MetaObject format for QueryBuilder
78
- */
79
- function cleanTableToMetaObject(tables) {
80
- return {
81
- tables: tables.map((table) => ({
82
- name: table.name,
83
- fields: table.fields.map((field) => ({
84
- name: field.name,
85
- type: {
86
- gqlType: field.type.gqlType,
87
- isArray: field.type.isArray,
88
- modifier: field.type.modifier,
89
- pgAlias: field.type.pgAlias,
90
- pgType: field.type.pgType,
91
- subtype: field.type.subtype,
92
- typmod: field.type.typmod,
93
- },
94
- })),
95
- primaryConstraints: [], // Would need to be derived from schema
96
- uniqueConstraints: [], // Would need to be derived from schema
97
- foreignConstraints: table.relations.belongsTo.map((rel) => ({
98
- refTable: rel.referencesTable,
99
- fromKey: {
100
- name: rel.fieldName || '',
101
- type: {
102
- gqlType: 'UUID', // Default, should be derived from actual field
103
- isArray: false,
104
- modifier: null,
105
- pgAlias: null,
106
- pgType: null,
107
- subtype: null,
108
- typmod: null,
109
- },
110
- alias: rel.fieldName || '',
111
- },
112
- toKey: {
113
- name: 'id',
114
- type: {
115
- gqlType: 'UUID',
116
- isArray: false,
117
- modifier: null,
118
- pgAlias: null,
119
- pgType: null,
120
- subtype: null,
121
- typmod: null,
122
- },
123
- },
124
- })),
125
- })),
126
- };
127
- }
128
- /**
129
- * Generate basic IntrospectionSchema from CleanTable array
130
- * This creates a minimal schema for AST generation
131
- */
132
- function generateIntrospectionSchema(tables) {
133
- const schema = {};
134
- for (const table of tables) {
135
- const modelName = table.name;
136
- const pluralName = toCamelCasePlural(modelName);
137
- // Basic field selection for the model
138
- const selection = table.fields.map((field) => field.name);
139
- // Add getMany query
140
- schema[pluralName] = {
141
- qtype: 'getMany',
142
- model: modelName,
143
- selection,
144
- properties: convertFieldsToProperties(table.fields),
145
- };
146
- // Add getOne query (by ID)
147
- const singularName = (0, inflekt_1.camelize)(modelName, true);
148
- schema[singularName] = {
149
- qtype: 'getOne',
150
- model: modelName,
151
- selection,
152
- properties: convertFieldsToProperties(table.fields),
153
- };
154
- // Add create mutation
155
- schema[`create${modelName}`] = {
156
- qtype: 'mutation',
157
- mutationType: 'create',
158
- model: modelName,
159
- selection,
160
- properties: {
161
- input: {
162
- name: 'input',
163
- type: `Create${modelName}Input`,
164
- isNotNull: true,
165
- isArray: false,
166
- isArrayNotNull: false,
167
- properties: {
168
- [(0, inflekt_1.camelize)(modelName, true)]: {
169
- name: (0, inflekt_1.camelize)(modelName, true),
170
- type: `${modelName}Input`,
171
- isNotNull: true,
172
- isArray: false,
173
- isArrayNotNull: false,
174
- properties: convertFieldsToNestedProperties(table.fields),
175
- },
176
- },
177
- },
178
- },
179
- };
180
- // Add update mutation
181
- schema[`update${modelName}`] = {
182
- qtype: 'mutation',
183
- mutationType: 'patch',
184
- model: modelName,
185
- selection,
186
- properties: {
187
- input: {
188
- name: 'input',
189
- type: `Update${modelName}Input`,
190
- isNotNull: true,
191
- isArray: false,
192
- isArrayNotNull: false,
193
- properties: {
194
- patch: {
195
- name: 'patch',
196
- type: `${modelName}Patch`,
197
- isNotNull: true,
198
- isArray: false,
199
- isArrayNotNull: false,
200
- properties: convertFieldsToNestedProperties(table.fields),
201
- },
202
- },
203
- },
204
- },
205
- };
206
- // Add delete mutation
207
- schema[`delete${modelName}`] = {
208
- qtype: 'mutation',
209
- mutationType: 'delete',
210
- model: modelName,
211
- selection,
212
- properties: {
213
- input: {
214
- name: 'input',
215
- type: `Delete${modelName}Input`,
216
- isNotNull: true,
217
- isArray: false,
218
- isArrayNotNull: false,
219
- properties: {
220
- id: {
221
- name: 'id',
222
- type: 'UUID',
223
- isNotNull: true,
224
- isArray: false,
225
- isArrayNotNull: false,
226
- },
227
- },
228
- },
229
- },
230
- };
231
- }
232
- return schema;
233
- }
234
- /**
235
- * Convert CleanTable fields to QueryBuilder properties
236
- */
237
- function convertFieldsToProperties(fields) {
238
- const properties = {};
239
- fields.forEach((field) => {
240
- properties[field.name] = {
241
- name: field.name,
242
- type: field.type.gqlType,
243
- isNotNull: !field.type.gqlType.endsWith('!'),
244
- isArray: field.type.isArray,
245
- isArrayNotNull: false,
246
- };
247
- });
248
- return properties;
249
- }
250
- /**
251
- * Convert fields to nested properties for mutations
252
- */
253
- function convertFieldsToNestedProperties(fields) {
254
- const properties = {};
255
- fields.forEach((field) => {
256
- properties[field.name] = {
257
- name: field.name,
258
- type: field.type.gqlType,
259
- isNotNull: false, // Mutations typically allow optional fields
260
- isArray: field.type.isArray,
261
- isArrayNotNull: false,
262
- };
263
- });
264
- return properties;
265
- }
266
- /**
267
- * Create AST-based query builder for a table
268
- */
269
- function createASTQueryBuilder(tables) {
270
- const metaObject = cleanTableToMetaObject(tables);
271
- const introspectionSchema = generateIntrospectionSchema(tables);
272
- return new query_builder_1.QueryBuilder({
273
- meta: metaObject,
274
- introspection: introspectionSchema,
275
- });
276
- }
277
- /**
278
- * Build a SELECT query for a table with optional filtering, sorting, and pagination
279
- * Uses direct AST generation without intermediate conversions
280
- */
281
- function buildSelect(table, allTables, options = {}) {
282
- const tableList = Array.from(allTables);
283
- const selection = convertFieldSelectionToSelectionOptions(table, tableList, options.fieldSelection);
284
- // Generate query directly using AST
285
- const queryString = generateSelectQueryAST(table, tableList, selection, options);
286
- return new typed_document_1.TypedDocumentString(queryString, {});
287
- }
288
- /**
289
- * Build a single row query by primary key or unique field
290
- */
291
- function buildFindOne(table, _pkField = 'id') {
292
- const queryString = generateFindOneQueryAST(table);
293
- return new typed_document_1.TypedDocumentString(queryString, {});
294
- }
295
- /**
296
- * Build a count query for a table
297
- */
298
- function buildCount(table) {
299
- const queryString = generateCountQueryAST(table);
300
- return new typed_document_1.TypedDocumentString(queryString, {});
301
- }
302
- function convertFieldSelectionToSelectionOptions(table, allTables, options) {
303
- return (0, field_selector_1.convertToSelectionOptions)(table, allTables, options);
304
- }
305
- /**
306
- * Generate SELECT query AST directly from CleanTable
307
- */
308
- function generateSelectQueryAST(table, allTables, selection, options) {
309
- const pluralName = toCamelCasePlural(table.name);
310
- // Generate field selections
311
- const fieldSelections = generateFieldSelectionsFromOptions(table, allTables, selection);
312
- // Build the query AST
313
- const variableDefinitions = [];
314
- const queryArgs = [];
315
- // Add pagination variables if needed
316
- const limitValue = options.limit ?? options.first;
317
- if (limitValue !== undefined) {
318
- variableDefinitions.push(t.variableDefinition({
319
- variable: t.variable({ name: 'first' }),
320
- type: t.namedType({ type: 'Int' }),
321
- }));
322
- queryArgs.push(t.argument({
323
- name: 'first',
324
- value: t.variable({ name: 'first' }),
325
- }));
326
- }
327
- if (options.offset !== undefined) {
328
- variableDefinitions.push(t.variableDefinition({
329
- variable: t.variable({ name: 'offset' }),
330
- type: t.namedType({ type: 'Int' }),
331
- }));
332
- queryArgs.push(t.argument({
333
- name: 'offset',
334
- value: t.variable({ name: 'offset' }),
335
- }));
336
- }
337
- // Add cursor-based pagination variables if needed (for infinite scroll)
338
- if (options.after !== undefined) {
339
- variableDefinitions.push(t.variableDefinition({
340
- variable: t.variable({ name: 'after' }),
341
- type: t.namedType({ type: 'Cursor' }),
342
- }));
343
- queryArgs.push(t.argument({
344
- name: 'after',
345
- value: t.variable({ name: 'after' }),
346
- }));
347
- }
348
- if (options.before !== undefined) {
349
- variableDefinitions.push(t.variableDefinition({
350
- variable: t.variable({ name: 'before' }),
351
- type: t.namedType({ type: 'Cursor' }),
352
- }));
353
- queryArgs.push(t.argument({
354
- name: 'before',
355
- value: t.variable({ name: 'before' }),
356
- }));
357
- }
358
- // Add filter variables if needed
359
- if (options.where) {
360
- variableDefinitions.push(t.variableDefinition({
361
- variable: t.variable({ name: 'filter' }),
362
- type: t.namedType({ type: `${table.name}Filter` }),
363
- }));
364
- queryArgs.push(t.argument({
365
- name: 'filter',
366
- value: t.variable({ name: 'filter' }),
367
- }));
368
- }
369
- // Add orderBy variables if needed
370
- if (options.orderBy && options.orderBy.length > 0) {
371
- variableDefinitions.push(t.variableDefinition({
372
- variable: t.variable({ name: 'orderBy' }),
373
- // PostGraphile expects [ProductsOrderBy!] - list of non-null enum values
374
- type: t.listType({
375
- type: t.nonNullType({
376
- type: t.namedType({ type: toOrderByTypeName(table.name) }),
377
- }),
378
- }),
379
- }));
380
- queryArgs.push(t.argument({
381
- name: 'orderBy',
382
- value: t.variable({ name: 'orderBy' }),
383
- }));
384
- }
385
- // Build connection selections: totalCount, nodes, and optionally pageInfo
386
- const connectionSelections = [
387
- t.field({ name: 'totalCount' }),
388
- t.field({
389
- name: 'nodes',
390
- selectionSet: t.selectionSet({
391
- selections: fieldSelections,
392
- }),
393
- }),
394
- ];
395
- // Add pageInfo if requested (for cursor-based pagination / infinite scroll)
396
- if (options.includePageInfo ||
397
- options.after !== undefined ||
398
- options.before !== undefined) {
399
- connectionSelections.push(t.field({
400
- name: 'pageInfo',
401
- selectionSet: t.selectionSet({
402
- selections: [
403
- t.field({ name: 'hasNextPage' }),
404
- t.field({ name: 'hasPreviousPage' }),
405
- t.field({ name: 'startCursor' }),
406
- t.field({ name: 'endCursor' }),
407
- ],
408
- }),
409
- }));
410
- }
411
- const ast = t.document({
412
- definitions: [
413
- t.operationDefinition({
414
- operation: graphql_1.OperationTypeNode.QUERY,
415
- name: `${pluralName}Query`,
416
- variableDefinitions,
417
- selectionSet: t.selectionSet({
418
- selections: [
419
- t.field({
420
- name: pluralName,
421
- args: queryArgs,
422
- selectionSet: t.selectionSet({
423
- selections: connectionSelections,
424
- }),
425
- }),
426
- ],
427
- }),
428
- }),
429
- ],
430
- });
431
- return (0, graphql_1.print)(ast);
432
- }
433
- /**
434
- * Generate field selections from SelectionOptions
435
- */
436
- function generateFieldSelectionsFromOptions(table, allTables, selection) {
437
- const DEFAULT_NESTED_RELATION_FIRST = 20;
438
- if (!selection) {
439
- // Default to all non-relational fields (includes complex fields like JSON, geometry, etc.)
440
- return table.fields
441
- .filter((field) => !(0, field_selector_1.isRelationalField)(field.name, table))
442
- .map((field) => {
443
- if ((0, custom_ast_1.requiresSubfieldSelection)(field)) {
444
- // For complex fields that require subfield selection, use custom AST generation
445
- return (0, custom_ast_1.getCustomAstForCleanField)(field);
446
- }
447
- else {
448
- // For simple fields, use basic field selection
449
- return t.field({ name: field.name });
450
- }
451
- });
452
- }
453
- const fieldSelections = [];
454
- Object.entries(selection).forEach(([fieldName, fieldOptions]) => {
455
- if (fieldOptions === true) {
456
- // Check if this field requires subfield selection
457
- const field = table.fields.find((f) => f.name === fieldName);
458
- if (field && (0, custom_ast_1.requiresSubfieldSelection)(field)) {
459
- // Use custom AST generation for complex fields
460
- fieldSelections.push((0, custom_ast_1.getCustomAstForCleanField)(field));
461
- }
462
- else {
463
- // Simple field selection for scalar fields
464
- fieldSelections.push(t.field({ name: fieldName }));
465
- }
466
- }
467
- else if (typeof fieldOptions === 'object' && fieldOptions.select) {
468
- // Nested field selection (for relation fields)
469
- const nestedSelections = [];
470
- // Find the related table to check for complex fields
471
- const relatedTable = findRelatedTable(fieldName, table, allTables);
472
- Object.entries(fieldOptions.select).forEach(([nestedField, include]) => {
473
- if (include) {
474
- // Check if this nested field requires subfield selection
475
- const nestedFieldDef = relatedTable?.fields.find((f) => f.name === nestedField);
476
- if (nestedFieldDef && (0, custom_ast_1.requiresSubfieldSelection)(nestedFieldDef)) {
477
- // Use custom AST generation for complex nested fields
478
- nestedSelections.push((0, custom_ast_1.getCustomAstForCleanField)(nestedFieldDef));
479
- }
480
- else {
481
- // Simple field selection for scalar nested fields
482
- nestedSelections.push(t.field({ name: nestedField }));
483
- }
484
- }
485
- });
486
- // Check if this is a hasMany relation that uses Connection pattern
487
- const relationInfo = getRelationInfo(fieldName, table);
488
- if (relationInfo &&
489
- (relationInfo.type === 'hasMany' || relationInfo.type === 'manyToMany')) {
490
- // For hasMany/manyToMany relations, wrap selections in nodes { ... }
491
- fieldSelections.push(t.field({
492
- name: fieldName,
493
- args: [
494
- t.argument({
495
- name: 'first',
496
- value: t.intValue({
497
- value: DEFAULT_NESTED_RELATION_FIRST.toString(),
498
- }),
499
- }),
500
- ],
501
- selectionSet: t.selectionSet({
502
- selections: [
503
- t.field({
504
- name: 'nodes',
505
- selectionSet: t.selectionSet({
506
- selections: nestedSelections,
507
- }),
508
- }),
509
- ],
510
- }),
511
- }));
512
- }
513
- else {
514
- // For belongsTo/hasOne relations, use direct selection
515
- fieldSelections.push(t.field({
516
- name: fieldName,
517
- selectionSet: t.selectionSet({
518
- selections: nestedSelections,
519
- }),
520
- }));
521
- }
522
- }
523
- });
524
- return fieldSelections;
525
- }
526
- /**
527
- * Get relation information for a field
528
- */
529
- function getRelationInfo(fieldName, table) {
530
- const { belongsTo, hasOne, hasMany, manyToMany } = table.relations;
531
- // Check belongsTo relations
532
- const belongsToRel = belongsTo.find((rel) => rel.fieldName === fieldName);
533
- if (belongsToRel) {
534
- return { type: 'belongsTo', relation: belongsToRel };
535
- }
536
- // Check hasOne relations
537
- const hasOneRel = hasOne.find((rel) => rel.fieldName === fieldName);
538
- if (hasOneRel) {
539
- return { type: 'hasOne', relation: hasOneRel };
540
- }
541
- // Check hasMany relations
542
- const hasManyRel = hasMany.find((rel) => rel.fieldName === fieldName);
543
- if (hasManyRel) {
544
- return { type: 'hasMany', relation: hasManyRel };
545
- }
546
- // Check manyToMany relations
547
- const manyToManyRel = manyToMany.find((rel) => rel.fieldName === fieldName);
548
- if (manyToManyRel) {
549
- return { type: 'manyToMany', relation: manyToManyRel };
550
- }
551
- return null;
552
- }
553
- /**
554
- * Find the related table for a given relation field
555
- */
556
- function findRelatedTable(relationField, table, allTables) {
557
- // Find the related table name
558
- let referencedTableName;
559
- // Check belongsTo relations
560
- const belongsToRel = table.relations.belongsTo.find((rel) => rel.fieldName === relationField);
561
- if (belongsToRel) {
562
- referencedTableName = belongsToRel.referencesTable;
563
- }
564
- // Check hasOne relations
565
- if (!referencedTableName) {
566
- const hasOneRel = table.relations.hasOne.find((rel) => rel.fieldName === relationField);
567
- if (hasOneRel) {
568
- referencedTableName = hasOneRel.referencedByTable;
569
- }
570
- }
571
- // Check hasMany relations
572
- if (!referencedTableName) {
573
- const hasManyRel = table.relations.hasMany.find((rel) => rel.fieldName === relationField);
574
- if (hasManyRel) {
575
- referencedTableName = hasManyRel.referencedByTable;
576
- }
577
- }
578
- // Check manyToMany relations
579
- if (!referencedTableName) {
580
- const manyToManyRel = table.relations.manyToMany.find((rel) => rel.fieldName === relationField);
581
- if (manyToManyRel) {
582
- referencedTableName = manyToManyRel.rightTable;
583
- }
584
- }
585
- if (!referencedTableName) {
586
- return null;
587
- }
588
- // Find the related table in allTables
589
- return allTables.find((tbl) => tbl.name === referencedTableName) || null;
590
- }
591
- /**
592
- * Generate FindOne query AST directly from CleanTable
593
- */
594
- function generateFindOneQueryAST(table) {
595
- const singularName = (0, inflekt_1.camelize)(table.name, true);
596
- // Generate field selections (include all non-relational fields, including complex types)
597
- const fieldSelections = table.fields
598
- .filter((field) => !(0, field_selector_1.isRelationalField)(field.name, table))
599
- .map((field) => {
600
- if ((0, custom_ast_1.requiresSubfieldSelection)(field)) {
601
- // For complex fields that require subfield selection, use custom AST generation
602
- return (0, custom_ast_1.getCustomAstForCleanField)(field);
603
- }
604
- else {
605
- // For simple fields, use basic field selection
606
- return t.field({ name: field.name });
607
- }
608
- });
609
- const ast = t.document({
610
- definitions: [
611
- t.operationDefinition({
612
- operation: graphql_1.OperationTypeNode.QUERY,
613
- name: `${singularName}Query`,
614
- variableDefinitions: [
615
- t.variableDefinition({
616
- variable: t.variable({ name: 'id' }),
617
- type: t.nonNullType({
618
- type: t.namedType({ type: 'UUID' }),
619
- }),
620
- }),
621
- ],
622
- selectionSet: t.selectionSet({
623
- selections: [
624
- t.field({
625
- name: singularName,
626
- args: [
627
- t.argument({
628
- name: 'id',
629
- value: t.variable({ name: 'id' }),
630
- }),
631
- ],
632
- selectionSet: t.selectionSet({
633
- selections: fieldSelections,
634
- }),
635
- }),
636
- ],
637
- }),
638
- }),
639
- ],
640
- });
641
- return (0, graphql_1.print)(ast);
642
- }
643
- /**
644
- * Generate Count query AST directly from CleanTable
645
- */
646
- function generateCountQueryAST(table) {
647
- const pluralName = toCamelCasePlural(table.name);
648
- const ast = t.document({
649
- definitions: [
650
- t.operationDefinition({
651
- operation: graphql_1.OperationTypeNode.QUERY,
652
- name: `${pluralName}CountQuery`,
653
- variableDefinitions: [
654
- t.variableDefinition({
655
- variable: t.variable({ name: 'filter' }),
656
- type: t.namedType({ type: `${table.name}Filter` }),
657
- }),
658
- ],
659
- selectionSet: t.selectionSet({
660
- selections: [
661
- t.field({
662
- name: pluralName,
663
- args: [
664
- t.argument({
665
- name: 'filter',
666
- value: t.variable({ name: 'filter' }),
667
- }),
668
- ],
669
- selectionSet: t.selectionSet({
670
- selections: [t.field({ name: 'totalCount' })],
671
- }),
672
- }),
673
- ],
674
- }),
675
- }),
676
- ],
677
- });
678
- return (0, graphql_1.print)(ast);
679
- }
3
+ exports.toOrderByTypeName = exports.toCamelCasePlural = exports.generateIntrospectionSchema = exports.createASTQueryBuilder = exports.cleanTableToMetaObject = exports.buildCount = exports.buildFindOne = exports.buildSelect = void 0;
4
+ /**
5
+ * Re-export select query generators from @constructive-io/graphql-query.
6
+ *
7
+ * The canonical implementations of buildSelect, buildFindOne, buildCount,
8
+ * cleanTableToMetaObject, createASTQueryBuilder, generateIntrospectionSchema,
9
+ * toCamelCasePlural, and toOrderByTypeName now live in graphql-query.
10
+ */
11
+ var graphql_query_1 = require("@constructive-io/graphql-query");
12
+ Object.defineProperty(exports, "buildSelect", { enumerable: true, get: function () { return graphql_query_1.buildSelect; } });
13
+ Object.defineProperty(exports, "buildFindOne", { enumerable: true, get: function () { return graphql_query_1.buildFindOne; } });
14
+ Object.defineProperty(exports, "buildCount", { enumerable: true, get: function () { return graphql_query_1.buildCount; } });
15
+ Object.defineProperty(exports, "cleanTableToMetaObject", { enumerable: true, get: function () { return graphql_query_1.cleanTableToMetaObject; } });
16
+ Object.defineProperty(exports, "createASTQueryBuilder", { enumerable: true, get: function () { return graphql_query_1.createASTQueryBuilder; } });
17
+ Object.defineProperty(exports, "generateIntrospectionSchema", { enumerable: true, get: function () { return graphql_query_1.generateIntrospectionSchema; } });
18
+ Object.defineProperty(exports, "toCamelCasePlural", { enumerable: true, get: function () { return graphql_query_1.toCamelCasePlural; } });
19
+ Object.defineProperty(exports, "toOrderByTypeName", { enumerable: true, get: function () { return graphql_query_1.toOrderByTypeName; } });