diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..92f279e --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +v22 \ No newline at end of file diff --git a/docs/configuration/module.md b/docs/configuration/module.md index 877785d..2d9f3fd 100644 --- a/docs/configuration/module.md +++ b/docs/configuration/module.md @@ -146,11 +146,12 @@ operations types. [Check out `@graphql-codegen/typescript-operations` for all available options](https://www.the-guild.dev/graphql/codegen/plugins/typescript/typescript-operations) -## outputDocuments: boolean +## outputDocuments: boolean | string -Output the compiled documents to disk. Path is +Output the compiled documents to disk. If boolean then path is $buildDir/nuxt-graphql-middleware/documents, usually -`/.nuxt/nuxt-graphql-middleware/documents`. +`/.nuxt/nuxt-graphql-middleware/documents` else we use `outputDocuments` as the +path. ### default diff --git a/package-lock.json b/package-lock.json index f809108..41a78bb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "nuxt-graphql-middleware", - "version": "4.2.0", + "version": "4.3.0", "license": "MIT", "dependencies": { "@graphql-codegen/cli": "^5.0.2", diff --git a/package.json.md b/package.json.md new file mode 100644 index 0000000..739ce01 --- /dev/null +++ b/package.json.md @@ -0,0 +1,13 @@ +This file contains explanation and help around code in package.json + +# scritps + +For the following commands to work you need to execute npm run dev:prepare at +least once + +- prepack +- lint +- typecheck +- test + +To create a build run `npm run prepack` diff --git a/src/helpers/index.ts b/src/helpers/index.ts index 6d707d7..0b01fc0 100644 --- a/src/helpers/index.ts +++ b/src/helpers/index.ts @@ -19,8 +19,9 @@ import { generateSchema, generateTemplates } from './../codegen' import { type GraphqlMiddlewareDocument } from './../types' import { type ModuleOptions } from './../module' import { logDocuments } from './reporter' +import { name } from '../../package.json' -export const logger = useLogger('nuxt-graphql-middleware') +export const logger = useLogger(name) export const defaultOptions: ModuleOptions = { codegenConfig: { @@ -494,3 +495,19 @@ export async function outputDocuments( } }) } + +export async function getOutputDocumentsPath( + optionsOutputDocuments: ModuleOptions['outputDocuments'], + nuxtBuildDir: string, + resolvePath: Resolver['resolvePath'], +): Promise { + if (!optionsOutputDocuments) { + return null + } + + if (typeof optionsOutputDocuments === 'boolean') { + return resolve(nuxtBuildDir, `${name}/documents`) + } else { + return await resolvePath(optionsOutputDocuments) + } +} diff --git a/src/module.ts b/src/module.ts index d23c796..fb06eab 100644 --- a/src/module.ts +++ b/src/module.ts @@ -29,6 +29,7 @@ import { logger, fileExists, outputDocuments, + getOutputDocumentsPath, } from './helpers' import { type CodegenResult } from './codegen' import { type ClientFunctions, type ServerFunctions } from './rpc-types' @@ -207,8 +208,9 @@ export interface ModuleOptions { /** * Set to true if you want to output each compiled query and mutation in the * .nuxt folder. + * Set to a path to output to a custom path. */ - outputDocuments?: boolean + outputDocuments?: boolean | string /** * Enable Nuxt DevTools integration. @@ -354,17 +356,19 @@ export default defineNuxtModule({ rpc?.broadcast.documentsUpdated(documents) // Output the generated documents if desired. - if (options.outputDocuments) { - const destFolder = resolve( - nuxt.options.buildDir, - 'nuxt-graphql-middleware/documents', - ) + const outputDocumentsPath = await getOutputDocumentsPath( + options.outputDocuments, + nuxt.options.buildDir, + rootResolver.resolvePath, + ) + if (outputDocumentsPath) { + outputDocuments(outputDocumentsPath, documents) - outputDocuments(destFolder, documents) if (isFirst) { - logger.info('Documents generated at ' + destFolder) + logger.info('Documents generated at ' + outputDocumentsPath) } } + if (hasErrors) { throw new Error('Documents has errors.') }