diff --git a/docs/introduction/setup.md b/docs/introduction/setup.md index 6a875ec..fa7bdb4 100644 --- a/docs/introduction/setup.md +++ b/docs/introduction/setup.md @@ -16,14 +16,12 @@ Add the module to your Nuxt config and provide the basic configuration. // ./nuxt.config.ts import { defineNuxtConfig } from 'nuxt' -const IS_DEV = process.env.NODE_ENV === 'development' - export default defineNuxtConfig({ modules: ['nuxt-graphql-middleware'], graphqlMiddleware: { graphqlEndpoint: 'https://example.com/graphql', - downloadSchema: IS_DEV, + downloadSchema: 'dev-only', }, }) ``` diff --git a/package-lock.json b/package-lock.json index 4a457ca..5768ac7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "nuxt-graphql-middleware", - "version": "5.3.1", + "version": "5.3.2", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package.json b/package.json index 1d4bf1f..bfceb64 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nuxt-graphql-middleware", - "version": "5.3.1", + "version": "5.3.2", "description": "Module to perform GraphQL requests as a server middleware.", "author": "Jan Hug ", "license": "MIT", diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index a148baa..22a806d 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -1,11 +1,10 @@ import { defineNuxtConfig } from 'nuxt/config' import graphqlMiddlewareModule from './../src/module' import type { ModuleOptions } from '../src/build/types/options' -const IS_DEV = process.env.NODE_ENV === 'development' const graphqlMiddleware: ModuleOptions = { graphqlEndpoint: 'http://localhost:4000', - downloadSchema: IS_DEV, + downloadSchema: 'dev-only', autoImportPatterns: [ '~/**/*.{graphql,gql}', '!queryFromDisk.graphql', diff --git a/src/build/ModuleHelper.ts b/src/build/ModuleHelper.ts index b9a7e52..55e82b8 100644 --- a/src/build/ModuleHelper.ts +++ b/src/build/ModuleHelper.ts @@ -93,6 +93,8 @@ export class ModuleHelper { options: ModuleOptions, ) { this.isPrepare = nuxt.options._prepare + this.isDev = nuxt.options.dev + const isModuleBuild = process.env.PLAYGROUND_MODULE_BUILD === 'true' && this.isPrepare @@ -183,7 +185,6 @@ export class ModuleHelper { validateOptions(this.options) } - this.isDev = nuxt.options.dev this.resolvers = { module: createResolver(moduleUrl), server: createResolver(nuxt.options.serverDir), @@ -215,6 +216,17 @@ export class ModuleHelper { this.paths.serverOptions = this.findServerOptions() } + /** + * Whether the schema should be downloaded. + */ + public shouldDownloadSchema(): boolean { + if (this.options.downloadSchema === 'dev-only') { + return this.isDev + } + + return this.options.downloadSchema + } + /** * Find the path to the graphqlMiddleware.serverOptions.ts file. */ diff --git a/src/build/SchemaProvider.ts b/src/build/SchemaProvider.ts index 87229ed..f85b2ae 100644 --- a/src/build/SchemaProvider.ts +++ b/src/build/SchemaProvider.ts @@ -41,7 +41,7 @@ export class SchemaProvider { if ( this.helper.isDev && hasSchemaOnDisk && - this.helper.options.downloadSchema + this.helper.shouldDownloadSchema() ) { const shouldUseFromDisk = await this.helper.prompt.confirm( 'Do you want to continue with the previously downloaded schema from disk?', @@ -145,9 +145,9 @@ export class SchemaProvider { forceDownload?: boolean forceDisk?: boolean }) { - if (opts?.forceDisk || this.helper.isPrepare) { + if (opts?.forceDisk) { this.schemaContent = await this.loadSchemaFromDisk() - } else if (this.helper.options.downloadSchema || opts?.forceDownload) { + } else if (this.helper.shouldDownloadSchema() || opts?.forceDownload) { this.schemaContent = await this.downloadSchema() } else { this.schemaContent = await this.loadSchemaFromDisk() diff --git a/src/build/types/options.ts b/src/build/types/options.ts index aae3483..75ca6ae 100644 --- a/src/build/types/options.ts +++ b/src/build/types/options.ts @@ -99,13 +99,16 @@ export interface ModuleOptions { graphqlEndpoint?: string /** - * Download the GraphQL schema and store it on disk. + * Whether to download the GraphQL schema and store it on disk. * - * Usually you'll want to only enable this during dev mode. + * - `false` The GraphQL schema must exist on disk for the module to work. + * - `true` The GraphQL schema will be downloaded and stored on disk. + * - `'dev-only'` When running `npm run dev` the schema is downloaded, + * otherwise it is read from disk. * * @default true */ - downloadSchema?: boolean + downloadSchema?: boolean | 'dev-only' /** * Path to the GraphQL schema file.