11import CodeBlockWriter from "code-block-writer" ;
2- import type { Server } from "../src/server" ;
32import { consola } from "consola" ;
3+ import type { ServerSideFetch } from "@aklinker1/zeta/types" ;
4+ import app from "../src/server" ;
45
56const typesFile = Bun . file ( "src/@types/gql.d.ts" ) ;
67
@@ -11,12 +12,17 @@ const scalarNameToTs = {
1112 Float : "number" ,
1213} ;
1314
14- export async function generateGqlTypes ( server : Server ) {
15+ export async function generateGqlTypes ( fetch : ServerSideFetch = app . build ( ) ) {
1516 consola . info ( "Generating GraphQL types..." ) ;
16- const introspection = await server . introspect ( ) ;
17+ const introspection = await introspect ( fetch ) ;
1718
18- const { queryType, mutationType, subscriptionType, types, directives } =
19- introspection . data . __schema ;
19+ const {
20+ queryType,
21+ mutationType,
22+ subscriptionType,
23+ types,
24+ directives : _ ,
25+ } = introspection . data . __schema ;
2026
2127 let argTypes : any [ ] = [ ] ;
2228
@@ -63,7 +69,7 @@ export async function generateGqlTypes(server: Server) {
6369
6470function capitalizeFirstLetter ( str : string ) : string {
6571 if ( str . length === 0 ) return str ;
66- return str [ 0 ] . toUpperCase ( ) + str . substring ( 1 ) ;
72+ return str [ 0 ] ! . toUpperCase ( ) + str . substring ( 1 ) ;
6773}
6874
6975function getTsTypeString ( gqlType : any ) : string {
@@ -120,3 +126,21 @@ function writeScalarType(code: CodeBlockWriter, type: any) {
120126 }
121127 code . writeLine ( `type ${ type . name } = ${ typeStr || "unknown" } ;` ) ;
122128}
129+
130+ async function introspect ( fetch : ServerSideFetch ) : Promise < any > {
131+ const request = new Request ( "http://localhost/api" , {
132+ body : JSON . stringify ( {
133+ operationName : "IntrospectionQuery" ,
134+ query :
135+ "query IntrospectionQuery { __schema { queryType { name } mutationType { name } subscriptionType { name } types { ...FullType } directives { name description locations args { ...InputValue } } } } fragment FullType on __Type { kind name description fields(includeDeprecated: true) { name description args { ...InputValue } type { ...TypeRef } isDeprecated deprecationReason } inputFields { ...InputValue } interfaces { ...TypeRef } enumValues(includeDeprecated: true) { name description isDeprecated deprecationReason } possibleTypes { ...TypeRef } } fragment InputValue on __InputValue { name description type { ...TypeRef } defaultValue } fragment TypeRef on __Type { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name } } } } } } } } " ,
136+ } ) ,
137+ headers : {
138+ "Content-Type" : "application/json" ,
139+ } ,
140+ method : "POST" ,
141+ } ) ;
142+ const res = await fetch ( request ) ;
143+ if ( res . ok ) return await res . json ( ) ;
144+
145+ throw Error ( "Introspection request failed: " + ( await res . text ( ) ) ) ;
146+ }
0 commit comments