Skip to content

Commit

Permalink
fix(codegen): watch not work
Browse files Browse the repository at this point in the history
  • Loading branch information
Teages committed Mar 5, 2024
1 parent 5dc887c commit b679212
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
1 change: 0 additions & 1 deletion src/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export interface UrqlModuleOptions {
* @description Setup the codegen of GraphQL operations for all clients.
*/
codegen?: false | (CodegenOptions & {

/**
* @description Rerun code generation when the app HMR.
*
Expand Down
61 changes: 28 additions & 33 deletions src/setup/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,49 +211,44 @@ export async function setupCodegen(
const isServer = resolvedPath.startsWith(normalize(rootResolver.resolve(nuxt.options.serverDir)))

const clients = await Promise.all(getClients.map(getClient => getClient()))

const graphqlTags = clients.map(client => client.pluckConfig.globalGqlIdentifierName).flat()

const files = clients.map(client => client.documents).flat()
const extraFiles = extra
.flatMap(doc => glob.sync(doc, { absolute: true }))
.map(file => normalize(file))
files.push(...extraFiles)

if (files.includes(resolvedPath) || (isServer && nitroWatch)) {
const changedFile = await fs.readFile(resolvedPath, 'utf-8')
if (
!(extraFiles.includes(resolvedPath) || graphqlTags.some(tag => changedFile.includes(tag)))
&& !(isServer && nitroWatch)
) {
logger.info('GraphQL codegen skipped')
return
}

const fileHash = hash(changedFile)
if (lock === fileHash) {
return
}
const changedFile = await fs.readFile(resolvedPath, 'utf-8')
if (
!(extraFiles.includes(resolvedPath) || graphqlTags.some(tag => changedFile.includes(tag)))
&& !(isServer && nitroWatch)
) {
logger.info('GraphQL codegen skipped')
return
}

if (isServer && nitroWatch) {
reloadSchema.forEach(async (clientId) => {
const client = options.clients[clientId]
if (!client) {
return
}
const url = client.url
schemaCache.delete(url)
})
}
const fileHash = hash(changedFile)
if (lock === fileHash) {
return
}

logger.start(`GraphQL codegen: ${path}`)
lock = fileHash
await codegen()
await updateTemplates({
filter: ({ filename }) => filename.startsWith('urql-client/codegen/'),
if (isServer && nitroWatch) {
reloadSchema.forEach(async (clientId) => {
const client = options.clients[clientId]
if (!client) {
return
}
const url = client.url
schemaCache.delete(url)
})
lock = undefined
}

logger.start(`GraphQL codegen: ${path}`)
lock = fileHash
await codegen()
await updateTemplates({
filter: ({ filename }) => filename.startsWith('urql-client/codegen/'),
})
lock = undefined
})
}

Expand Down

0 comments on commit b679212

Please sign in to comment.