Skip to content

Commit a15558f

Browse files
committed
feat: add virtual module for complete schema
1 parent e956ca3 commit a15558f

File tree

8 files changed

+168
-22
lines changed

8 files changed

+168
-22
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
This package allows you to easily develop a GraphQL server in your [nuxt](v3.nuxtjs.org) application.
99

10+
## Features
11+
12+
- Provides a virtual module `#graphql/schema` from where you can import your schema. Under the hood, it automatically merges multiple schema files together into a complete schema. Moreover, you no longer need to worry about deploying schema `graphql` files.
13+
1014
## Installation
1115

1216
```sh

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@
2727
"release": "standard-version && git push --follow-tags && pnpm publish"
2828
},
2929
"dependencies": {
30+
"@graphql-tools/graphql-file-loader": "^7.5.5",
31+
"@graphql-tools/load": "^7.8.0",
3032
"@nuxt/kit": "^3.0.0-rc.12"
3133
},
34+
"peerDependencies": {
35+
"graphql": "^16.6.0"
36+
},
3237
"devDependencies": {
3338
"@nuxt/module-builder": "^0.2.0",
3439
"@nuxt/schema": "^3.0.0-rc.12",
@@ -38,6 +43,7 @@
3843
"eslint": "^8.26.0",
3944
"eslint-config-prettier": "^8.5.0",
4045
"eslint-plugin-unused-imports": "^2.0.0",
46+
"graphql": "^16.6.0",
4147
"nuxt": "^3.0.0-rc.12",
4248
"prettier": "^2.7.1",
4349
"standard-version": "^9.5.0",

playground/server/api/GetSchema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { schema } from '#graphql/schema'
2+
3+
// API endpoint that exposes the schema as JSON.
4+
// In production, use introspection instead!
5+
// This is here as demonstration and to easily test HMR.
6+
export default defineEventHandler(() => schema)

playground/server/schema.graphql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type Book {
2+
title: String
3+
author: Author
4+
}
5+
6+
type Author {
7+
name: String
8+
books: [Book]
9+
}

pnpm-lock.yaml

Lines changed: 100 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/module.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { resolve } from 'path'
2-
import { fileURLToPath } from 'url'
3-
import { defineNuxtModule, addPlugin } from '@nuxt/kit'
1+
import { defineNuxtModule } from '@nuxt/kit'
2+
import { createSchemaImport } from './schema-loader'
43

54
export interface ModuleOptions {
6-
addPlugin: boolean
5+
schema: string | string[]
76
}
87

98
export default defineNuxtModule<ModuleOptions>({
@@ -12,13 +11,16 @@ export default defineNuxtModule<ModuleOptions>({
1211
configKey: 'myModule',
1312
},
1413
defaults: {
15-
addPlugin: true,
14+
schema: './server/**/*.graphql',
1615
},
1716
setup(options, nuxt) {
18-
if (options.addPlugin) {
19-
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
20-
nuxt.options.build.transpile.push(runtimeDir)
21-
addPlugin(resolve(runtimeDir, 'plugin'))
22-
}
17+
nuxt.hook('nitro:config', async (nitroConfig) => {
18+
// Register #graphql/schema virtual module
19+
nitroConfig.virtual = nitroConfig.virtual || {}
20+
nitroConfig.virtual['#graphql/schema'] = await createSchemaImport(
21+
options.schema,
22+
nitroConfig.rootDir
23+
)
24+
})
2325
},
2426
})

src/runtime/plugin.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/schema-loader.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'
2+
import { loadSchema as loadGraphqlSchema } from '@graphql-tools/load'
3+
import { GraphQLSchema, printSchema } from 'graphql'
4+
5+
/**
6+
* Loads the schema from the GraphQL files.
7+
* @returns the GraphQL schema
8+
*/
9+
export async function loadSchemaFromFiles(
10+
schemaPointers: string | string[],
11+
cwd?: string
12+
): Promise<GraphQLSchema> {
13+
return await loadGraphqlSchema(schemaPointers, {
14+
cwd,
15+
loaders: [new GraphQLFileLoader()],
16+
})
17+
}
18+
19+
export async function createSchemaImport(
20+
schemaPointers: string | string[],
21+
cwd?: string
22+
): Promise<string> {
23+
const schema = await loadSchemaFromFiles(schemaPointers, cwd)
24+
return `
25+
import { loadSchemaSync } from '@graphql-tools/load'
26+
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'
27+
export const schema = loadSchemaSync(\`${printSchema(schema)}\`, {
28+
loaders: [new GraphQLFileLoader()]
29+
})
30+
`
31+
}

0 commit comments

Comments
 (0)