@@ -6,10 +6,10 @@ import { makeExecutableSchema } from '@graphql-tools/schema'
66import { consola } from 'consola'
77import { defineNitroModule } from 'nitropack/kit'
88import { join } from 'pathe'
9- import { setupClientWatcher } from './client-watcher'
10- import { generateTypes } from './codegen'
9+ // import { generateTypes } from './codegen' // Conditionally imported to prevent bundling
1110import { scanGraphQLFiles } from './scanner'
12- import { setupGraphQLWatcher } from './watcher'
11+
12+ const logger = consola . withTag ( 'graphql' )
1313
1414export default defineNitroModule ( {
1515 name : 'nitro:graphql-yoga' ,
@@ -44,6 +44,31 @@ export default defineNitroModule({
4444
4545 // Access the internal rollup config and add our prefix
4646 nitro . hooks . hook ( 'rollup:before' , ( nitro , rollupConfig ) => {
47+ // Add codegen packages as external dependencies to prevent bundling
48+ rollupConfig . external = rollupConfig . external || [ ]
49+ const codegenExternals = [
50+ '@graphql-codegen/core' ,
51+ '@graphql-codegen/typescript' ,
52+ '@graphql-codegen/typescript-resolvers' ,
53+ '@graphql-codegen/typescript-operations' ,
54+ '@graphql-codegen/typescript-generic-sdk' ,
55+ '@graphql-tools/graphql-file-loader' ,
56+ '@graphql-tools/load' ,
57+ ]
58+
59+ if ( Array . isArray ( rollupConfig . external ) ) {
60+ rollupConfig . external . push ( ...codegenExternals )
61+ }
62+ else if ( typeof rollupConfig . external === 'function' ) {
63+ const originalExternal = rollupConfig . external
64+ rollupConfig . external = ( id , parent , isResolved ) => {
65+ if ( codegenExternals . some ( external => id . includes ( external ) ) ) {
66+ return true
67+ }
68+ return originalExternal ( id , parent , isResolved )
69+ }
70+ }
71+
4772 // Add GraphQL path to chunkNamePrefixes
4873 const originalChunkFileNames = rollupConfig . output . chunkFileNames
4974 rollupConfig . output . chunkFileNames = ( chunk ) => {
@@ -82,24 +107,19 @@ export type { GraphQLContext } from 'nitro-graphql-yoga/context'
82107
83108 // Log resolver discovery for debugging
84109 if ( scanResult . resolvers . length > 0 ) {
85- consola . success ( `[graphql] Found ${ scanResult . resolvers . length } resolvers` )
86-
87- if ( nitro . options . dev ) {
88- consola . info ( '[graphql] Resolver files:' )
89- for ( const resolver of scanResult . resolvers ) {
90- consola . info ( ` ${ resolver . name } -> ${ resolver . relativePath } ` )
91- }
92- }
110+ logger . success ( `Found ${ scanResult . resolvers . length } resolvers` )
93111 }
94112
95- // Generate initial types if we have GraphQL files
113+ // Generate types for both development and build modes
96114 if ( scanResult . typeDefs . length > 0 ) {
97115 const mergedTypeDefs = mergeTypeDefs ( scanResult . typeDefs )
98116 const schema = makeExecutableSchema ( {
99117 typeDefs : mergedTypeDefs ,
100118 resolvers : { } ,
101119 } )
102120
121+ // Use Function constructor to prevent bundling in production
122+ const { generateTypes } = await ( new Function ( 'return import("nitro-graphql-yoga/codegen")' ) ) ( )
103123 const generatedTypes = await generateTypes ( schema )
104124
105125 // Write to file
@@ -119,11 +139,37 @@ declare module 'nitro-graphql-yoga' {
119139`
120140 await writeFile ( graphqlDtsPath , graphqlDtsContent )
121141
122- consola . success ( '[graphql] Generated types at:' , outputPath )
142+ logger . success ( 'Types generated' )
143+ }
144+ else {
145+ // Create minimal types when no schema files found
146+ const typesDir = join ( nitro . options . buildDir , 'types' )
147+ await mkdir ( typesDir , { recursive : true } )
148+
149+ const minimalTypes = `// Generated by nitro-graphql-yoga (no schema found)
150+ export type Resolvers = any
151+ `
152+ const outputPath = join ( typesDir , 'graphql-types.generated.ts' )
153+ await writeFile ( outputPath , minimalTypes )
154+
155+ const graphqlDtsPath = join ( typesDir , 'graphql.d.ts' )
156+ const graphqlDtsContent = `// Auto-generated by nitro-graphql-yoga
157+ import type { Resolvers as Test } from './graphql-types.generated'
158+
159+ declare module 'nitro-graphql-yoga' {
160+ interface Resolvers extends Test {}
161+ }
162+ `
163+ await writeFile ( graphqlDtsPath , graphqlDtsContent )
164+
165+ logger . info ( 'Created minimal types (no schema found)' )
123166 }
124167
125- // Setup file watchers in dev mode
168+ // Setup file watchers in dev mode - completely excluded from production
126169 if ( nitro . options . dev ) {
170+ // Use Function constructor to prevent bundling in production
171+ const setupGraphQLWatcher = ( await ( new Function ( 'return import("nitro-graphql-yoga/watcher")' ) ) ( ) ) . setupGraphQLWatcher
172+ const setupClientWatcher = ( await ( new Function ( 'return import("nitro-graphql-yoga/client-watcher")' ) ) ( ) ) . setupClientWatcher
127173 await setupGraphQLWatcher ( nitro )
128174 await setupClientWatcher ( nitro , options )
129175 }
@@ -165,7 +211,6 @@ import { defineEventHandler, readRawBody, setHeader, setResponseStatus } from 'h
165211import { useStorage } from 'nitro/runtime'
166212import { makeExecutableSchema } from '@graphql-tools/schema'
167213import { mergeTypeDefs, mergeResolvers } from '@graphql-tools/merge'
168- import { loadFilesSync } from '@graphql-tools/load-files'
169214import { join } from 'pathe'
170215// Types are generated at build time to .nitro/graphql-types.generated.ts
171216
@@ -204,26 +249,24 @@ async function loadResolvers() {
204249
205250 if (resolver) {
206251 resolverModules.push(resolver)
207- console.log('[nitro-graphql-yoga] Loaded resolver:', i)
208252 }
209253 } catch (error) {
210- console.warn('[nitro- graphql-yoga ] Failed to load resolver:', i, error.message)
254+ console.warn('[graphql] Failed to load resolver:', i, error.message)
211255 }
212256 }
213257
214258 if (resolverModules.length > 0) {
215259 resolvers = mergeResolvers(resolverModules)
216- console.log('[nitro-graphql-yoga] Successfully merged', resolverModules.length, 'resolvers')
217260 } else {
218- console.warn('[nitro- graphql-yoga ] No resolvers could be loaded')
261+ console.warn('[graphql] No resolvers could be loaded')
219262 resolvers = { Query: {}, Mutation: {} }
220263 }
221264 } else {
222- console.warn('[nitro- graphql-yoga ] No resolvers found')
265+ console.warn('[graphql] No resolvers found')
223266 resolvers = { Query: {}, Mutation: {} }
224267 }
225268 } catch (error) {
226- console.warn('[nitro- graphql-yoga ] Error loading resolvers:', error.message)
269+ console.warn('[graphql] Error loading resolvers:', error.message)
227270 resolvers = { Query: {}, Mutation: {} }
228271 }
229272 return resolvers
0 commit comments