|
| 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 | +}) |
0 commit comments