1
- import type { Diagnostic } from "@typespec/compiler" ;
1
+ import type { Diagnostic , Program , Type } from "@typespec/compiler" ;
2
2
import {
3
3
createTestHost ,
4
4
createTestWrapper ,
5
5
expectDiagnosticEmpty ,
6
6
resolveVirtualPath ,
7
7
} from "@typespec/compiler/testing" ;
8
8
import { ok } from "assert" ;
9
+ import type { GraphQLSchema } from "graphql" ;
10
+ import { buildSchema } from "graphql" ;
11
+ import { expect } from "vitest" ;
9
12
import type { GraphQLEmitterOptions } from "../src/lib.js" ;
10
13
import { GraphqlTestLibrary } from "../src/testing/index.js" ;
11
14
@@ -15,21 +18,41 @@ export async function createGraphqlTestHost() {
15
18
} ) ;
16
19
}
17
20
21
+ export interface GraphQLTestResult {
22
+ readonly graphQLSchema ?: GraphQLSchema ;
23
+ readonly graphQLOutput ?: string ;
24
+ readonly diagnostics : readonly Diagnostic [ ] ;
25
+ }
26
+
18
27
export async function createGraphqlTestRunner ( ) {
19
28
const host = await createGraphqlTestHost ( ) ;
20
29
21
30
return createTestWrapper ( host , {
31
+ autoUsings : [ "TypeSpec.GraphQL" ] ,
22
32
compilerOptions : {
23
33
noEmit : false ,
24
34
emit : [ "@typespec/graphql" ] ,
25
35
} ,
26
36
} ) ;
27
37
}
28
38
39
+ export async function diagnose ( code : string ) : Promise < readonly Diagnostic [ ] > {
40
+ const runner = await createGraphqlTestRunner ( ) ;
41
+ return runner . diagnose ( code ) ;
42
+ }
43
+
44
+ export async function compileAndDiagnose < T extends Record < string , Type > > (
45
+ code : string ,
46
+ ) : Promise < [ Program , T , readonly Diagnostic [ ] ] > {
47
+ const runner = await createGraphqlTestRunner ( ) ;
48
+ const [ testTypes , diagnostics ] = await runner . compileAndDiagnose ( code ) ;
49
+ return [ runner . program , testTypes as T , diagnostics ] ;
50
+ }
51
+
29
52
export async function emitWithDiagnostics (
30
53
code : string ,
31
54
options : GraphQLEmitterOptions = { } ,
32
- ) : Promise < [ string , readonly Diagnostic [ ] ] > {
55
+ ) : Promise < readonly GraphQLTestResult [ ] > {
33
56
const runner = await createGraphqlTestRunner ( ) ;
34
57
const outputFile = resolveVirtualPath ( "schema.graphql" ) ;
35
58
const compilerOptions = { ...options , "output-file" : outputFile } ;
@@ -41,13 +64,37 @@ export async function emitWithDiagnostics(
41
64
} ,
42
65
} ) ;
43
66
const content = runner . fs . get ( outputFile ) ;
44
- ok ( content , "Expected to have found graphql output" ) ;
45
- // Change this to whatever makes sense for the actual GraphQL emitter, probably a GraphQLSchemaRecord
46
- return [ content , diagnostics ] ;
67
+ const schema = content
68
+ ? buildSchema ( content , {
69
+ assumeValidSDL : true ,
70
+ noLocation : true ,
71
+ } )
72
+ : undefined ;
73
+
74
+ return [
75
+ {
76
+ graphQLSchema : schema ,
77
+ graphQLOutput : content ,
78
+ diagnostics,
79
+ } ,
80
+ ] ;
81
+ }
82
+
83
+ export async function emitSingleSchemaWithDiagnostics (
84
+ code : string ,
85
+ options : GraphQLEmitterOptions = { } ,
86
+ ) : Promise < GraphQLTestResult > {
87
+ const schemaRecords = await emitWithDiagnostics ( code , options ) ;
88
+ expect ( schemaRecords . length ) . toBe ( 1 ) ;
89
+ return schemaRecords [ 0 ] ;
47
90
}
48
91
49
- export async function emit ( code : string , options : GraphQLEmitterOptions = { } ) : Promise < string > {
50
- const [ result , diagnostics ] = await emitWithDiagnostics ( code , options ) ;
51
- expectDiagnosticEmpty ( diagnostics ) ;
52
- return result ;
92
+ export async function emitSingleSchema (
93
+ code : string ,
94
+ options : GraphQLEmitterOptions = { } ,
95
+ ) : Promise < string > {
96
+ const schemaRecord = await emitSingleSchemaWithDiagnostics ( code , options ) ;
97
+ expectDiagnosticEmpty ( schemaRecord . diagnostics ) ;
98
+ ok ( schemaRecord . graphQLOutput , "Expected to have found graphql output" ) ;
99
+ return schemaRecord . graphQLOutput ;
53
100
}
0 commit comments