Skip to content

Commit 3d62a37

Browse files
feat: create default GraphQL server directory and example files (#7)
* feat: create default GraphQL server directory and example files if they do not exist * fix: remove early return when no GraphQL framework is specified
1 parent 39911a9 commit 3d62a37

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

src/index.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Nitro } from 'nitropack/types'
22
import type { NitroGraphQLOptions } from './types'
3-
import { existsSync, writeFileSync } from 'node:fs'
3+
import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
44
import { fileURLToPath } from 'node:url'
55
import { watch } from 'chokidar'
66
import consola from 'consola'
@@ -21,7 +21,6 @@ export default defineNitroModule({
2121
async setup(nitro: Nitro) {
2222
if (!nitro.options.graphql?.framework) {
2323
consola.warn('No GraphQL framework specified. Please set graphql.framework to "graphql-yoga" or "apollo-server".')
24-
return
2524
}
2625

2726
nitro.graphql ||= {
@@ -266,6 +265,48 @@ export default <IGraphQLConfig> {
266265
],
267266
},
268267
},
268+
}`, 'utf-8')
269+
}
270+
271+
if (!existsSync(nitro.graphql.serverDir)) {
272+
mkdirSync(nitro.graphql.serverDir, { recursive: true })
273+
}
274+
275+
if (!existsSync(join(nitro.graphql.serverDir, 'schema.ts'))) {
276+
writeFileSync(join(nitro.graphql.serverDir, 'schema.ts'), `export default defineSchema({
277+
278+
})
279+
`, 'utf-8')
280+
}
281+
282+
if (!existsSync(join(nitro.graphql.serverDir, 'config.ts'))) {
283+
writeFileSync(join(nitro.graphql.serverDir, 'config.ts'), `// Example GraphQL config file please change it to your needs
284+
// import * as tables from '../drizzle/schema/index'
285+
// import { useDatabase } from '../utils/useDb'
286+
287+
export default defineGraphQLConfig({
288+
// graphql-yoga example config
289+
// context: () => {
290+
// return {
291+
// context: {
292+
// useDatabase,
293+
// tables,
294+
// },
295+
// }
296+
// },
297+
})
298+
`, 'utf-8')
299+
}
300+
301+
if (!existsSync(join(nitro.graphql.serverDir, 'context.d.ts'))) {
302+
writeFileSync(join(nitro.graphql.serverDir, 'context.d.ts'), `// Example context definition please change it to your needs
303+
// import type { Database } from '../utils/useDb'
304+
305+
declare module 'h3' {
306+
interface H3EventContext {
307+
// useDatabase: () => Database
308+
// tables: typeof import('~~/server/drizzle/schema/index')
309+
}
269310
}`, 'utf-8')
270311
}
271312
},

0 commit comments

Comments
 (0)