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 } ;
@@ -40,14 +63,47 @@ export async function emitWithDiagnostics(
40
63
"@typespec/graphql" : compilerOptions ,
41
64
} ,
42
65
} ) ;
66
+
67
+ /**
68
+ * There doesn't appear to be a good way to hook into the emit process and get the GraphQLSchema
69
+ * that's produced by the emitter. So we're going to read the file that was emitted and parse it.
70
+ *
71
+ * This is the same way it's done in @typespec/openapi3:
72
+ * https://github.com/microsoft/typespec/blame/1cf8601d0f65f707926d58d56566fb0cb4d4f4ff/packages/openapi3/test/test-host.ts#L105
73
+ */
74
+
43
75
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 ] ;
76
+ const schema = content
77
+ ? buildSchema ( content , {
78
+ assumeValidSDL : true ,
79
+ noLocation : true ,
80
+ } )
81
+ : undefined ;
82
+
83
+ return [
84
+ {
85
+ graphQLSchema : schema ,
86
+ graphQLOutput : content ,
87
+ diagnostics,
88
+ } ,
89
+ ] ;
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 emitSingleSchemaWithDiagnostics (
93
+ code : string ,
94
+ options : GraphQLEmitterOptions = { } ,
95
+ ) : Promise < GraphQLTestResult > {
96
+ const schemaRecords = await emitWithDiagnostics ( code , options ) ;
97
+ expect ( schemaRecords . length ) . toBe ( 1 ) ;
98
+ return schemaRecords [ 0 ] ;
99
+ }
100
+
101
+ export async function emitSingleSchema (
102
+ code : string ,
103
+ options : GraphQLEmitterOptions = { } ,
104
+ ) : Promise < string > {
105
+ const schemaRecord = await emitSingleSchemaWithDiagnostics ( code , options ) ;
106
+ expectDiagnosticEmpty ( schemaRecord . diagnostics ) ;
107
+ ok ( schemaRecord . graphQLOutput , "Expected to have found graphql output" ) ;
108
+ return schemaRecord . graphQLOutput ;
53
109
}
0 commit comments