Skip to content

Commit

Permalink
fix: hmr schema watcher had an always truthy conditional check (#67)
Browse files Browse the repository at this point in the history
* fix: multimatch returns an array, so we check for length instead

* chore: use variables to keep template names

* fix: ensure the glob pattern is correct for project

* fix: bug from incomplete name

* add handling of absolute and relative paths

---------

Co-authored-by: Tobias Diez <code@tobiasdiez.de>
Co-authored-by: Tobias Diez <code@tobiasdiez.com>
  • Loading branch information
3 people committed Sep 3, 2023
1 parent b4591ce commit 4104b50
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface ModuleOptions {
}

const logger = useLogger('graphql/server')
// Activate this to see debug logs if you run `pnpm dev --loglevel verbose`
// logger.level = 5

function setAlias(nuxt: Nuxt, alias: string, path: string) {
// workaround for https://github.com/nuxt/nuxt/issues/19453
Expand Down Expand Up @@ -60,10 +62,13 @@ export default defineNuxtModule<ModuleOptions>({
const { resolve } = createResolver(import.meta.url)

// Register #graphql/schema virtual module
const schemaPathTemplateName = 'graphql-schema.mjs'
const { dst: schemaPath } = addTemplate({
filename: 'graphql-schema.mjs',
getContents: () =>
createSchemaImport(options.schema, nuxt.options.rootDir),
filename: schemaPathTemplateName,
getContents: () => {
logger.debug(`Generating ${schemaPathTemplateName}`)
return createSchemaImport(options.schema, nuxt.options.rootDir)
},
write: true,
})
logger.debug(`GraphQL schema registered at ${schemaPath}`)
Expand All @@ -74,10 +79,11 @@ export default defineNuxtModule<ModuleOptions>({
filename: 'types/graphql-server.d.ts',
src: resolve('graphql-server.d.ts'),
})
const resolverTypesTemplateName = 'types/graphql-server-resolver.d.ts'
const { dst: resolverTypeDefPath } = addTemplate({
filename: 'types/graphql-server-resolver.d.ts',
filename: resolverTypesTemplateName,
getContents: () => {
logger.debug('Generating graphql-server-resolver.d.ts')
logger.debug(`Generating ${resolverTypesTemplateName}`)
return createResolverTypeDefs(
options.schema,
options.codegen ?? {},
Expand All @@ -103,14 +109,22 @@ export default defineNuxtModule<ModuleOptions>({
if (nuxt.options.dev) {
nuxt.hook('nitro:build:before', (nitro) => {
nuxt.hook('builder:watch', async (event, path) => {
if (multimatch(path, options.schema)) {
logger.debug('Schema changed', path)
logger.debug('File changed', path)
// Depending on the nuxt version, path is either absolute or relative
const absolutePath = resolve(nuxt.options.srcDir, path)
const schema = Array.isArray(options.schema)
? options.schema.map((pattern) =>
resolve(nuxt.options.srcDir, pattern),
)
: resolve(nuxt.options.srcDir, options.schema)
if (multimatch(absolutePath, schema).length > 0) {
logger.debug('Schema changed', absolutePath)

// Update templates
await updateTemplates({
filter: (template) =>
template.filename.startsWith('types/graphql-server') ||
template.filename === 'graphql-schema.mjs',
template.filename === resolverTypesTemplateName ||
template.filename === schemaPathTemplateName,
})

// Reload nitro dev server
Expand Down

0 comments on commit 4104b50

Please sign in to comment.