Skip to content

Commit d2ae7d1

Browse files
feat: auto-create app/graphql directory for Nuxt projects (#24)
* feat: auto-create app/graphql directory for Nuxt projects - Automatically create app/graphql directory when framework is Nuxt - Create app/graphql/default subdirectory for new folder structure - Add sample queries.graphql file to help users get started - Fix TypeScript error with optional chaining for external service patterns Fixes the issue where users had to manually create the app/graphql directory in Nuxt projects, now it's created automatically like server/graphql. * fix: add optional chaining to prevent errors when extracting base directory from pattern
1 parent be1db66 commit d2ae7d1

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nitro-graphql",
33
"type": "module",
4-
"version": "1.1.0",
4+
"version": "1.2.0",
55
"packageManager": "pnpm@10.13.1",
66
"description": "GraphQL integration for Nitro",
77
"license": "MIT",

src/index.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ export default defineNitroModule({
9898
for (const service of nitro.options.graphql.externalServices) {
9999
if (service.documents?.length) {
100100
for (const pattern of service.documents) {
101+
if (!pattern)
102+
continue
101103
// Extract directory from pattern for watching
102-
const baseDir = pattern.split('**')[0].replace(/\/$/, '') || '.'
104+
const baseDir = pattern.split('**')[0]?.replace(/\/$/, '') || '.'
103105
const resolvedDir = resolve(nitro.options.rootDir, baseDir)
104106
if (!watchDirs.includes(resolvedDir)) {
105107
watchDirs.push(resolvedDir)
@@ -385,5 +387,34 @@ declare module 'h3' {
385387
consola.warn('nitro-graphql: Found context.d.ts file. Please rename it to context.ts for the new structure.')
386388
consola.info('The context file should now be context.ts instead of context.d.ts')
387389
}
390+
391+
// Create client directory for Nuxt projects
392+
if (nitro.options.framework.name === 'nuxt') {
393+
if (!existsSync(nitro.graphql.clientDir)) {
394+
mkdirSync(nitro.graphql.clientDir, { recursive: true })
395+
}
396+
397+
// Create default subdirectory for the new folder structure
398+
const defaultDir = join(nitro.graphql.clientDir, 'default')
399+
if (!existsSync(defaultDir)) {
400+
mkdirSync(defaultDir, { recursive: true })
401+
}
402+
403+
// Create a sample GraphQL query file to help users get started
404+
const sampleQueryFile = join(defaultDir, 'queries.graphql')
405+
if (!existsSync(sampleQueryFile)) {
406+
writeFileSync(sampleQueryFile, `# Example GraphQL queries
407+
# Add your GraphQL queries here
408+
409+
# query GetUser($id: ID!) {
410+
# user(id: $id) {
411+
# id
412+
# name
413+
# email
414+
# }
415+
# }
416+
`, 'utf-8')
417+
}
418+
}
388419
},
389420
})

0 commit comments

Comments
 (0)