Skip to content

Commit 401a463

Browse files
feat: implement schema hot reload for development mode
- Add dynamic schema loading with loadTypeDefs function - Enable schema reloading in development mode for hot updates - Reset yoga instance in dev mode to pick up schema changes - Preserve production caching for performance - Fix issue where GraphQL schema changes weren't reflected at runtime
1 parent 073ca58 commit 401a463

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/index.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,14 @@ globalThis.createResolver = function(resolvers) {
148148
return resolvers
149149
}
150150
151-
// Load schema files
152-
const schemaPath = join('${nitro.options.srcDir}', 'graphql', '**', '*.graphql')
153-
const typeDefs = loadFilesSync(schemaPath, {
154-
recursive: true,
155-
})
151+
// Dynamic schema loading function
152+
async function loadTypeDefs() {
153+
const schemaPath = join('${nitro.options.srcDir}', 'graphql', '**', '*.graphql')
154+
const { loadFilesSync } = await import('@graphql-tools/load-files')
155+
return loadFilesSync(schemaPath, {
156+
recursive: true,
157+
})
158+
}
156159
157160
// Load resolvers using dynamic imports (Nitro handles the bundling)
158161
const resolverImports = [
@@ -229,11 +232,19 @@ let yoga = null
229232
let initPromise = null
230233
231234
async function getYoga() {
232-
if (yoga) return yoga
235+
// In development mode, always reload schema for hot updates
236+
const isDev = ${nitro.options.dev}
237+
if (yoga && !isDev) return yoga
233238
234-
if (!initPromise) {
239+
if (!initPromise || isDev) {
240+
// Reset yoga instance in development mode
241+
if (isDev) {
242+
yoga = null
243+
}
244+
235245
initPromise = (async () => {
236246
const resolvers = await loadResolvers()
247+
const typeDefs = await loadTypeDefs()
237248
238249
// Merge schema and resolvers
239250
const schema = makeExecutableSchema({

0 commit comments

Comments
 (0)