Skip to content

Commit 2594a1a

Browse files
committed
feat: add support for server types
1 parent f8a8b9a commit 2594a1a

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default defineEventHandler(async (event) => {
2+
const ships = await GqlShips({ limit: 3 })
3+
4+
return ships.ships
5+
})

src/module.ts

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,12 @@ export default defineNuxtModule<GqlConfig>({
205205
...(typeof config.codegen !== 'boolean' && config.codegen)
206206
}).then(output => output.reduce<Record<string, string>>((acc, c) => ({ ...acc, [c.filename.split('.ts')[0]]: c.content }), {}))
207207
: ctx.clients!.reduce<Record<string, string>>((acc, k) => {
208-
if (!clientDocs?.[k]?.length) { return acc }
208+
if (!clientDocs?.[k]?.length) { return acc }
209209

210-
const entries = extractGqlOperations(ctx?.clientDocs?.[k] || [])
210+
const entries = extractGqlOperations(ctx?.clientDocs?.[k] || [])
211211

212-
return { ...acc, [k]: mockTemplate(entries) }
213-
}, {})
212+
return { ...acc, [k]: mockTemplate(entries) }
213+
}, {})
214214

215215
ctx.template = defu(codegenResult, ctx.template)
216216
}
@@ -276,6 +276,49 @@ export default defineNuxtModule<GqlConfig>({
276276
'export default { clients, config }'
277277
].join('\n')
278278

279+
// Add type declarations for the #gql-nitro virtual module
280+
addTemplate({
281+
filename: 'types/gql-nitro.d.ts',
282+
getContents: () => {
283+
if (!ctx.codegen || !ctx.fns || !ctx.clients) {
284+
// Fallback for non-codegen mode
285+
const fnTypes = ctx.fns?.map(fn => ` export const ${config.functionPrefix + upperFirst(fn)}: (...params: any[]) => Promise<any>`).join('\n') || ''
286+
return [
287+
'declare module \'#gql-nitro\' {',
288+
fnTypes,
289+
' const _default: { clients: Record<string, any>, config: any }',
290+
' export default _default',
291+
'}'
292+
].join('\n')
293+
}
294+
295+
// Use import type syntax that works in .d.ts files
296+
const typeImports = ctx.clients.map(client => `import type * as ${client}Types from '../gql/${client}'`).join('\n')
297+
298+
const fnTypes = ctx.fns.map((fn) => {
299+
const fnName = config.functionPrefix + upperFirst(fn)
300+
const sdkUnion = ctx.clients!.map(c => `ReturnType<typeof ${c}Types.getSdk>`).join(' & ')
301+
return ` export const ${fnName}: (...params: Parameters<(${sdkUnion})['${fn}']>) => ReturnType<(${sdkUnion})['${fn}']>`
302+
}).join('\n')
303+
304+
return [
305+
typeImports,
306+
'declare module \'#gql-nitro\' {',
307+
fnTypes,
308+
' const _default: { clients: Record<string, any>, config: any }',
309+
' export default _default',
310+
'}'
311+
].join('\n')
312+
}
313+
})
314+
315+
// Configure TypeScript to resolve #gql-nitro virtual module types
316+
nitro.typescript = nitro.typescript || {}
317+
nitro.typescript.tsConfig = nitro.typescript.tsConfig || {}
318+
nitro.typescript.tsConfig.compilerOptions = nitro.typescript.tsConfig.compilerOptions || {}
319+
nitro.typescript.tsConfig.compilerOptions.paths = nitro.typescript.tsConfig.compilerOptions.paths || {}
320+
nitro.typescript.tsConfig.compilerOptions.paths['#gql-nitro'] = ['./types/gql-nitro']
321+
279322
nitro.imports = defu(nitro.imports, {
280323
presets: [{
281324
from: '#gql-nitro',
@@ -287,6 +330,13 @@ export default defineNuxtModule<GqlConfig>({
287330
nitro.plugins.push(resolver.resolve('runtime/nitro'))
288331
})
289332

333+
// Add TypeScript path mapping for #gql-nitro virtual module
334+
nuxt.hook('prepare:types', ({ tsConfig, references }) => {
335+
tsConfig.compilerOptions = tsConfig.compilerOptions || {}
336+
tsConfig.compilerOptions.paths = tsConfig.compilerOptions.paths || {}
337+
tsConfig.compilerOptions.paths['#gql-nitro'] = ['./.nuxt/types/gql-nitro']
338+
})
339+
290340
const allowDocument = (f: string) => {
291341
const isSchema = f.match(/([^/]+)\.(gql|graphql)$/)?.[0]?.toLowerCase().includes('schema')
292342

0 commit comments

Comments
 (0)