|
| 1 | +import*asMemFSfrom'memfs' |
| 2 | +import*asFsfrom'node:fs/promises' |
| 3 | +import{beforeEach,describe,expect,test}from'vitest' |
| 4 | +import{generate}from'../generator/generate.js' |
| 5 | + |
| 6 | +constfs=MemFS.fs.promisesasanyastypeofFs |
| 7 | + |
| 8 | +beforeEach(async()=>{ |
| 9 | +try{ |
| 10 | +awaitfs.rmdir(process.cwd(),{recursive:true}) |
| 11 | +}catch{} |
| 12 | +awaitfs.mkdir(process.cwd(),{recursive:true}) |
| 13 | +}) |
| 14 | + |
| 15 | +describe('custom root type names',()=>{ |
| 16 | +constgenerateAndGetDocument=async(sdl:string)=>{ |
| 17 | +awaitgenerate({ fs,schema:{type:'sdl', sdl}}) |
| 18 | +constcontent=MemFS.fs.readFileSync('./graffle/modules/selection-sets.ts','utf8') |
| 19 | +constmatch=content.match(/exportinterface\$Document[^}]+\}/s) |
| 20 | +expect(match).toBeTruthy() |
| 21 | +returnmatch![0] |
| 22 | +} |
| 23 | + |
| 24 | +test('uses dynamic root type names instead of hardcoded Query/Mutation',async()=>{ |
| 25 | +constdoc=awaitgenerateAndGetDocument(` |
| 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 | +constdoc=awaitgenerateAndGetDocument(` |
| 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 | +constdoc=awaitgenerateAndGetDocument(` |
| 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 | +}) |