@constructive-io/graphql-codegen 2.22.1 → 2.23.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/cli/codegen/barrel.d.ts +5 -1
  2. package/cli/codegen/barrel.js +13 -11
  3. package/cli/codegen/index.d.ts +3 -3
  4. package/cli/codegen/index.js +15 -9
  5. package/cli/codegen/orm/client-generator.js +3 -2
  6. package/cli/codegen/orm/custom-ops-generator.js +17 -4
  7. package/cli/codegen/orm/input-types-generator.js +129 -18
  8. package/cli/codegen/orm/model-generator.js +2 -1
  9. package/cli/codegen/orm/query-builder.d.ts +1 -1
  10. package/cli/codegen/orm/query-builder.js +2 -2
  11. package/cli/codegen/schema-types-generator.js +5 -5
  12. package/cli/codegen/utils.d.ts +6 -1
  13. package/cli/codegen/utils.js +23 -8
  14. package/cli/commands/generate-orm.d.ts +5 -3
  15. package/cli/commands/generate-orm.js +65 -84
  16. package/cli/commands/generate.d.ts +2 -0
  17. package/cli/commands/generate.js +66 -87
  18. package/cli/commands/shared.d.ts +74 -0
  19. package/cli/commands/shared.js +88 -0
  20. package/cli/index.js +75 -45
  21. package/cli/introspect/index.d.ts +8 -5
  22. package/cli/introspect/index.js +19 -7
  23. package/cli/introspect/infer-tables.d.ts +51 -0
  24. package/cli/introspect/infer-tables.js +550 -0
  25. package/cli/introspect/source/endpoint.d.ts +34 -0
  26. package/cli/introspect/source/endpoint.js +35 -0
  27. package/cli/introspect/source/file.d.ts +20 -0
  28. package/cli/introspect/source/file.js +103 -0
  29. package/cli/introspect/source/index.d.ts +48 -0
  30. package/cli/introspect/source/index.js +72 -0
  31. package/cli/introspect/source/types.d.ts +58 -0
  32. package/cli/introspect/source/types.js +27 -0
  33. package/cli/introspect/transform.d.ts +5 -6
  34. package/cli/introspect/transform.js +0 -173
  35. package/cli/watch/cache.d.ts +3 -4
  36. package/cli/watch/cache.js +6 -10
  37. package/cli/watch/poller.d.ts +1 -2
  38. package/cli/watch/poller.js +27 -45
  39. package/cli/watch/types.d.ts +0 -3
  40. package/core/ast.js +4 -4
  41. package/core/query-builder.js +12 -12
  42. package/esm/cli/codegen/barrel.d.ts +5 -1
  43. package/esm/cli/codegen/barrel.js +13 -11
  44. package/esm/cli/codegen/index.d.ts +3 -3
  45. package/esm/cli/codegen/index.js +18 -12
  46. package/esm/cli/codegen/orm/client-generator.js +3 -2
  47. package/esm/cli/codegen/orm/custom-ops-generator.js +18 -5
  48. package/esm/cli/codegen/orm/input-types-generator.js +130 -19
  49. package/esm/cli/codegen/orm/model-generator.js +3 -2
  50. package/esm/cli/codegen/orm/query-builder.d.ts +1 -1
  51. package/esm/cli/codegen/orm/query-builder.js +2 -2
  52. package/esm/cli/codegen/schema-types-generator.js +6 -6
  53. package/esm/cli/codegen/utils.d.ts +6 -1
  54. package/esm/cli/codegen/utils.js +22 -8
  55. package/esm/cli/commands/generate-orm.d.ts +5 -3
  56. package/esm/cli/commands/generate-orm.js +65 -84
  57. package/esm/cli/commands/generate.d.ts +2 -0
  58. package/esm/cli/commands/generate.js +66 -87
  59. package/esm/cli/commands/shared.d.ts +74 -0
  60. package/esm/cli/commands/shared.js +84 -0
  61. package/esm/cli/index.js +76 -46
  62. package/esm/cli/introspect/index.d.ts +8 -5
  63. package/esm/cli/introspect/index.js +10 -3
  64. package/esm/cli/introspect/infer-tables.d.ts +51 -0
  65. package/esm/cli/introspect/infer-tables.js +547 -0
  66. package/esm/cli/introspect/source/endpoint.d.ts +34 -0
  67. package/esm/cli/introspect/source/endpoint.js +31 -0
  68. package/esm/cli/introspect/source/file.d.ts +20 -0
  69. package/esm/cli/introspect/source/file.js +66 -0
  70. package/esm/cli/introspect/source/index.d.ts +48 -0
  71. package/esm/cli/introspect/source/index.js +54 -0
  72. package/esm/cli/introspect/source/types.d.ts +58 -0
  73. package/esm/cli/introspect/source/types.js +23 -0
  74. package/esm/cli/introspect/transform.d.ts +5 -6
  75. package/esm/cli/introspect/transform.js +0 -172
  76. package/esm/cli/watch/cache.d.ts +3 -4
  77. package/esm/cli/watch/cache.js +7 -11
  78. package/esm/cli/watch/poller.d.ts +1 -2
  79. package/esm/cli/watch/poller.js +28 -46
  80. package/esm/cli/watch/types.d.ts +0 -3
  81. package/esm/core/ast.js +4 -4
  82. package/esm/core/query-builder.js +12 -12
  83. package/esm/generators/mutations.js +3 -3
  84. package/esm/generators/select.js +7 -7
  85. package/esm/types/config.d.ts +21 -5
  86. package/esm/types/config.js +2 -1
  87. package/generators/mutations.js +3 -3
  88. package/generators/select.js +7 -7
  89. package/package.json +5 -3
  90. package/types/config.d.ts +21 -5
  91. package/types/config.js +2 -1
  92. package/cli/introspect/fetch-meta.d.ts +0 -31
  93. package/cli/introspect/fetch-meta.js +0 -108
  94. package/cli/introspect/meta-query.d.ts +0 -111
  95. package/cli/introspect/meta-query.js +0 -191
  96. package/esm/cli/introspect/fetch-meta.d.ts +0 -31
  97. package/esm/cli/introspect/fetch-meta.js +0 -104
  98. package/esm/cli/introspect/meta-query.d.ts +0 -111
  99. package/esm/cli/introspect/meta-query.js +0 -188
