Skip to content

Commit

Permalink
feat: add option to provide a custom output path (#47)
Browse files Browse the repository at this point in the history
* add option to provide a custom output path

* add some help with npm scripts.

* put getOutputDocumentsPath into a function and use rootResolver
  • Loading branch information
milanbombschliip authored Nov 20, 2024
1 parent 5028dba commit d3bc2dc
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 13 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22
7 changes: 4 additions & 3 deletions docs/configuration/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.

13 changes: 13 additions & 0 deletions package.json.md
Original file line number Diff line number Diff line change
@@ -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`
19 changes: 18 additions & 1 deletion src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -494,3 +495,19 @@ export async function outputDocuments(
}
})
}

export async function getOutputDocumentsPath(
optionsOutputDocuments: ModuleOptions['outputDocuments'],
nuxtBuildDir: string,
resolvePath: Resolver['resolvePath'],
): Promise<string | null> {
if (!optionsOutputDocuments) {
return null
}

if (typeof optionsOutputDocuments === 'boolean') {
return resolve(nuxtBuildDir, `${name}/documents`)
} else {
return await resolvePath(optionsOutputDocuments)
}
}
20 changes: 12 additions & 8 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
logger,
fileExists,
outputDocuments,
getOutputDocumentsPath,
} from './helpers'
import { type CodegenResult } from './codegen'
import { type ClientFunctions, type ServerFunctions } from './rpc-types'
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -354,17 +356,19 @@ export default defineNuxtModule<ModuleOptions>({
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.')
}
Expand Down

0 comments on commit d3bc2dc

Please sign in to comment.