Skip to content

Commit 14c3198

Browse files
fix: use import.meta.resolve() to convert module specifiers to absolute paths
This ensures route handler paths are correctly resolved both in local development (monorepo symlinks) and in CI/production (npm install). The previous approach used bare module specifiers like 'nitro-graphql/nitro/routes/graphql-yoga' which rolldown couldn't resolve due to missing tsconfig. Now we use import.meta.resolve() at config time to convert these to absolute filesystem paths that the bundler can handle. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 3b0686e commit 14c3198

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/nitro/setup/routes.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@
33
*/
44

55
import type { Nitro } from 'nitro/types'
6+
import { fileURLToPath } from 'node:url'
67
import { ENDPOINT_DEBUG, GRAPHQL_HTTP_METHODS } from '../../core/constants'
78

9+
/**
10+
* Resolve a module specifier to an absolute filesystem path.
11+
* Uses import.meta.resolve() to handle both:
12+
* - Local development (monorepo symlinks resolve to local source)
13+
* - CI/Production (npm install resolves to node_modules)
14+
*/
15+
function resolveHandler(specifier: string): string {
16+
return fileURLToPath(import.meta.resolve(specifier))
17+
}
18+
819
/**
920
* Register GraphQL route handlers
1021
*
11-
* Uses module specifiers instead of filesystem paths for route handlers.
22+
* Uses import.meta.resolve() to convert module specifiers to absolute paths.
1223
* This ensures handlers are resolved correctly both in local development
1324
* (monorepo symlink) and in CI/production (installed from npm).
1425
*/
@@ -22,15 +33,15 @@ export function registerRouteHandlers(nitro: Nitro): void {
2233
for (const method of GRAPHQL_HTTP_METHODS) {
2334
nitro.options.handlers.push({
2435
route: endpoint,
25-
handler: 'nitro-graphql/nitro/routes/graphql-yoga',
36+
handler: resolveHandler('nitro-graphql/nitro/routes/graphql-yoga'),
2637
method,
2738
})
2839
}
2940

3041
// Apollo Sandbox script proxy (cacheable)
3142
nitro.options.handlers.push({
3243
route: `${endpoint}/sandbox.js`,
33-
handler: 'nitro-graphql/nitro/routes/apollo-sandbox-script',
44+
handler: resolveHandler('nitro-graphql/nitro/routes/apollo-sandbox-script'),
3445
method: 'GET',
3546
})
3647

@@ -40,7 +51,7 @@ export function registerRouteHandlers(nitro: Nitro): void {
4051
const wsPath = subscriptions.websocket?.path || `${endpoint}/ws`
4152
nitro.options.handlers.push({
4253
route: wsPath,
43-
handler: 'nitro-graphql/nitro/routes/graphql-yoga-ws',
54+
handler: resolveHandler('nitro-graphql/nitro/routes/graphql-yoga-ws'),
4455
})
4556
}
4657
}
@@ -49,7 +60,7 @@ export function registerRouteHandlers(nitro: Nitro): void {
4960
for (const method of GRAPHQL_HTTP_METHODS) {
5061
nitro.options.handlers.push({
5162
route: endpoint,
52-
handler: 'nitro-graphql/nitro/routes/apollo-server',
63+
handler: resolveHandler('nitro-graphql/nitro/routes/apollo-server'),
5364
method,
5465
})
5566
}
@@ -60,23 +71,23 @@ export function registerRouteHandlers(nitro: Nitro): void {
6071
const wsPath = subscriptions.websocket?.path || `${endpoint}/ws`
6172
nitro.options.handlers.push({
6273
route: wsPath,
63-
handler: 'nitro-graphql/nitro/routes/apollo-server-ws',
74+
handler: resolveHandler('nitro-graphql/nitro/routes/apollo-server-ws'),
6475
})
6576
}
6677
}
6778

6879
// Health check endpoint
6980
nitro.options.handlers.push({
7081
route: nitro.options.runtimeConfig.graphql?.endpoint?.healthCheck || '/api/graphql/health',
71-
handler: 'nitro-graphql/nitro/routes/health',
82+
handler: resolveHandler('nitro-graphql/nitro/routes/health'),
7283
method: 'GET',
7384
})
7485

7586
// Debug endpoint (development only)
7687
if (nitro.options.dev) {
7788
nitro.options.handlers.push({
7889
route: ENDPOINT_DEBUG,
79-
handler: 'nitro-graphql/nitro/routes/debug',
90+
handler: resolveHandler('nitro-graphql/nitro/routes/debug'),
8091
method: 'GET',
8192
})
8293
}

0 commit comments

Comments
 (0)