Skip to content

Commit 4207aee

Browse files
fix: implement custom GraphQL Yoga configuration and integrate it into the rollup process
1 parent 0f6584e commit 4207aee

File tree

6 files changed

+23
-4
lines changed

6 files changed

+23
-4
lines changed

playground/server/graphql/yoga.config.ts renamed to playground/server/graphql/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
export default defineGraphQLConfig({
33
// Custom context enhancer - adds additional properties to GraphQL context
44

5+
graphiql: true,
56
// Additional yoga options can be added here
67
// See: https://the-guild.dev/graphql/yoga-server/docs
78
})

src/rollup.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import type { Nitro } from 'nitropack'
33
import { readFile } from 'node:fs/promises'
44
import { parse } from 'graphql'
55
import { genImport } from 'knitwork'
6+
import { resolve } from 'pathe'
67
import { getImportId, scanGraphql } from './utils'
78
import { serverTypeGeneration } from './utils/server-type-generation'
89

910
export async function rollupConfig(app: Nitro) {
1011
virtualDefs(app)
1112
virtualResolvers(app)
12-
13+
getGraphQLConfig(app)
1314
app.hooks.hook('rollup:before', (nitro, rollupConfig) => {
1415
rollupConfig.plugins = rollupConfig.plugins || []
1516
const {
@@ -154,3 +155,15 @@ export function virtualResolvers(app: Nitro) {
154155
return code
155156
}
156157
}
158+
159+
export function getGraphQLConfig(app: Nitro) {
160+
const configPath = resolve(app.graphql.serverDir, 'config.ts')
161+
162+
app.options.virtual ??= {}
163+
app.options.virtual['#nitro-internal-virtual/graphql-config'] = () => {
164+
return `import config from '${configPath}'
165+
const importedConfig = config
166+
export { importedConfig }
167+
`
168+
}
169+
}

src/routes/graphql-yoga.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import type { YogaServerInstance } from 'graphql-yoga'
2-
import { defs } from '#nitro-internal-virtual/server-defs'
2+
import { importedConfig } from '#nitro-internal-virtual/graphql-config'
33

4+
import { defs } from '#nitro-internal-virtual/server-defs'
45
import { resolvers } from '#nitro-internal-virtual/server-resolvers'
6+
57
import { mergeResolvers, mergeTypeDefs } from '@graphql-tools/merge'
8+
import defu from 'defu'
69
import { createSchema, createYoga } from 'graphql-yoga'
710
import { defineEventHandler, toWebRequest } from 'h3'
811
// TODO: https://github.com/nitrojs/nitro/issues/3403 if used import this error.
@@ -57,12 +60,12 @@ export default defineEventHandler(async (event) => {
5760
if (!yoga) {
5861
const schema = createMergedSchema()
5962
// Yoga instance'ı henüz oluşturulmadıysa, oluştur
60-
yoga = createYoga({
63+
yoga = createYoga(defu({
6164
schema,
6265
graphqlEndpoint: '/api/graphql',
6366
landingPage: false,
6467
renderGraphiQL: () => apolloSandboxHtml,
65-
})
68+
}, importedConfig))
6669
}
6770
const request = toWebRequest(event)
6871
const response = await yoga.handleRequest(request, event as any)

src/virtual/graphql-config.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const importedConfig: any = {}

tsdown.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default defineConfig({
3535
'h3',
3636
'#nitro-internal-virtual/server-defs',
3737
'#nitro-internal-virtual/server-resolvers',
38+
'#nitro-internal-virtual/graphql-config',
3839
'@nuxt/kit',
3940
'@nuxt/schema',
4041
'@apollo/server',

0 commit comments

Comments
 (0)