Skip to content

Commit 73c5227

Browse files
fix: hot reload client types (#10)
* fix: update version to 0.3.2, remove unused GraphQL queries and mutations, and refactor type generation functions for improved clarity and functionality * refactor: rename 'defs' to 'schemas' for consistency and clarity, update related imports and type generation logic
1 parent d0c4d76 commit 73c5227

File tree

16 files changed

+113
-158
lines changed

16 files changed

+113
-158
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nitro-graphql",
33
"type": "module",
4-
"version": "0.3.0",
4+
"version": "0.3.2",
55
"packageManager": "pnpm@10.13.1",
66
"description": "GraphQL integration for Nitro",
77
"license": "MIT",

playground/client/mutations.graphql

Lines changed: 0 additions & 48 deletions
This file was deleted.

playground/client/queries.graphql

Lines changed: 0 additions & 49 deletions
This file was deleted.

playground/graphql/queries.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query GetUsers {
2+
hello
3+
}

playground/graphql/sdk.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// THIS FILE IS GENERATED, DO NOT EDIT!
2+
/* eslint-disable eslint-comments/no-unlimited-disable */
3+
/* tslint:disable */
4+
/* eslint-disable */
5+
/* prettier-ignore */
6+
import type * as Types from '#graphql/client';
7+
8+
export type GetUsersQueryVariables = Types.Exact<{ [key: string]: never; }>;
9+
10+
11+
export type GetUsersQuery = { __typename?: 'Query', hello: string };
12+
13+
14+
export const GetUsersDocument = /*#__PURE__*/ `
15+
query GetUsers {
16+
hello
17+
}
18+
`;
19+
export type Requester<C = {}> = <R, V>(doc: string, vars?: V, options?: C) => Promise<R> | AsyncIterable<R>
20+
export function getSdk<C>(requester: Requester<C>) {
21+
return {
22+
GetUsers(variables?: Types.GetUsersQueryVariables, options?: C): Promise<Types.GetUsersQuery> {
23+
return requester<Types.GetUsersQuery, Types.GetUsersQueryVariables>(GetUsersDocument, variables, options) as Promise<Types.GetUsersQuery>;
24+
}
25+
};
26+
}
27+
export type Sdk = ReturnType<typeof getSdk>;

src/index.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import type { NitroGraphQLOptions } from './types'
33
import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
44
import { fileURLToPath } from 'node:url'
55
import { watch } from 'chokidar'
6-
import consola from 'consola'
76

7+
import consola from 'consola'
88
import defu from 'defu'
99
import { defineNitroModule } from 'nitropack/kit'
10-
import { dirname, join, resolve } from 'pathe'
10+
import { dirname, join, relative, resolve } from 'pathe'
1111
import { rollupConfig } from './rollup'
12-
import { relativeWithDot, scanDefs, scanResolvers } from './utils'
12+
import { relativeWithDot, scanDocs, scanResolvers, scanSchemas } from './utils'
1313
import { clientTypeGeneration, serverTypeGeneration } from './utils/server-type-generation'
1414

1515
export type * from './types'
@@ -28,6 +28,11 @@ export default defineNitroModule({
2828
watchDirs: [],
2929
clientDir: '',
3030
serverDir: resolve(nitro.options.srcDir, 'graphql'),
31+
dir: {
32+
build: relative(nitro.options.rootDir, nitro.options.buildDir),
33+
client: 'graphql',
34+
server: 'server',
35+
},
3136
}
3237

3338
nitro.hooks.hook('rollup:before', (nitro, rollupConfig) => {
@@ -68,6 +73,11 @@ export default defineNitroModule({
6873
case 'nuxt':
6974
watchDirs.push(join(nitro.options.rootDir, 'app', 'graphql'))
7075
nitro.graphql.clientDir = resolve(nitro.options.rootDir, 'app', 'graphql')
76+
nitro.graphql.dir.client = 'app/graphql'
77+
break
78+
case 'nitro':
79+
nitro.graphql.clientDir = resolve(nitro.options.rootDir, 'graphql')
80+
nitro.graphql.dir.client = 'graphql'
7181
break
7282
default:
7383
}
@@ -78,7 +88,7 @@ export default defineNitroModule({
7888
ignored: nitro.options.ignore,
7989
}).on('all', async (event, path) => {
8090
if (path.endsWith('.graphql') || path.endsWith('.gql')) {
81-
await clientTypeGeneration(nitro, path)
91+
await clientTypeGeneration(nitro)
8292
}
8393
})
8494

@@ -93,29 +103,35 @@ export default defineNitroModule({
93103
const tsconfigDir = dirname(tsConfigPath)
94104
const typesDir = resolve(nitro.options.buildDir, 'types')
95105

96-
const defs = await scanDefs(nitro)
97-
nitro.scanDefs = defs
106+
const schemas = await scanSchemas(nitro)
107+
nitro.scanSchemas = schemas
108+
109+
const docs = await scanDocs(nitro)
110+
nitro.scanDocuments = docs
98111

99112
const resolvers = await scanResolvers(nitro)
100113
nitro.scanResolvers = resolvers
101114

102115
nitro.hooks.hook('dev:start', async () => {
103-
const defs = await scanDefs(nitro)
104-
nitro.scanDefs = defs
116+
const schemas = await scanSchemas(nitro)
117+
nitro.scanSchemas = schemas
105118

106119
const resolvers = await scanResolvers(nitro)
107120
nitro.scanResolvers = resolvers
121+
122+
const docs = await scanDocs(nitro)
123+
nitro.scanDocuments = docs
108124
})
109125

110126
await rollupConfig(nitro)
111127

112128
// Generate server and client types
113129
await serverTypeGeneration(nitro)
114-
await clientTypeGeneration(nitro, nitro.graphql.clientDir)
130+
await clientTypeGeneration(nitro)
115131

116132
nitro.hooks.hook('close', async () => {
117133
await serverTypeGeneration(nitro)
118-
await clientTypeGeneration(nitro, nitro.graphql.clientDir)
134+
await clientTypeGeneration(nitro)
119135
})
120136

121137
const runtime = fileURLToPath(
@@ -182,7 +198,7 @@ export default defineNitroModule({
182198
return 'schemas'
183199
}
184200

185-
// resolsvers and defs are not in the same directory, so we need to check both
201+
// resolsvers and schemas are not in the same directory, so we need to check both
186202
if (id.endsWith('.resolver.ts')) {
187203
return 'resolvers'
188204
}

src/rollup.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { parse } from 'graphql'
55
import { genImport } from 'knitwork'
66
import { resolve } from 'pathe'
77
import { getImportId, scanGraphql } from './utils'
8-
import { serverTypeGeneration } from './utils/server-type-generation'
8+
import { clientTypeGeneration, serverTypeGeneration } from './utils/server-type-generation'
99

1010
export async function rollupConfig(app: Nitro) {
11-
virtualDefs(app)
11+
virtualSchemas(app)
1212
virtualResolvers(app)
1313
getGraphQLConfig(app)
1414
app.hooks.hook('rollup:before', (nitro, rollupConfig) => {
@@ -40,9 +40,8 @@ export async function rollupConfig(app: Nitro) {
4040
return `export default ${JSON.stringify(content)}`
4141
}
4242
catch (error) {
43-
// Dosya okuma hatası vs
4443
if ((error as any).code === 'ENOENT') {
45-
return null // Dosya bulunamazsa sessizce geç
44+
return null
4645
}
4746
this.error(`Failed to read GraphQL file ${id}: ${(error as any).message}`)
4847
}
@@ -77,29 +76,30 @@ export async function rollupConfig(app: Nitro) {
7776

7877
app.hooks.hook('dev:reload', async () => {
7978
await serverTypeGeneration(app)
79+
await clientTypeGeneration(app)
8080
})
8181
}
8282

83-
export function virtualDefs(app: Nitro) {
84-
const getDefs = () => {
85-
const defs: string[] = [
86-
...app.scanDefs,
83+
export function virtualSchemas(app: Nitro) {
84+
const getSchemas = () => {
85+
const schemas: string[] = [
86+
...app.scanSchemas,
8787
...(app.options.graphql?.typedefs ?? []),
8888
]
8989

90-
return defs
90+
return schemas
9191
}
9292

9393
app.options.virtual ??= {}
94-
app.options.virtual['#nitro-internal-virtual/server-defs'] = () => {
95-
const imports = getDefs()
94+
app.options.virtual['#nitro-internal-virtual/server-schemas'] = () => {
95+
const imports = getSchemas()
9696

9797
const code = /* js */`
9898
${imports
9999
.map(handler => `import ${getImportId(handler)} from '${handler}';`)
100100
.join('\n')}
101101
102-
export const defs = [
102+
export const schemas = [
103103
${imports
104104
.map(
105105
h =>

src/routes/apollo-server.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { BaseContext } from '@apollo/server'
22
import { importedConfig } from '#nitro-internal-virtual/graphql-config'
3-
import { defs } from '#nitro-internal-virtual/server-defs'
43
import { resolvers } from '#nitro-internal-virtual/server-resolvers'
4+
import { schemas } from '#nitro-internal-virtual/server-schemas'
55
import { ApolloServer } from '@apollo/server'
66
import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default'
77
// TODO: fix bug
@@ -12,8 +12,8 @@ import { startServerAndCreateH3Handler } from '../utils/apollo'
1212

1313
function createMergedSchema() {
1414
try {
15-
const mergedDefs = defs.map(schema => schema.def).join('\n\n')
16-
const typeDefs = mergeTypeDefs([mergedDefs])
15+
const mergedSchemas = schemas.map(schema => schema.def).join('\n\n')
16+
const typeDefs = mergeTypeDefs([mergedSchemas])
1717
const mergedResolvers = mergeResolvers(resolvers.map(r => r.resolver))
1818

1919
return {
@@ -27,7 +27,7 @@ function createMergedSchema() {
2727
}
2828
}
2929

30-
let apolloServer: ApolloServer<BaseContext>
30+
let apolloServer: ApolloServer<BaseContext> | null = null
3131

3232
function createApolloServer() {
3333
if (!apolloServer) {
@@ -45,6 +45,9 @@ function createApolloServer() {
4545
return apolloServer
4646
}
4747

48-
export default startServerAndCreateH3Handler(createApolloServer, {
49-
context: async event => ({ event }),
50-
})
48+
export default startServerAndCreateH3Handler(
49+
apolloServer || createApolloServer,
50+
{
51+
context: async event => ({ event }),
52+
},
53+
)

src/routes/graphql-yoga.ts

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

4-
import { defs } from '#nitro-internal-virtual/server-defs'
54
import { resolvers } from '#nitro-internal-virtual/server-resolvers'
5+
import { schemas } from '#nitro-internal-virtual/server-schemas'
66

77
import { mergeResolvers, mergeTypeDefs } from '@graphql-tools/merge'
88
import defu from 'defu'
@@ -39,8 +39,8 @@ new window.EmbeddedSandbox({
3939
// Schema ve yoga instance'ını build time'da oluştur
4040
function createMergedSchema() {
4141
try {
42-
const mergedDefs = defs.map(schema => schema.def).join('\n\n')
43-
const typeDefs = mergeTypeDefs([mergedDefs])
42+
const mergedSchemas = schemas.map(schema => schema.def).join('\n\n')
43+
const typeDefs = mergeTypeDefs([mergedSchemas])
4444
const mergedResolvers = mergeResolvers(resolvers.map(r => r.resolver))
4545

4646
return createSchema({

src/types/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@ export interface GenImport {
2222

2323
declare module 'nitropack/types' {
2424
interface Nitro {
25-
scanDefs: string[]
25+
scanSchemas: string[]
26+
scanDocuments: string[]
2627
scanResolvers: GenImport[]
2728
graphql: {
2829
buildDir: string
2930
watchDirs: string[]
3031
clientDir: string
3132
serverDir: string
33+
dir: {
34+
build: string
35+
client: string
36+
server: string
37+
}
3238
}
3339
}
3440
}

0 commit comments

Comments
 (0)