Skip to content

Commit

Permalink
fix: refactor GraphQLYoga module to generate GraphQL template dynamic…
Browse files Browse the repository at this point in the history
…ally
  • Loading branch information
productdevbook committed Feb 13, 2024
1 parent 99ea9a2 commit 9189ace
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
14 changes: 9 additions & 5 deletions packages/nuxt/src/runtime/modules/graphqlYoga/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { useNitroImports } from '../../core/utils/useImports'
import { generateModuleRuntimeConfig } from '../../core/utils/moduleRuntimeConfig'
import { writeFilePergel } from '../../core/utils/writeFilePergel'
import type { GraphQLYogaConfig, ResolvedGraphQLYogaConfig } from './types'
import { generateGraphQLTemplate } from './utils/generateGraphqlTemplate'

// const _logger = useLogger('pergel:graphql:yoga')

Expand Down Expand Up @@ -256,10 +255,15 @@ export default definePergelModule<GraphQLYogaConfig, ResolvedGraphQLYogaConfig>(
graphqlYoga: graphqlYoga,
`,
})
if (!existsSync(join(options.folderName, 'generated', 'schema.mjs'))) {
const generateGraphQLTemplate = await import('./utils/generateGraphqlTemplate').then(m => m.generateGraphQLTemplate).catch(() => undefined)

generateGraphQLTemplate({
nuxt,
options,
})
if (generateGraphQLTemplate) {
generateGraphQLTemplate({
nuxt,
options,
})
}
}
},
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { join } from 'node:path'
import { existsSync } from 'node:fs'
import { matchGlobs } from '../utils'
import type { ResolvedGraphQLYogaConfig } from '../types'
import { addModuleDTS } from '../../../core/utils/addModuleDTS'
Expand Down Expand Up @@ -32,43 +31,41 @@ export interface GraphqlYogaContext extends YogaInitialContext {
dir: data.options.serverDir,
})

if (!existsSync(join(data.options.folderName, 'generated', 'schema.mjs'))) {
const { updatesFunction } = useGenerateCodegen({
nuxt: data.nuxt,
options: data.options,
moduleDTS: {
name: 'GraphqlYogaContext',
path: `pergel/${data.options.projectName}/types`,
},
})
const { updatesFunction } = useGenerateCodegen({
nuxt: data.nuxt,
options: data.options,
moduleDTS: {
name: 'GraphqlYogaContext',
path: `pergel/${data.options.projectName}/types`,
},
})

if (data.nuxt.options.dev) {
data.nuxt.hook('builder:watch', async (event, path) => {
const test = globsBuilderWatch(data.nuxt, path, '.graphql')
if (!test)
return
if (data.nuxt.options.dev) {
data.nuxt.hook('builder:watch', async (event, path) => {
const test = globsBuilderWatch(data.nuxt, path, '.graphql')
if (!test)
return

// TODO: globsBuilderWatch add dynamic function
const serverFolder = matchGlobs(test.match.filepath, [join('**', dir.schema, '**', `*${codegen.server.extension}`)])
const clientFolder = matchGlobs(test.match.filepath, [join('**', dir.document, '**', `*${codegen.client.extension}`)])
// TODO: globsBuilderWatch add dynamic function
const serverFolder = matchGlobs(test.match.filepath, [join('**', dir.schema, '**', `*${codegen.server.extension}`)])
const clientFolder = matchGlobs(test.match.filepath, [join('**', dir.document, '**', `*${codegen.client.extension}`)])

// return
if (serverFolder) {
// return
if (serverFolder) {
// If change server, and update schema.graphql and after update client auto. Maybe change this in future.
await updatesFunction({
type: 'server',
})
}
if (clientFolder) {
await updatesFunction({
type: 'client',
})
}

await updatesFunction({
type: 'all',
type: 'server',
})
}
if (clientFolder) {
await updatesFunction({
type: 'client',
})
}

await updatesFunction({
type: 'all',
})
}
})
}
}

0 comments on commit 9189ace

Please sign in to comment.