Skip to content

Commit 09eb43b

Browse files
author
unknown
committed
refactor(codegen): elevate nodeHttpAdapter to top-level generation option
- Move nodeHttpAdapter from CliConfig to GraphQLSDKConfigTarget - Auto-enable nodeHttpAdapter when CLI generation is enabled (unless explicitly false) - Generate node-fetch.ts in ORM output when nodeHttpAdapter is enabled - Export NodeHttpAdapter from ORM index.ts for standalone Node.js usage - Pass nodeHttpAdapter to multi-target CLI generator - Update template comment to reflect general Node.js usage (not CLI-specific) This allows any Node.js application (ORM, CLI, or custom) to use NodeHttpAdapter for localhost subdomain routing without needing CLI generation enabled.
1 parent 8916fcd commit 09eb43b

6 files changed

Lines changed: 65 additions & 15 deletions

File tree

graphql/codegen/src/core/codegen/cli/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export function generateCli(options: GenerateCliOptions): GenerateCliResult {
4545
? cliConfig.toolName
4646
: 'app';
4747

48-
const useNodeHttpAdapter =
49-
typeof cliConfig === 'object' && !!cliConfig.nodeHttpAdapter;
48+
// Use top-level nodeHttpAdapter from config (auto-enabled for CLI by generate.ts)
49+
const useNodeHttpAdapter = !!config.nodeHttpAdapter;
5050

5151
const executorFile = generateExecutorFile(toolName, {
5252
nodeHttpAdapter: useNodeHttpAdapter,

graphql/codegen/src/core/codegen/orm/client-generator.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export function generateCreateClientFile(
122122
tables: CleanTable[],
123123
hasCustomQueries: boolean,
124124
hasCustomMutations: boolean,
125+
options?: { nodeHttpAdapter?: boolean },
125126
): GeneratedClientFile {
126127
const statements: t.Statement[] = [];
127128

@@ -215,6 +216,22 @@ export function generateCreateClientFile(
215216
// Re-export all models
216217
statements.push(t.exportAllDeclaration(t.stringLiteral('./models')));
217218

219+
// Re-export NodeHttpAdapter when enabled (for use in any Node.js application)
220+
if (options?.nodeHttpAdapter) {
221+
statements.push(
222+
t.exportNamedDeclaration(
223+
null,
224+
[
225+
t.exportSpecifier(
226+
t.identifier('NodeHttpAdapter'),
227+
t.identifier('NodeHttpAdapter'),
228+
),
229+
],
230+
t.stringLiteral('./node-fetch'),
231+
),
232+
);
233+
}
234+
218235
// Re-export custom operations
219236
if (hasCustomQueries) {
220237
statements.push(

graphql/codegen/src/core/codegen/orm/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ export function generateOrm(options: GenerateOrmOptions): GenerateOrmResult {
170170
tables,
171171
hasCustomQueries,
172172
hasCustomMutations,
173+
{ nodeHttpAdapter: !!options.config.nodeHttpAdapter },
173174
);
174175
files.push({ path: indexFile.fileName, content: indexFile.content });
175176

graphql/codegen/src/core/codegen/templates/node-fetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Node HTTP Adapter for CLI
2+
* Node HTTP Adapter for Node.js applications
33
*
44
* Implements the GraphQLAdapter interface using node:http / node:https
55
* instead of the Fetch API. This solves two Node.js limitations:

graphql/codegen/src/core/generate.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ export async function generate(
109109
const runOrm =
110110
runReactQuery || !!config.cli || (options.orm !== undefined ? !!options.orm : false);
111111

112+
// Auto-enable nodeHttpAdapter when CLI is enabled, unless explicitly set to false
113+
const useNodeHttpAdapter =
114+
options.nodeHttpAdapter === true ||
115+
(runCli && options.nodeHttpAdapter !== false);
116+
112117
if (!options.schemaOnly && !runReactQuery && !runOrm && !runCli) {
113118
return {
114119
success: false,
@@ -256,7 +261,7 @@ export async function generate(
256261
mutations: customOperations.mutations,
257262
typeRegistry: customOperations.typeRegistry,
258263
},
259-
config,
264+
config: { ...config, nodeHttpAdapter: useNodeHttpAdapter },
260265
sharedTypesPath: bothEnabled ? '..' : undefined,
261266
});
262267
filesToWrite.push(
@@ -265,6 +270,16 @@ export async function generate(
265270
path: path.posix.join('orm', file.path),
266271
})),
267272
);
273+
274+
// Generate NodeHttpAdapter in ORM output when enabled
275+
if (useNodeHttpAdapter) {
276+
const { generateNodeFetchFile } = await import('./codegen/cli/utils-generator');
277+
const nodeFetchFile = generateNodeFetchFile();
278+
filesToWrite.push({
279+
path: path.posix.join('orm', nodeFetchFile.fileName),
280+
content: nodeFetchFile.content,
281+
});
282+
}
268283
}
269284

270285
// Generate CLI commands
@@ -276,7 +291,7 @@ export async function generate(
276291
queries: customOperations.queries,
277292
mutations: customOperations.mutations,
278293
},
279-
config,
294+
config: { ...config, nodeHttpAdapter: useNodeHttpAdapter },
280295
});
281296
filesToWrite.push(
282297
...files.map((file) => ({
@@ -669,10 +684,18 @@ export async function generateMulti(
669684
if (useUnifiedCli && cliTargets.length > 0 && !dryRun) {
670685
const cliConfig = typeof unifiedCli === 'object' ? unifiedCli : {};
671686
const toolName = cliConfig.toolName ?? 'app';
687+
// Auto-enable nodeHttpAdapter for unified CLI unless explicitly disabled
688+
// Check first target config for explicit nodeHttpAdapter setting
689+
const firstTargetConfig = configs[names[0]];
690+
const multiNodeHttpAdapter =
691+
firstTargetConfig?.nodeHttpAdapter === true ||
692+
(firstTargetConfig?.nodeHttpAdapter !== false);
693+
672694
const { files } = generateMultiTargetCli({
673695
toolName,
674696
builtinNames: cliConfig.builtinNames,
675697
targets: cliTargets,
698+
nodeHttpAdapter: multiNodeHttpAdapter,
676699
});
677700

678701
const cliFilesToWrite = files.map((file) => ({

graphql/codegen/src/types/config.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,6 @@ export interface CliConfig {
199199
* context -> 'context' (renamed to 'env' on collision)
200200
*/
201201
builtinNames?: BuiltinNames;
202-
203-
/**
204-
* Enable NodeHttpAdapter for *.localhost subdomain routing.
205-
* When true, generates a node-fetch.ts with NodeHttpAdapter (implements GraphQLAdapter)
206-
* using node:http/node:https for requests, enabling local development
207-
* with subdomain-based routing (e.g. auth.localhost:3000).
208-
* No global patching — the adapter is passed to createClient via the adapter option.
209-
* @default false
210-
*/
211-
nodeHttpAdapter?: boolean;
212202
}
213203

214204
/**
@@ -333,6 +323,25 @@ export interface GraphQLSDKConfigTarget {
333323
*/
334324
orm?: boolean;
335325

326+
/**
327+
* Enable NodeHttpAdapter generation for Node.js applications.
328+
* When true, generates a node-fetch.ts with NodeHttpAdapter (implements GraphQLAdapter)
329+
* using node:http/node:https for requests, enabling local development
330+
* with subdomain-based routing (e.g. auth.localhost:3000).
331+
* No global patching — the adapter is passed to createClient via the adapter option.
332+
*
333+
* When CLI generation is enabled (`cli: true`), this is auto-enabled unless
334+
* explicitly set to `false`.
335+
*
336+
* Can also be used standalone with the ORM client for any Node.js application:
337+
* ```ts
338+
* import { NodeHttpAdapter } from './orm/node-fetch';
339+
* const db = createClient({ adapter: new NodeHttpAdapter(endpoint, headers) });
340+
* ```
341+
* @default false
342+
*/
343+
nodeHttpAdapter?: boolean;
344+
336345
/**
337346
* Whether to generate React Query hooks
338347
* When enabled, generates React Query hooks to {output}/hooks

0 commit comments

Comments
 (0)