From 9420656ce96183877511f93f30785b26d8b304b9 Mon Sep 17 00:00:00 2001 From: ghou9khub Date: Mon, 19 Jan 2026 20:42:18 -0800 Subject: [PATCH 1/2] Format header logics. Solve lint issue --- .../codegen/src/cli/commands/generate-orm.ts | 4 +- graphql/codegen/src/cli/commands/generate.ts | 4 +- packages/cli/src/commands/codegen.ts | 80 +++++++++++++++---- 3 files changed, 69 insertions(+), 19 deletions(-) diff --git a/graphql/codegen/src/cli/commands/generate-orm.ts b/graphql/codegen/src/cli/commands/generate-orm.ts index 10178f3f5..326eef46d 100644 --- a/graphql/codegen/src/cli/commands/generate-orm.ts +++ b/graphql/codegen/src/cli/commands/generate-orm.ts @@ -35,6 +35,8 @@ export interface GenerateOrmOptions { output?: string; /** Authorization header */ authorization?: string; + /** Additional HTTP headers for endpoint requests */ + headers?: Record; /** Verbose output */ verbose?: boolean; /** Dry run - don't write files */ @@ -173,7 +175,7 @@ async function generateOrmForTarget( endpoint: config.endpoint || undefined, schema: config.schema || undefined, authorization: options.authorization || config.headers['Authorization'], - headers: config.headers, + headers: { ...config.headers, ...options.headers }, }); // 2. Run the codegen pipeline diff --git a/graphql/codegen/src/cli/commands/generate.ts b/graphql/codegen/src/cli/commands/generate.ts index 4f36710b0..e039da494 100644 --- a/graphql/codegen/src/cli/commands/generate.ts +++ b/graphql/codegen/src/cli/commands/generate.ts @@ -37,6 +37,8 @@ export interface GenerateOptions { output?: string; /** Authorization header */ authorization?: string; + /** Additional HTTP headers for endpoint requests */ + headers?: Record; /** Verbose output */ verbose?: boolean; /** Dry run - don't write files */ @@ -174,7 +176,7 @@ async function generateForTarget( endpoint: config.endpoint || undefined, schema: config.schema || undefined, authorization: options.authorization || config.headers['Authorization'], - headers: config.headers, + headers: { ...config.headers, ...options.headers }, }); // 2. Run the codegen pipeline diff --git a/packages/cli/src/commands/codegen.ts b/packages/cli/src/commands/codegen.ts index 9e67a4a83..d26519cd3 100644 --- a/packages/cli/src/commands/codegen.ts +++ b/packages/cli/src/commands/codegen.ts @@ -7,12 +7,30 @@ import { generateCommand, type GenerateTargetResult, } from '@constructive-io/graphql-codegen/cli/commands/generate'; +import { + generateOrmCommand, + type GenerateOrmTargetResult, +} from '@constructive-io/graphql-codegen/cli/commands/generate-orm'; import { findConfigFile } from '@constructive-io/graphql-codegen/cli/commands/init'; import { ConstructiveOptions, getEnvOptions, } from '@constructive-io/graphql-env'; +function parseHeaders(headerArg: string | string[] | undefined): Record { + const headerList = Array.isArray(headerArg) ? headerArg : headerArg ? [headerArg] : []; + const headers: Record = {}; + for (const h of headerList) { + const idx = typeof h === 'string' ? h.indexOf(':') : -1; + if (idx <= 0) continue; + const name = h.slice(0, idx).trim(); + const value = h.slice(idx + 1).trim(); + if (!name) continue; + headers[name] = value; + } + return headers; +} + const usage = ` Constructive GraphQL Codegen: @@ -20,16 +38,23 @@ Constructive GraphQL Codegen: Options: --help, -h Show this help message + --mode Generation mode: "hooks" (default) or "orm" --config Path to graphql-codegen config file --target Target name in config file --endpoint GraphQL endpoint URL --auth Authorization header value (e.g., "Bearer 123") - --out Output directory (default: graphql/codegen/dist) + --header "Name: Value" Optional HTTP header; repeat to add multiple + --out Output directory --dry-run Preview without writing files -v, --verbose Verbose output --database Database override for DB mode (defaults to PGDATABASE) --schemas Comma-separated schemas (required for DB mode) + +Examples: + cnc codegen --endpoint https://api.example.com/graphql + cnc codegen --mode orm --endpoint https://api.example.com/graphql + cnc codegen --mode hooks --schemas public --database mydb `; export default async ( @@ -42,6 +67,7 @@ export default async ( process.exit(0); } + const mode = (argv.mode as string) || 'hooks'; const endpointArg = (argv.endpoint as string) || ''; const outputArg = argv.out as string | undefined; const outDir = outputArg || 'codegen'; @@ -50,6 +76,11 @@ export default async ( const target = (argv.target as string) || ''; const dryRun = !!(argv['dry-run'] || argv.dryRun); const verbose = !!(argv.verbose || argv.v); + + if (mode !== 'hooks' && mode !== 'orm') { + console.error(`Error: Invalid mode "${mode}". Must be "hooks" or "orm".`); + process.exit(1); + } const resolvedConfigPath = configPath || findConfigFile() || ''; const hasConfigFile = Boolean(resolvedConfigPath); const outputForGenerate = outputArg || !hasConfigFile ? outDir : undefined; @@ -60,6 +91,8 @@ export default async ( : getEnvOptions(); const schemasArg = (argv.schemas as string) || ''; + const headers = parseHeaders(argv.header as string | string[] | undefined); + const runGenerate = async ({ endpoint, schema, @@ -67,17 +100,30 @@ export default async ( endpoint?: string; schema?: string; }) => { - const result = await generateCommand({ - config: configPath || undefined, - target: target || undefined, - endpoint, - schema, - output: outputForGenerate, - authorization: auth || undefined, - verbose, - dryRun, - }); - const targetResults: GenerateTargetResult[] = result.targets ?? []; + const result = mode === 'orm' + ? await generateOrmCommand({ + config: configPath || undefined, + target: target || undefined, + endpoint, + schema, + output: outputForGenerate, + authorization: auth || undefined, + headers, + verbose, + dryRun, + }) + : await generateCommand({ + config: configPath || undefined, + target: target || undefined, + endpoint, + schema, + output: outputForGenerate, + authorization: auth || undefined, + headers, + verbose, + dryRun, + }); + const targetResults: (GenerateTargetResult | GenerateOrmTargetResult)[] = result.targets ?? []; const hasNamedTargets = targetResults.length > 0 && (targetResults.length > 1 || targetResults[0]?.name !== 'default'); @@ -90,14 +136,14 @@ export default async ( if (target.tables?.length) { console.log(' Tables:'); - target.tables.forEach((table) => console.log(` - ${table}`)); + (target.tables as any).forEach((table: any) => console.log(` - ${table}`)); } if (target.filesWritten?.length) { console.log(' Files written:'); - target.filesWritten.forEach((file) => console.log(` - ${file}`)); + (target.filesWritten as any).forEach((file: any) => console.log(` - ${file}`)); } if (!target.success && target.errors?.length) { - target.errors.forEach((error) => console.error(` - ${error}`)); + (target.errors as any).forEach((error: any) => console.error(` - ${error}`)); } }); @@ -110,12 +156,12 @@ export default async ( if (!result.success) { console.error(result.message); if (result.errors?.length) - result.errors.forEach((e) => console.error(' -', e)); + (result.errors as any).forEach((e: any) => console.error(' -', e)); process.exit(1); } console.log(result.message); if (result.filesWritten?.length) { - result.filesWritten.forEach((f) => console.log(f)); + (result.filesWritten as any).forEach((f: any) => console.log(f)); } }; From 6fd8c4d85c9df96a274969ff65eef5c366dd2b5b Mon Sep 17 00:00:00 2001 From: ghou9khub Date: Mon, 19 Jan 2026 21:42:04 -0800 Subject: [PATCH 2/2] Snapshot update --- .../codegen/__snapshots__/client-generator.test.ts.snap | 2 +- .../codegen/__snapshots__/input-types-generator.test.ts.snap | 2 +- .../codegen/__snapshots__/model-generator.test.ts.snap | 2 +- .../codegen/__snapshots__/query-keys-factory.test.ts.snap | 2 +- .../codegen/__snapshots__/react-query-hooks.test.ts.snap | 2 +- .../codegen/__snapshots__/schema-types-generator.test.ts.snap | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/graphql/codegen/src/__tests__/codegen/__snapshots__/client-generator.test.ts.snap b/graphql/codegen/src/__tests__/codegen/__snapshots__/client-generator.test.ts.snap index 2581ad40f..e9a299087 100644 --- a/graphql/codegen/src/__tests__/codegen/__snapshots__/client-generator.test.ts.snap +++ b/graphql/codegen/src/__tests__/codegen/__snapshots__/client-generator.test.ts.snap @@ -1,4 +1,4 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing exports[`client-generator generateCreateClientFile generates createClient factory with models 1`] = ` "/** diff --git a/graphql/codegen/src/__tests__/codegen/__snapshots__/input-types-generator.test.ts.snap b/graphql/codegen/src/__tests__/codegen/__snapshots__/input-types-generator.test.ts.snap index 9fa5733d5..523156dd3 100644 --- a/graphql/codegen/src/__tests__/codegen/__snapshots__/input-types-generator.test.ts.snap +++ b/graphql/codegen/src/__tests__/codegen/__snapshots__/input-types-generator.test.ts.snap @@ -1,4 +1,4 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing exports[`generateInputTypesFile generates complete types file for multiple tables with relations 1`] = ` "/** diff --git a/graphql/codegen/src/__tests__/codegen/__snapshots__/model-generator.test.ts.snap b/graphql/codegen/src/__tests__/codegen/__snapshots__/model-generator.test.ts.snap index db345a4e2..dc51d712f 100644 --- a/graphql/codegen/src/__tests__/codegen/__snapshots__/model-generator.test.ts.snap +++ b/graphql/codegen/src/__tests__/codegen/__snapshots__/model-generator.test.ts.snap @@ -1,4 +1,4 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing exports[`model-generator generates model with all CRUD methods 1`] = ` "/** diff --git a/graphql/codegen/src/__tests__/codegen/__snapshots__/query-keys-factory.test.ts.snap b/graphql/codegen/src/__tests__/codegen/__snapshots__/query-keys-factory.test.ts.snap index 9d6e45077..6bf8d8eb1 100644 --- a/graphql/codegen/src/__tests__/codegen/__snapshots__/query-keys-factory.test.ts.snap +++ b/graphql/codegen/src/__tests__/codegen/__snapshots__/query-keys-factory.test.ts.snap @@ -1,4 +1,4 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing exports[`generateInvalidationFile generates invalidation helpers for a single table without relationships 1`] = ` "/** diff --git a/graphql/codegen/src/__tests__/codegen/__snapshots__/react-query-hooks.test.ts.snap b/graphql/codegen/src/__tests__/codegen/__snapshots__/react-query-hooks.test.ts.snap index 1fa921534..1c246200e 100644 --- a/graphql/codegen/src/__tests__/codegen/__snapshots__/react-query-hooks.test.ts.snap +++ b/graphql/codegen/src/__tests__/codegen/__snapshots__/react-query-hooks.test.ts.snap @@ -1,4 +1,4 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing exports[`Barrel File Generators generateCustomMutationsBarrel generates custom mutations barrel 1`] = ` "/** diff --git a/graphql/codegen/src/__tests__/codegen/__snapshots__/schema-types-generator.test.ts.snap b/graphql/codegen/src/__tests__/codegen/__snapshots__/schema-types-generator.test.ts.snap index f3111ec74..e7d4d48d2 100644 --- a/graphql/codegen/src/__tests__/codegen/__snapshots__/schema-types-generator.test.ts.snap +++ b/graphql/codegen/src/__tests__/codegen/__snapshots__/schema-types-generator.test.ts.snap @@ -1,4 +1,4 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing exports[`schema-types-generator generates enum types as string unions 1`] = ` "/**