Skip to content

Commit f74bac1

Browse files
authored
fix(generator): use dynamic root type names in SelectionSets.ts (#1364)
1 parent 20c09b5 commit f74bac1

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as MemFS from 'memfs'
2+
import * as Fs from 'node:fs/promises'
3+
import { beforeEach, describe, expect, test } from 'vitest'
4+
import { generate } from '../generator/generate.js'
5+
6+
const fs = MemFS.fs.promises as any as typeof Fs
7+
8+
beforeEach(async () => {
9+
try {
10+
await fs.rmdir(process.cwd(), { recursive: true })
11+
} catch {}
12+
await fs.mkdir(process.cwd(), { recursive: true })
13+
})
14+
15+
describe('custom root type names', () => {
16+
const generateAndGetDocument = async (sdl: string) => {
17+
await generate({ fs, schema: { type: 'sdl', sdl } })
18+
const content = MemFS.fs.readFileSync('./graffle/modules/selection-sets.ts', 'utf8')
19+
const match = content.match(/export interface \$Document[^}]+\}/s)
20+
expect(match).toBeTruthy()
21+
return match![0]
22+
}
23+
24+
test('uses dynamic root type names instead of hardcoded Query/Mutation', async () => {
25+
const doc = await generateAndGetDocument(`
26+
schema { query: QueryRoot, mutation: MutationRoot }
27+
type QueryRoot { x: String }
28+
type MutationRoot { y: String }
29+
`)
30+
31+
expect(doc).toContain('QueryRoot<_$Scalars>')
32+
expect(doc).toContain('MutationRoot<_$Scalars>')
33+
expect(doc).not.toContain(': Query<')
34+
expect(doc).not.toContain(': Mutation<')
35+
})
36+
37+
test('works with standard names', async () => {
38+
const doc = await generateAndGetDocument(`
39+
type Query { x: String }
40+
type Mutation { y: String }
41+
`)
42+
43+
expect(doc).toContain('Query<_$Scalars>')
44+
expect(doc).toContain('Mutation<_$Scalars>')
45+
})
46+
47+
test('handles query-only schema', async () => {
48+
const doc = await generateAndGetDocument(`
49+
schema { query: MyQuery }
50+
type MyQuery { x: String }
51+
`)
52+
53+
expect(doc).toContain('MyQuery<_$Scalars>')
54+
expect(doc).not.toContain('mutation?:')
55+
})
56+
})

src/generator/generators/SelectionSets.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export const ModuleGeneratorSelectionSets = createModuleGenerator(
4646
parameters: $ScalarsTypeParameter,
4747
// dprint-ignore
4848
block: `
49-
${config.schema.kindMap.index.Root.query ? `query?: Record<string, Query<${i._$Scalars}>>` : ``}
50-
${config.schema.kindMap.index.Root.mutation ? `mutation?: Record<string, Mutation<${i._$Scalars}>>` : ``}
49+
${config.schema.kindMap.index.Root.query ? `query?: Record<string, ${renderName(config.schema.kindMap.index.Root.query)}<${i._$Scalars}>>` : ``}
50+
${config.schema.kindMap.index.Root.mutation ? `mutation?: Record<string, ${renderName(config.schema.kindMap.index.Root.mutation)}<${i._$Scalars}>>` : ``}
5151
`,
5252
}))
5353
code``

0 commit comments

Comments
 (0)