Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions docs/introduction/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
})
```
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <me@dulnan.net>",
"license": "MIT",
Expand Down
3 changes: 1 addition & 2 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
14 changes: 13 additions & 1 deletion src/build/ModuleHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/build/SchemaProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?',
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions src/build/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading