Skip to content

Commit

Permalink
feat(nuxt): add support for dynamic plugin generation
Browse files Browse the repository at this point in the history
  • Loading branch information
productdevbook committed Feb 19, 2024
1 parent e55c093 commit 8e79455
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 5 deletions.
38 changes: 38 additions & 0 deletions packages/nuxt/src/runtime/modules/graphqlYoga/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,25 @@ export default definePergelModule<GraphQLYogaConfig, ResolvedGraphQLYogaConfig>(
}
}
}

if (!existsSync(resolve(options.serverDir, 'plugins'))) {
const files = globbySync((join(nuxt._pergel.pergelModuleRoot, 'templates', options.moduleName, 'drizzle-lucia', 'plugins', '**/*')), {
onlyFiles: true,
})

for (const file of files) {
const readFile = await nuxt._pergel.jitiDyanmicImport(file)
if (readFile) {
const fileData = readFile({
projectName: options.projectName,
nuxt,
})
const fileName = basename(file)

writeFilePergel(resolve(options.serverDir, 'plugins', fileName), fileData)
}
}
}
}
else {
if (!existsSync(resolve(options.serverDir, 'index.ts'))) {
Expand Down Expand Up @@ -270,6 +289,25 @@ export default definePergelModule<GraphQLYogaConfig, ResolvedGraphQLYogaConfig>(
}
}
}

if (!existsSync(resolve(options.serverDir, 'plugins'))) {
const files = globbySync((join(nuxt._pergel.pergelModuleRoot, 'templates', options.moduleName, 'empty', 'plugins', '**/*')), {
onlyFiles: true,
})

for (const file of files) {
const readFile = await nuxt._pergel.jitiDyanmicImport(file)
if (readFile) {
const fileData = readFile({
projectName: options.projectName,
nuxt,
})
const fileName = basename(file)

writeFilePergel(resolve(options.serverDir, 'plugins', fileName), fileData)
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@ import { camelCase } from 'scule'
export default function (data: {
projectName: string
}) {
const pergelFunction = camelCase(`pergel_${data.projectName}`)
const pergelFunction = camelCase(`pergel-${data.projectName}`)
const graphQLCreateSchema = camelCase(`${data.projectName}-GraphQLCreateSchema`)
const drizzleStorage = camelCase(`${data.projectName}-Drizzle-Storage`)
return /* TS */ `export default ${pergelFunction}().graphqlYoga().nitro().use({
onBeforeOptions: async ({ options }) => {
const luciaRequest = camelCase(`${data.projectName}-Lucia-Request`)
return /* TS */ `// Please move server/plugins/graphqlv1.ts. If you want to change graphqlv1.ts you can change it.
export default ${pergelFunction}().graphqlYoga().nitro().use({
onRequest: [
// If lucia true, remove comment
// ${luciaRequest},
],
onBeforeOptions: async ({ options }, event) => {
// const allowedOrigins = (propscess.env.ORIGIN as string).split(',')
const origin = event.node.req.headers.origin as string
options.add({
schema: ${graphQLCreateSchema},
cors: {
origin,
credentials: true,
},
})
},
async onBeforeContext({ options }, event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { camelCase } from 'scule'
import { camelCase, pascalCase } from 'scule'

export default function (data: {
projectName: string
}) {
const functionName = camelCase(`${data.projectName}-GraphQLCreateSchema`)
const resolverFunctionName = camelCase(`${data.projectName}-GraphQLResolvers`)
const authFunctionName = camelCase(`${data.projectName}-Auth`)
const serviceFunctionName = camelCase(`${data.projectName}-GraphQLService`)
const serviceInterfaceName = pascalCase(`${data.projectName}-GraphQLService`)
return /* TS */ `import { DateTimeResolver, DateTimeTypeDefinition } from 'graphql-scalars'
import type { ${serviceInterfaceName} } from './services'
import { ${serviceFunctionName} } from './services'
import { createSchema } from 'graphql-yoga'
import type { Book, Resolvers, User } from '#${data.projectName}/graphqlYoga/generated/server'
import type { Resolvers } from '#${data.projectName}/graphqlYoga/generated/server'
import { schema } from '#${data.projectName}/graphqlYoga/generated/schema'
export type { ${serviceInterfaceName} }
export { ${serviceFunctionName} }
export const ${resolverFunctionName}: Resolvers = {
DateTime: DateTimeResolver,
SearchResult: {
Expand Down
43 changes: 43 additions & 0 deletions packages/nuxt/src/templates/graphqlYoga/empty/plugins/graphqlv1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { camelCase } from 'scule'

export default function (data: {
projectName: string
}) {
const pergelFunction = camelCase(`pergel-${data.projectName}`)
const graphQLCreateSchema = camelCase(`${data.projectName}-GraphQLCreateSchema`)
const drizzleStorage = camelCase(`${data.projectName}-Drizzle-Storage`)
const luciaRequest = camelCase(`${data.projectName}-Lucia-Request`)
return /* TS */ `// Please move server/plugins/graphqlv1.ts. If you want to change graphqlv1.ts you can change it.
export default ${pergelFunction}().graphqlYoga().nitro().use({
onRequest: [
// If lucia true, remove comment
// ${luciaRequest},
],
onBeforeOptions: async ({ options }, event) => {
// const allowedOrigins = (propscess.env.ORIGIN as string).split(',')
const origin = event.node.req.headers.origin as string
options.add({
schema: ${graphQLCreateSchema},
cors: {
origin,
credentials: true,
},
})
},
async onBeforeContext({ options }, event) {
const db = await ${pergelFunction}().drizzle()
.postgresjs()
.connect({
event,
})
const storage = ${drizzleStorage}({ db })
await options.add({
db,
storage,
})
},
})
`
}

0 comments on commit 8e79455

Please sign in to comment.