Skip to content

Commit 6e8e87c

Browse files
feat: enhance todos resolver with createdAt field and improve error handling in client code generation
1 parent f0ed482 commit 6e8e87c

File tree

6 files changed

+29
-22
lines changed

6 files changed

+29
-22
lines changed

playground/client/example.ts

Whitespace-only changes.

playground/server/graphql/resolvers/queries/todos.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export default createResolver({
66
id: '1',
77
title: 'Sample Todo',
88
completed: false,
9+
createdAt: new Date().toISOString(),
910
}]
1011
},
1112

src/client-codegen.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { GraphQLSchema } from 'graphql'
22
import type { CodegenClientConfig } from './types'
33
import { codegen } from '@graphql-codegen/core'
44
import { plugin as typescriptPlugin } from '@graphql-codegen/typescript'
5-
import { plugin as typescriptOperations } from '@graphql-codegen/typescript-operations'
65
import { plugin as typescriptGenericSdk } from '@graphql-codegen/typescript-generic-sdk'
6+
import { plugin as typescriptOperations } from '@graphql-codegen/typescript-operations'
77
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'
88
import { loadDocuments } from '@graphql-tools/load'
99
import { printSchemaWithDirectives } from '@graphql-tools/utils'
@@ -30,15 +30,17 @@ async function loadGraphQLDocuments(patterns: string | string[]) {
3030
loaders: [new GraphQLFileLoader()],
3131
})
3232
return result
33-
} catch (e: any) {
33+
}
34+
catch (e: any) {
3435
if (
3536
(e.message || '').includes(
3637
'Unable to find any GraphQL type definitions for the following pointers:',
3738
)
3839
) {
3940
// No GraphQL files found - this is normal
4041
return []
41-
} else {
42+
}
43+
else {
4244
// Re-throw other errors
4345
throw e
4446
}
@@ -100,8 +102,9 @@ export async function generateClientTypes(
100102
})
101103

102104
return output
103-
} catch (error) {
105+
}
106+
catch (error) {
104107
consola.warn('[graphql] Client type generation failed:', error)
105108
return ''
106109
}
107-
}
110+
}

src/client-watcher.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import type { Nitro } from 'nitropack/types'
22
import type { NitroGraphQLYogaOptions } from './types'
3+
import { mkdir, writeFile } from 'node:fs/promises'
4+
import { mergeTypeDefs } from '@graphql-tools/merge'
5+
import { makeExecutableSchema } from '@graphql-tools/schema'
36
import { consola } from 'consola'
47
import { join } from 'pathe'
5-
import { mkdir, writeFile } from 'node:fs/promises'
68
import { generateClientTypes } from './client-codegen'
79
import { scanGraphQLFiles } from './scanner'
8-
import { mergeTypeDefs } from '@graphql-tools/merge'
9-
import { makeExecutableSchema } from '@graphql-tools/schema'
1010
import { debounce } from './utils'
1111

1212
async function regenerateClientTypes(nitro: Nitro, options: NitroGraphQLYogaOptions) {
1313
try {
14-
if (!options.client?.enabled) return
14+
if (!options.client?.enabled)
15+
return
1516

1617
consola.start('[graphql] 🔄 Regenerating client types...')
1718

@@ -45,16 +46,17 @@ async function regenerateClientTypes(nitro: Nitro, options: NitroGraphQLYogaOpti
4546
)
4647

4748
if (generatedTypes) {
48-
const outputPath = options.client.outputPath ||
49-
join(nitro.options.buildDir, 'types', 'graphql-client.generated.ts')
50-
49+
const outputPath = options.client.outputPath
50+
|| join(nitro.options.buildDir, 'types', 'graphql-client.generated.ts')
51+
5152
const typesDir = join(nitro.options.buildDir, 'types')
5253
await mkdir(typesDir, { recursive: true })
5354
await writeFile(outputPath, generatedTypes)
5455

5556
consola.success('[graphql] ✨ Client types updated at:', outputPath)
5657
}
57-
} catch (error) {
58+
}
59+
catch (error) {
5860
const errorMessage = error instanceof Error ? error.message : String(error)
5961
consola.error('[graphql] ❌ Client type generation failed:', errorMessage)
6062
}
@@ -82,14 +84,15 @@ export async function setupClientWatcher(nitro: Nitro, options: NitroGraphQLYoga
8284
const { globby } = await import('globby')
8385

8486
// Find existing client GraphQL files
85-
const existingClientFiles = await globby(clientPatterns, {
87+
const existingClientFiles = await globby(clientPatterns, {
8688
absolute: true,
87-
ignore: [join(nitro.options.srcDir, 'graphql/**/*')] // Exclude server files
89+
ignore: [join(nitro.options.srcDir, 'graphql/**/*')], // Exclude server files
8890
})
8991

9092
if (existingClientFiles.length > 0) {
9193
consola.info(`[graphql] 📁 Watching ${existingClientFiles.length} client GraphQL files`)
92-
} else {
94+
}
95+
else {
9396
consola.info('[graphql] 📁 No client GraphQL files found to watch')
9497
}
9598

@@ -142,4 +145,4 @@ export async function setupClientWatcher(nitro: Nitro, options: NitroGraphQLYoga
142145
await generateClientTypesDebounced()
143146

144147
consola.success('[graphql] ✅ Client watcher ready')
145-
}
148+
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import { makeExecutableSchema } from '@graphql-tools/schema'
66
import { consola } from 'consola'
77
import { defineNitroModule } from 'nitropack/kit'
88
import { join } from 'pathe'
9+
import { setupClientWatcher } from './client-watcher'
910
import { generateTypes } from './codegen'
1011
import { scanGraphQLFiles } from './scanner'
1112
import { setupGraphQLWatcher } from './watcher'
12-
import { setupClientWatcher } from './client-watcher'
1313

1414
export default defineNitroModule({
1515
name: 'nitro:graphql-yoga',
@@ -32,7 +32,7 @@ export default defineNitroModule({
3232
emitLegacyCommonJSImports: false,
3333
useTypeImports: true,
3434
enumsAsTypes: true,
35-
}
35+
},
3636
},
3737
// Merge with user config from nitro.options
3838
...(nitro.options as any).graphqlYoga,

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { YogaServerOptions } from 'graphql-yoga'
2-
import type { IncomingMessage, ServerResponse } from 'node:http'
31
import type { TypeScriptPluginConfig } from '@graphql-codegen/typescript'
42
import type { TypeScriptDocumentsPluginConfig } from '@graphql-codegen/typescript-operations'
3+
import type { YogaServerOptions } from 'graphql-yoga'
4+
import type { IncomingMessage, ServerResponse } from 'node:http'
55

66
export interface GraphQLSchemaConfig {
77
typeDefs: string | string[]
@@ -34,7 +34,7 @@ declare module 'nitropack' {
3434
interface NitroOptions {
3535
graphqlYoga?: NitroGraphQLYogaOptions
3636
}
37-
37+
3838
interface NitroRuntimeConfig {
3939
graphqlYoga?: NitroGraphQLYogaOptions
4040
}

0 commit comments

Comments
 (0)