Skip to content

Commit

Permalink
put getOutputDocumentsPath into a function and use rootResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
milanbombschliip committed Nov 20, 2024
1 parent 3ebcd5c commit ef92e6b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
6 changes: 4 additions & 2 deletions package.json.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ 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
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`
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)
}
}
22 changes: 10 additions & 12 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 @@ -350,22 +351,19 @@ export default defineNuxtModule<ModuleOptions>({
rpc?.broadcast.documentsUpdated(documents)

// Output the generated documents if desired.
if (options.outputDocuments) {
let destFolder
if (typeof options.outputDocuments === 'boolean') {
destFolder = resolve(
nuxt.options.buildDir,
'nuxt-graphql-middleware/documents',
)
} else {
destFolder = options.outputDocuments
}
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 ef92e6b

Please sign in to comment.