Skip to content

Commit df4d0a2

Browse files
fix!: server context type (#16)
* fix: server context type * fix: remove empty context function from GraphQL configuration
1 parent d925c34 commit df4d0a2

File tree

8 files changed

+28
-28
lines changed

8 files changed

+28
-28
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
},
6868
"peerDependencies": {
6969
"graphql": "^16.11.0",
70+
"h3": "^1.15.3",
7071
"nitropack": "^2.11.13"
7172
},
7273
"dependencies": {

pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
packages:
22
- playground
33
- playground-nuxt
4+
45
catalog:
56
'@antfu/eslint-config': ^4.17.0
67
'@apollo/server': ^5.0.0
@@ -33,7 +34,7 @@ catalog:
3334
graphql-config: ^5.1.5
3435
graphql-scalars: ^1.24.2
3536
graphql-yoga: ^5.15.1
36-
h3: ^1.15.3
37+
h3: 1.15.3
3738
knitwork: ^1.2.0
3839
nitropack: ^2.12.3
3940
nuxt: 4.0.0-rc.0

src/graphql/server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ export interface StandardSchemaV1 extends Record<string, any> {}
22

33
export interface ResolversTypes extends Record<string, any> {}
44
export interface Resolvers extends Record<string, any> {}
5+
export interface NPMConfig {
6+
framework: 'any'
7+
}

src/index.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import { clientTypeGeneration, serverTypeGeneration } from './utils/type-generat
1414

1515
export type * from './types'
1616

17-
export type GraphQLFramework = 'graphql-yoga'
18-
1917
export default defineNitroModule({
2018
name: 'nitro-graphql',
2119
async setup(nitro: Nitro) {
@@ -229,16 +227,6 @@ export default defineNitroModule({
229227
}
230228
})
231229

232-
const graphqlDtsContent = `// Auto-generated by nitro-graphql
233-
234-
import type { SchemaType } from './nitro-graphql-server.d.ts'
235-
236-
declare module 'nitro-graphql' {
237-
type GraphQLFramework = '${nitro.options.graphql?.framework || 'graphql-yoga'}'
238-
}
239-
`
240-
241-
writeFileSync(join(typesDir, 'graphql.d.ts'), graphqlDtsContent)
242230
nitro.options.typescript.strict = true
243231

244232
nitro.hooks.hook('types:extend', (types) => {
@@ -316,13 +304,15 @@ export default defineGraphQLConfig({
316304

317305
if (!existsSync(join(nitro.graphql.serverDir, 'context.d.ts'))) {
318306
writeFileSync(join(nitro.graphql.serverDir, 'context.d.ts'), `// Example context definition please change it to your needs
319-
// import type { Database } from '../utils/useDb'
307+
import type { H3EventContext as OriginalH3EventContext } from 'h3'
308+
309+
export interface ExtendedH3EventContext extends OriginalH3EventContext {
310+
// useDatabase: () => Database
311+
// tables: typeof import('~~/server/drizzle/schema/index')
312+
}
320313
321314
declare module 'h3' {
322-
interface H3EventContext {
323-
// useDatabase: () => Database
324-
// tables: typeof import('~~/server/drizzle/schema/index')
325-
}
315+
interface H3EventContext extends ExtendedH3EventContext {}
326316
}`, 'utf-8')
327317
}
328318
},

src/utils/define.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { Resolvers, ResolversTypes } from '#graphql/server'
1+
import type { NPMConfig, Resolvers, ResolversTypes } from '#graphql/server'
22
import type { ApolloServerOptions } from '@apollo/server'
33
import type { YogaServerOptions } from 'graphql-yoga'
44
import type { H3Event } from 'h3'
55

6-
import type { GraphQLFramework, StandardSchemaV1 } from 'nitro-graphql'
6+
import type { StandardSchemaV1 } from 'nitro-graphql'
77

88
type Flatten<T> = T extends infer U ? { [K in keyof U]: U[K] } : never
99

@@ -60,14 +60,14 @@ export function defineType(
6060
return resolvers
6161
}
6262

63-
export type DefineServerConfig = GraphQLFramework extends 'graphql-yoga'
63+
export type DefineServerConfig<T extends NPMConfig = NPMConfig> = T['framework'] extends 'graphql-yoga'
6464
? Partial<YogaServerOptions<H3Event, Partial<H3Event>>>
65-
: GraphQLFramework extends 'apollo-server'
65+
: T['framework'] extends 'apollo-server'
6666
? Partial<ApolloServerOptions<H3Event>>
67-
: Record<string, any>
67+
: Partial<YogaServerOptions<H3Event, Partial<H3Event>>> | Partial<ApolloServerOptions<H3Event>>
6868

69-
export function defineGraphQLConfig(
70-
config: Partial<DefineServerConfig>,
71-
): Partial<DefineServerConfig> {
69+
export function defineGraphQLConfig<T extends NPMConfig = NPMConfig>(
70+
config: Partial<DefineServerConfig<T>>,
71+
): Partial<DefineServerConfig<T>> {
7272
return config
7373
}

src/utils/server-codegen.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function pluginContent(_schema: any, _documents: any, _config: any, _info: any)
3030
}
3131

3232
export async function generateTypes(
33+
selectFremework: string,
3334
schema: GraphQLSchema,
3435
config: CodegenServerConfig = {},
3536
outputPath?: string,
@@ -82,6 +83,10 @@ export async function generateTypes(
8283
`import type { StandardSchemaV1 } from 'nitro-graphql'`,
8384

8485
`
86+
export interface NPMConfig {
87+
framework: '${selectFremework || 'graphql-yoga'}';
88+
}
89+
8590
export type SchemaType = Partial<Record<Partial<keyof ResolversTypes>, StandardSchemaV1>>
8691
8792
// Check if schemas is empty object, return never if so

src/utils/type-generation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export async function serverTypeGeneration(app: Nitro) {
5959
assumeValid: true,
6060
})
6161

62-
const data = await generateTypes(schema, app.options.graphql?.codegen?.server ?? {})
62+
const data = await generateTypes(app.options.graphql?.framework || 'graphql-yoga', schema, app.options.graphql?.codegen?.server ?? {})
6363

6464
const printSchema = printSchemaWithDirectives(schema)
6565

0 commit comments

Comments
 (0)