Skip to content

Commit

Permalink
feat: add virtual module for complete schema
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Oct 26, 2022
1 parent e956ca3 commit a15558f
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 22 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

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

## Features

- 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.

## Installation

```sh
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@
"release": "standard-version && git push --follow-tags && pnpm publish"
},
"dependencies": {
"@graphql-tools/graphql-file-loader": "^7.5.5",
"@graphql-tools/load": "^7.8.0",
"@nuxt/kit": "^3.0.0-rc.12"
},
"peerDependencies": {
"graphql": "^16.6.0"
},
"devDependencies": {
"@nuxt/module-builder": "^0.2.0",
"@nuxt/schema": "^3.0.0-rc.12",
Expand All @@ -38,6 +43,7 @@
"eslint": "^8.26.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-unused-imports": "^2.0.0",
"graphql": "^16.6.0",
"nuxt": "^3.0.0-rc.12",
"prettier": "^2.7.1",
"standard-version": "^9.5.0",
Expand Down
6 changes: 6 additions & 0 deletions playground/server/api/GetSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { schema } from '#graphql/schema'

// API endpoint that exposes the schema as JSON.
// In production, use introspection instead!
// This is here as demonstration and to easily test HMR.
export default defineEventHandler(() => schema)
9 changes: 9 additions & 0 deletions playground/server/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type Book {
title: String
author: Author
}

type Author {
name: String
books: [Book]
}
107 changes: 100 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { resolve } from 'path'
import { fileURLToPath } from 'url'
import { defineNuxtModule, addPlugin } from '@nuxt/kit'
import { defineNuxtModule } from '@nuxt/kit'
import { createSchemaImport } from './schema-loader'

export interface ModuleOptions {
addPlugin: boolean
schema: string | string[]
}

export default defineNuxtModule<ModuleOptions>({
Expand All @@ -12,13 +11,16 @@ export default defineNuxtModule<ModuleOptions>({
configKey: 'myModule',
},
defaults: {
addPlugin: true,
schema: './server/**/*.graphql',
},
setup(options, nuxt) {
if (options.addPlugin) {
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
nuxt.options.build.transpile.push(runtimeDir)
addPlugin(resolve(runtimeDir, 'plugin'))
}
nuxt.hook('nitro:config', async (nitroConfig) => {
// Register #graphql/schema virtual module
nitroConfig.virtual = nitroConfig.virtual || {}
nitroConfig.virtual['#graphql/schema'] = await createSchemaImport(
options.schema,
nitroConfig.rootDir
)
})
},
})
5 changes: 0 additions & 5 deletions src/runtime/plugin.ts

This file was deleted.

31 changes: 31 additions & 0 deletions src/schema-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'
import { loadSchema as loadGraphqlSchema } from '@graphql-tools/load'
import { GraphQLSchema, printSchema } from 'graphql'

/**
* Loads the schema from the GraphQL files.
* @returns the GraphQL schema
*/
export async function loadSchemaFromFiles(
schemaPointers: string | string[],
cwd?: string
): Promise<GraphQLSchema> {
return await loadGraphqlSchema(schemaPointers, {
cwd,
loaders: [new GraphQLFileLoader()],
})
}

export async function createSchemaImport(
schemaPointers: string | string[],
cwd?: string
): Promise<string> {
const schema = await loadSchemaFromFiles(schemaPointers, cwd)
return `
import { loadSchemaSync } from '@graphql-tools/load'
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'
export const schema = loadSchemaSync(\`${printSchema(schema)}\`, {
loaders: [new GraphQLFileLoader()]
})
`
}

0 comments on commit a15558f

Please sign in to comment.