@@ -1,19 +1,17 @@
1
1
  "use strict";
2
2
  /**
3
- * Generate ORM command - generates Prisma-like ORM client
3
+ * Generate ORM command - generates Prisma-like ORM client from GraphQL schema
4
4
  *
5
5
  * This command:
6
- * 1. Fetches _meta query for table-based CRUD operations
7
- * 2. Fetches __schema introspection for custom operations
6
+ * 1. Fetches schema from endpoint or loads from file
7
+ * 2. Infers table metadata from introspection (replaces _meta)
8
8
  * 3. Generates a Prisma-like ORM client with fluent API
9
9
  */
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
11
  exports.generateOrmCommand = generateOrmCommand;
12
12
  const config_1 = require("../../types/config");
13
- const fetch_meta_1 = require("../introspect/fetch-meta");
14
- const fetch_schema_1 = require("../introspect/fetch-schema");
15
- const transform_1 = require("../introspect/transform");
16
- const transform_schema_1 = require("../introspect/transform-schema");
13
+ const source_1 = require("../introspect/source");
14
+ const shared_1 = require("./shared");
17
15
  const init_1 = require("./init");
18
16
  const generate_1 = require("./generate");
19
17
  const orm_1 = require("../codegen/orm");
@@ -34,109 +32,90 @@ async function generateOrmCommand(options = {}) {
34
32
  const config = configResult.config;
35
33
  // Use ORM output directory if specified, otherwise default
36
34
  const outputDir = options.output || config.orm?.output || './generated/orm';
37
- log(` Endpoint: ${config.endpoint}`);
35
+ // Log source
36
+ if (config.schema) {
37
+ log(` Schema: ${config.schema}`);
38
+ }
39
+ else {
40
+ log(` Endpoint: ${config.endpoint}`);
41
+ }
38
42
  log(` Output: ${outputDir}`);
39
- // 2. Validate endpoint
40
- const endpointValidation = (0, fetch_meta_1.validateEndpoint)(config.endpoint);
41
- if (!endpointValidation.valid) {
43
+ // 2. Create schema source
44
+ const sourceValidation = (0, source_1.validateSourceOptions)({
45
+ endpoint: config.endpoint || undefined,
46
+ schema: config.schema || undefined,
47
+ });
48
+ if (!sourceValidation.valid) {
42
49
  return {
43
50
  success: false,
44
- message: `Invalid endpoint: ${endpointValidation.error}`,
51
+ message: sourceValidation.error,
45
52
  };
46
53
  }
47
- // Build authorization header if provided
48
- const authHeader = options.authorization || config.headers['Authorization'];
49
- // 3. Fetch _meta for table-based operations
50
- log('Fetching schema metadata (_meta)...');
51
- const metaResult = await (0, fetch_meta_1.fetchMeta)({
52
- endpoint: config.endpoint,
53
- authorization: authHeader,
54
+ const source = (0, source_1.createSchemaSource)({
55
+ endpoint: config.endpoint || undefined,
56
+ schema: config.schema || undefined,
57
+ authorization: options.authorization || config.headers['Authorization'],
54
58
  headers: config.headers,
55
- timeout: 30000,
56
59
  });
57
- if (!metaResult.success) {
60
+ // 3. Run the codegen pipeline
61
+ let pipelineResult;
62
+ try {
63
+ pipelineResult = await (0, shared_1.runCodegenPipeline)({
64
+ source,
65
+ config,
66
+ verbose: options.verbose,
67
+ skipCustomOperations: options.skipCustomOperations,
68
+ });
69
+ }
70
+ catch (err) {
58
71
  return {
59
72
  success: false,
60
- message: `Failed to fetch _meta: ${metaResult.error}`,
73
+ message: `Failed to fetch schema: ${err instanceof Error ? err.message : 'Unknown error'}`,
61
74
  };
62
75
  }
63
- // 4. Transform to CleanTable[]
64
- log('Transforming table schema...');
65
- let tables = (0, transform_1.transformMetaToCleanTables)(metaResult.data);
66
- log(` Found ${tables.length} tables`);
67
- // 5. Filter tables
68
- tables = (0, transform_1.filterTables)(tables, config.tables.include, config.tables.exclude);
69
- log(` After filtering: ${tables.length} tables`);
70
- if (tables.length === 0) {
76
+ const { tables, customOperations, stats } = pipelineResult;
77
+ // 4. Validate tables found
78
+ const tablesValidation = (0, shared_1.validateTablesFound)(tables);
79
+ if (!tablesValidation.valid) {
71
80
  return {
72
81
  success: false,
73
- message: 'No tables found after filtering. Check your include/exclude patterns.',
82
+ message: tablesValidation.error,
74
83
  };
75
84
  }
76
- // Get table operation names for filtering custom operations
77
- const tableOperationNames = (0, transform_schema_1.getTableOperationNames)(tables);
78
- // 6. Fetch __schema for custom operations (unless skipped)
79
- let customQueries = [];
80
- let customMutations = [];
81
- let customOperationsData;
82
- if (!options.skipCustomOperations) {
83
- log('Fetching schema introspection (__schema)...');
84
- const schemaResult = await (0, fetch_schema_1.fetchSchema)({
85
- endpoint: config.endpoint,
86
- authorization: authHeader,
87
- headers: config.headers,
88
- timeout: 30000,
89
- });
90
- if (schemaResult.success && schemaResult.data) {
91
- log('Transforming custom operations...');
92
- // Transform to CleanOperation[]
93
- const { queries: allQueries, mutations: allMutations, typeRegistry } = (0, transform_schema_1.transformSchemaToOperations)(schemaResult.data);
94
- log(` Found ${allQueries.length} queries and ${allMutations.length} mutations total`);
95
- // Filter by config include/exclude
96
- const filteredQueries = (0, transform_schema_1.filterOperations)(allQueries, config.queries.include, config.queries.exclude);
97
- const filteredMutations = (0, transform_schema_1.filterOperations)(allMutations, config.mutations.include, config.mutations.exclude);
98
- log(` After config filtering: ${filteredQueries.length} queries, ${filteredMutations.length} mutations`);
99
- // Remove table operations (already handled by table generators)
100
- const customQueriesOps = (0, transform_schema_1.getCustomOperations)(filteredQueries, tableOperationNames);
101
- const customMutationsOps = (0, transform_schema_1.getCustomOperations)(filteredMutations, tableOperationNames);
102
- log(` Custom operations: ${customQueriesOps.length} queries, ${customMutationsOps.length} mutations`);
103
- customQueries = customQueriesOps.map((q) => q.name);
104
- customMutations = customMutationsOps.map((m) => m.name);
105
- customOperationsData = {
106
- queries: customQueriesOps,
107
- mutations: customMutationsOps,
108
- typeRegistry,
109
- };
110
- }
111
- else {
112
- log(` Warning: Could not fetch __schema: ${schemaResult.error}`);
113
- log(' Continuing with table-only generation...');
114
- }
115
- }
116
- // 7. Generate ORM code
85
+ // 5. Generate ORM code
117
86
  console.log('Generating code...');
118
- const { files: generatedFiles, stats } = (0, orm_1.generateOrm)({
87
+ const { files: generatedFiles, stats: genStats } = (0, orm_1.generateOrm)({
119
88
  tables,
120
- customOperations: customOperationsData,
89
+ customOperations: {
90
+ queries: customOperations.queries,
91
+ mutations: customOperations.mutations,
92
+ typeRegistry: customOperations.typeRegistry,
93
+ },
121
94
  config,
122
95
  });
123
- console.log(`Generated ${stats.totalFiles} files`);
124
- log(` ${stats.tables} table models`);
125
- log(` ${stats.customQueries} custom query operations`);
126
- log(` ${stats.customMutations} custom mutation operations`);
96
+ console.log(`Generated ${genStats.totalFiles} files`);
97
+ log(` ${genStats.tables} table models`);
98
+ log(` ${genStats.customQueries} custom query operations`);
99
+ log(` ${genStats.customMutations} custom mutation operations`);
100
+ const customQueries = customOperations.queries.map((q) => q.name);
101
+ const customMutations = customOperations.mutations.map((m) => m.name);
127
102
  if (options.dryRun) {
128
103
  return {
129
104
  success: true,
130
- message: `Dry run complete. Would generate ${generatedFiles.length} files for ${tables.length} tables and ${customQueries.length + customMutations.length} custom operations.`,
105
+ message: `Dry run complete. Would generate ${generatedFiles.length} files for ${tables.length} tables and ${stats.customQueries + stats.customMutations} custom operations.`,
131
106
  tables: tables.map((t) => t.name),
132
107
  customQueries,
133
108
  customMutations,
134
109
  filesWritten: generatedFiles.map((f) => f.path),
135
110
  };
136
111
  }
137
- // 8. Write files
112
+ // 6. Write files
138
113
  log('Writing files...');
139
- const writeResult = await (0, generate_1.writeGeneratedFiles)(generatedFiles, outputDir, ['models', 'query', 'mutation']);
114
+ const writeResult = await (0, generate_1.writeGeneratedFiles)(generatedFiles, outputDir, [
115
+ 'models',
116
+ 'query',
117
+ 'mutation',
118
+ ]);
140
119
  if (!writeResult.success) {
141
120
  return {
142
121
  success: false,
@@ -171,7 +150,8 @@ async function loadConfig(options) {
171
150
  }
172
151
  // Override with CLI options
173
152
  const mergedConfig = {
174
- endpoint: options.endpoint || baseConfig.endpoint || '',
153
+ endpoint: options.endpoint || baseConfig.endpoint,
154
+ schema: options.schema || baseConfig.schema,
175
155
  output: options.output || baseConfig.output,
176
156
  headers: baseConfig.headers,
177
157
  tables: baseConfig.tables,
@@ -183,10 +163,11 @@ async function loadConfig(options) {
183
163
  codegen: baseConfig.codegen,
184
164
  orm: baseConfig.orm,
185
165
  };
186
- if (!mergedConfig.endpoint) {
166
+ // Validate at least one source is provided
167
+ if (!mergedConfig.endpoint && !mergedConfig.schema) {
187
168
  return {
188
169
  success: false,
189
- error: 'No endpoint specified. Use --endpoint or create a config file with "graphql-codegen init".',
170
+ error: 'No source specified. Use --endpoint or --schema, or create a config file with "graphql-codegen init".',
190
171
  };
191
172
  }
192
173
  // Resolve with defaults
@@ -3,6 +3,8 @@ export interface GenerateOptions {
3
3
  config?: string;
4
4
  /** GraphQL endpoint URL (overrides config) */
5
5
  endpoint?: string;
6
+ /** Path to GraphQL schema file (.graphql) */
7
+ schema?: string;
6
8
  /** Output directory (overrides config) */
7
9
  output?: string;
8
10
  /** Authorization header */
@@ -36,22 +36,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.generateCommand = generateCommand;
37
37
  exports.writeGeneratedFiles = writeGeneratedFiles;
38
38
  /**
39
- * Generate command - introspects endpoint and generates SDK
39
+ * Generate command - generates SDK from GraphQL schema
40
40
  *
41
41
  * This command:
42
- * 1. Fetches _meta query for table-based CRUD operations
43
- * 2. Fetches __schema introspection for ALL operations
44
- * 3. Filters out table operations from custom operations to avoid duplicates
45
- * 4. Generates hooks for both table CRUD and custom operations
42
+ * 1. Fetches schema from endpoint or loads from file
43
+ * 2. Infers table metadata from introspection (replaces _meta)
44
+ * 3. Generates hooks for both table CRUD and custom operations
46
45
  */
47
46
  const fs = __importStar(require("node:fs"));
48
47
  const path = __importStar(require("node:path"));
49
48
  const prettier = __importStar(require("prettier"));
50
49
  const config_1 = require("../../types/config");
51
- const fetch_meta_1 = require("../introspect/fetch-meta");
52
- const fetch_schema_1 = require("../introspect/fetch-schema");
53
- const transform_1 = require("../introspect/transform");
54
- const transform_schema_1 = require("../introspect/transform-schema");
50
+ const source_1 = require("../introspect/source");
51
+ const shared_1 = require("./shared");
55
52
  const init_1 = require("./init");
56
53
  const codegen_1 = require("../codegen");
57
54
  /**
@@ -69,110 +66,90 @@ async function generateCommand(options = {}) {
69
66
  };
70
67
  }
71
68
  const config = configResult.config;
72
- log(` Endpoint: ${config.endpoint}`);
69
+ // Log source
70
+ if (config.schema) {
71
+ log(` Schema: ${config.schema}`);
72
+ }
73
+ else {
74
+ log(` Endpoint: ${config.endpoint}`);
75
+ }
73
76
  log(` Output: ${config.output}`);
74
- // 2. Validate endpoint
75
- const endpointValidation = (0, fetch_meta_1.validateEndpoint)(config.endpoint);
76
- if (!endpointValidation.valid) {
77
+ // 2. Create schema source
78
+ const sourceValidation = (0, source_1.validateSourceOptions)({
79
+ endpoint: config.endpoint || undefined,
80
+ schema: config.schema || undefined,
81
+ });
82
+ if (!sourceValidation.valid) {
77
83
  return {
78
84
  success: false,
79
- message: `Invalid endpoint: ${endpointValidation.error}`,
85
+ message: sourceValidation.error,
80
86
  };
81
87
  }
82
- // Build authorization header if provided
83
- const authHeader = options.authorization || config.headers['Authorization'];
84
- // 3. Fetch _meta for table-based operations
85
- log('Fetching schema metadata (_meta)...');
86
- const metaResult = await (0, fetch_meta_1.fetchMeta)({
87
- endpoint: config.endpoint,
88
- authorization: authHeader,
88
+ const source = (0, source_1.createSchemaSource)({
89
+ endpoint: config.endpoint || undefined,
90
+ schema: config.schema || undefined,
91
+ authorization: options.authorization || config.headers['Authorization'],
89
92
  headers: config.headers,
90
- timeout: 30000,
91
93
  });
92
- if (!metaResult.success) {
94
+ // 3. Run the codegen pipeline
95
+ let pipelineResult;
96
+ try {
97
+ pipelineResult = await (0, shared_1.runCodegenPipeline)({
98
+ source,
99
+ config,
100
+ verbose: options.verbose,
101
+ skipCustomOperations: options.skipCustomOperations,
102
+ });
103
+ }
104
+ catch (err) {
93
105
  return {
94
106
  success: false,
95
- message: `Failed to fetch _meta: ${metaResult.error}`,
107
+ message: `Failed to fetch schema: ${err instanceof Error ? err.message : 'Unknown error'}`,
96
108
  };
97
109
  }
98
- // 4. Transform to CleanTable[]
99
- log('Transforming table schema...');
100
- let tables = (0, transform_1.transformMetaToCleanTables)(metaResult.data);
101
- log(` Found ${tables.length} tables`);
102
- // 5. Filter tables
103
- tables = (0, transform_1.filterTables)(tables, config.tables.include, config.tables.exclude);
104
- log(` After filtering: ${tables.length} tables`);
105
- if (tables.length === 0) {
110
+ const { tables, customOperations, stats } = pipelineResult;
111
+ // 4. Validate tables found
112
+ const tablesValidation = (0, shared_1.validateTablesFound)(tables);
113
+ if (!tablesValidation.valid) {
106
114
  return {
107
115
  success: false,
108
- message: 'No tables found after filtering. Check your include/exclude patterns.',
116
+ message: tablesValidation.error,
109
117
  };
110
118
  }
111
- // Get table operation names for filtering custom operations
112
- const tableOperationNames = (0, transform_schema_1.getTableOperationNames)(tables);
113
- // 6. Fetch __schema for custom operations (unless skipped)
114
- let customQueries = [];
115
- let customMutations = [];
116
- let customOperationsData;
117
- if (!options.skipCustomOperations) {
118
- log('Fetching schema introspection (__schema)...');
119
- const schemaResult = await (0, fetch_schema_1.fetchSchema)({
120
- endpoint: config.endpoint,
121
- authorization: authHeader,
122
- headers: config.headers,
123
- timeout: 30000,
124
- });
125
- if (schemaResult.success && schemaResult.data) {
126
- log('Transforming custom operations...');
127
- // Transform to CleanOperation[]
128
- const { queries: allQueries, mutations: allMutations, typeRegistry } = (0, transform_schema_1.transformSchemaToOperations)(schemaResult.data);
129
- log(` Found ${allQueries.length} queries and ${allMutations.length} mutations total`);
130
- // Filter by config include/exclude
131
- const filteredQueries = (0, transform_schema_1.filterOperations)(allQueries, config.queries.include, config.queries.exclude);
132
- const filteredMutations = (0, transform_schema_1.filterOperations)(allMutations, config.mutations.include, config.mutations.exclude);
133
- log(` After config filtering: ${filteredQueries.length} queries, ${filteredMutations.length} mutations`);
134
- // Remove table operations (already handled by table generators)
135
- const customQueriesOps = (0, transform_schema_1.getCustomOperations)(filteredQueries, tableOperationNames);
136
- const customMutationsOps = (0, transform_schema_1.getCustomOperations)(filteredMutations, tableOperationNames);
137
- log(` Custom operations: ${customQueriesOps.length} queries, ${customMutationsOps.length} mutations`);
138
- customQueries = customQueriesOps.map((q) => q.name);
139
- customMutations = customMutationsOps.map((m) => m.name);
140
- customOperationsData = {
141
- queries: customQueriesOps,
142
- mutations: customMutationsOps,
143
- typeRegistry,
144
- };
145
- }
146
- else {
147
- log(` Warning: Could not fetch __schema: ${schemaResult.error}`);
148
- log(' Continuing with table-only generation...');
149
- }
150
- }
151
- // 7. Generate code
119
+ // 5. Generate code
152
120
  console.log('Generating code...');
153
- const { files: generatedFiles, stats } = (0, codegen_1.generate)({
121
+ const { files: generatedFiles, stats: genStats } = (0, codegen_1.generate)({
154
122
  tables,
155
- customOperations: customOperationsData,
123
+ customOperations: {
124
+ queries: customOperations.queries,
125
+ mutations: customOperations.mutations,
126
+ typeRegistry: customOperations.typeRegistry,
127
+ },
156
128
  config,
157
129
  });
158
- console.log(`Generated ${stats.totalFiles} files`);
159
- log(` ${stats.queryHooks} table query hooks`);
160
- log(` ${stats.mutationHooks} table mutation hooks`);
161
- log(` ${stats.customQueryHooks} custom query hooks`);
162
- log(` ${stats.customMutationHooks} custom mutation hooks`);
130
+ console.log(`Generated ${genStats.totalFiles} files`);
131
+ log(` ${genStats.queryHooks} table query hooks`);
132
+ log(` ${genStats.mutationHooks} table mutation hooks`);
133
+ log(` ${genStats.customQueryHooks} custom query hooks`);
134
+ log(` ${genStats.customMutationHooks} custom mutation hooks`);
135
+ const customQueries = customOperations.queries.map((q) => q.name);
136
+ const customMutations = customOperations.mutations.map((m) => m.name);
163
137
  if (options.dryRun) {
164
138
  return {
165
139
  success: true,
166
- message: `Dry run complete. Would generate ${generatedFiles.length} files for ${tables.length} tables and ${customQueries.length + customMutations.length} custom operations.`,
140
+ message: `Dry run complete. Would generate ${generatedFiles.length} files for ${tables.length} tables and ${stats.customQueries + stats.customMutations} custom operations.`,
167
141
  tables: tables.map((t) => t.name),
168
142
  customQueries,
169
143
  customMutations,
170
144
  filesWritten: generatedFiles.map((f) => f.path),
171
145
  };
172
146
  }
173
- // 8. Write files
147
+ // 6. Write files
174
148
  log('Writing files...');
175
- const writeResult = await writeGeneratedFiles(generatedFiles, config.output, ['queries', 'mutations']);
149
+ const writeResult = await writeGeneratedFiles(generatedFiles, config.output, [
150
+ 'queries',
151
+ 'mutations',
152
+ ]);
176
153
  if (!writeResult.success) {
177
154
  return {
178
155
  success: false,
@@ -207,7 +184,8 @@ async function loadConfig(options) {
207
184
  }
208
185
  // Override with CLI options
209
186
  const mergedConfig = {
210
- endpoint: options.endpoint || baseConfig.endpoint || '',
187
+ endpoint: options.endpoint || baseConfig.endpoint,
188
+ schema: options.schema || baseConfig.schema,
211
189
  output: options.output || baseConfig.output,
212
190
  headers: baseConfig.headers,
213
191
  tables: baseConfig.tables,
@@ -218,10 +196,11 @@ async function loadConfig(options) {
218
196
  postgraphile: baseConfig.postgraphile,
219
197
  codegen: baseConfig.codegen,
220
198
  };
221
- if (!mergedConfig.endpoint) {
199
+ // Validate at least one source is provided
200
+ if (!mergedConfig.endpoint && !mergedConfig.schema) {
222
201
  return {
223
202
  success: false,
224
- error: 'No endpoint specified. Use --endpoint or create a config file with "graphql-codegen init".',
203
+ error: 'No source specified. Use --endpoint or --schema, or create a config file with "graphql-codegen init".',
225
204
  };
226
205
  }
227
206
  // Resolve with defaults
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Shared Codegen Pipeline
3
+ *
4
+ * Consolidates the common logic between generate and generate-orm commands.
5
+ * Handles:
6
+ * - Schema fetching from source (endpoint or file)
7
+ * - Table inference from introspection
8
+ * - Operation transformation
9
+ * - Filtering
10
+ */
11
+ import type { ResolvedConfig } from '../../types/config';
12
+ import type { CleanTable, CleanOperation, TypeRegistry } from '../../types/schema';
13
+ import type { SchemaSource } from '../introspect/source';
14
+ export interface CodegenPipelineOptions {
15
+ /**
16
+ * Schema source (endpoint or file)
17
+ */
18
+ source: SchemaSource;
19
+ /**
20
+ * Resolved configuration
21
+ */
22
+ config: ResolvedConfig;
23
+ /**
24
+ * Enable verbose logging
25
+ */
26
+ verbose?: boolean;
27
+ /**
28
+ * Skip custom operations (only generate table CRUD)
29
+ */
30
+ skipCustomOperations?: boolean;
31
+ }
32
+ export interface CodegenPipelineResult {
33
+ /**
34
+ * Inferred table metadata
35
+ */
36
+ tables: CleanTable[];
37
+ /**
38
+ * Custom operations (queries and mutations not covered by tables)
39
+ */
40
+ customOperations: {
41
+ queries: CleanOperation[];
42
+ mutations: CleanOperation[];
43
+ typeRegistry: TypeRegistry;
44
+ };
45
+ /**
46
+ * Statistics about what was found
47
+ */
48
+ stats: {
49
+ totalTables: number;
50
+ filteredTables: number;
51
+ totalQueries: number;
52
+ totalMutations: number;
53
+ customQueries: number;
54
+ customMutations: number;
55
+ };
56
+ }
57
+ /**
58
+ * Run the unified codegen pipeline
59
+ *
60
+ * This replaces the duplicated logic in generate.ts and generate-orm.ts:
61
+ * 1. Fetch introspection from source (endpoint or file)
62
+ * 2. Infer tables from introspection (replaces _meta query)
63
+ * 3. Transform to operations
64
+ * 4. Filter by config
65
+ * 5. Separate table operations from custom operations
66
+ */
67
+ export declare function runCodegenPipeline(options: CodegenPipelineOptions): Promise<CodegenPipelineResult>;
68
+ /**
69
+ * Validate that tables were found
70
+ */
71
+ export declare function validateTablesFound(tables: CleanTable[]): {
72
+ valid: boolean;
73
+ error?: string;
74
+ };
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runCodegenPipeline = runCodegenPipeline;
4
+ exports.validateTablesFound = validateTablesFound;
5
+ const infer_tables_1 = require("../introspect/infer-tables");
6
+ const transform_1 = require("../introspect/transform");
7
+ const transform_schema_1 = require("../introspect/transform-schema");
8
+ // ============================================================================
9
+ // Main Pipeline
10
+ // ============================================================================
11
+ /**
12
+ * Run the unified codegen pipeline
13
+ *
14
+ * This replaces the duplicated logic in generate.ts and generate-orm.ts:
15
+ * 1. Fetch introspection from source (endpoint or file)
16
+ * 2. Infer tables from introspection (replaces _meta query)
17
+ * 3. Transform to operations
18
+ * 4. Filter by config
19
+ * 5. Separate table operations from custom operations
20
+ */
21
+ async function runCodegenPipeline(options) {
22
+ const { source, config, verbose = false, skipCustomOperations = false, } = options;
23
+ const log = verbose ? console.log : () => { };
24
+ // 1. Fetch introspection from source
25
+ log(`Fetching schema from ${source.describe()}...`);
26
+ const { introspection } = await source.fetch();
27
+ // 2. Infer tables from introspection (replaces _meta)
28
+ log('Inferring table metadata from schema...');
29
+ let tables = (0, infer_tables_1.inferTablesFromIntrospection)(introspection);
30
+ const totalTables = tables.length;
31
+ log(` Found ${totalTables} tables`);
32
+ // 3. Filter tables by config
33
+ tables = (0, transform_1.filterTables)(tables, config.tables.include, config.tables.exclude);
34
+ const filteredTables = tables.length;
35
+ log(` After filtering: ${filteredTables} tables`);
36
+ // 4. Transform introspection to operations
37
+ log('Transforming operations...');
38
+ const { queries: allQueries, mutations: allMutations, typeRegistry, } = (0, transform_schema_1.transformSchemaToOperations)(introspection);
39
+ const totalQueries = allQueries.length;
40
+ const totalMutations = allMutations.length;
41
+ log(` Found ${totalQueries} queries and ${totalMutations} mutations total`);
42
+ // 5. Get table operation names for filtering custom ops
43
+ const tableOperationNames = (0, transform_schema_1.getTableOperationNames)(tables);
44
+ // 6. Filter and separate custom operations
45
+ let customQueries = [];
46
+ let customMutations = [];
47
+ if (!skipCustomOperations) {
48
+ // Filter by config include/exclude
49
+ const filteredQueries = (0, transform_schema_1.filterOperations)(allQueries, config.queries.include, config.queries.exclude);
50
+ const filteredMutations = (0, transform_schema_1.filterOperations)(allMutations, config.mutations.include, config.mutations.exclude);
51
+ log(` After config filtering: ${filteredQueries.length} queries, ${filteredMutations.length} mutations`);
52
+ // Remove table operations (already handled by table generators)
53
+ customQueries = (0, transform_schema_1.getCustomOperations)(filteredQueries, tableOperationNames);
54
+ customMutations = (0, transform_schema_1.getCustomOperations)(filteredMutations, tableOperationNames);
55
+ log(` Custom operations: ${customQueries.length} queries, ${customMutations.length} mutations`);
56
+ }
57
+ return {
58
+ tables,
59
+ customOperations: {
60
+ queries: customQueries,
61
+ mutations: customMutations,
62
+ typeRegistry,
63
+ },
64
+ stats: {
65
+ totalTables,
66
+ filteredTables,
67
+ totalQueries,
68
+ totalMutations,
69
+ customQueries: customQueries.length,
70
+ customMutations: customMutations.length,
71
+ },
72
+ };
73
+ }
74
+ // ============================================================================
75
+ // Validation Helpers
76
+ // ============================================================================
77
+ /**
78
+ * Validate that tables were found
79
+ */
80
+ function validateTablesFound(tables) {
81
+ if (tables.length === 0) {
82
+ return {
83
+ valid: false,
84
+ error: 'No tables found after filtering. Check your include/exclude patterns.',
85
+ };
86
+ }
87
+ return { valid: true };
88
+ }