1
1
import {
2
2
buildSchema ,
3
+ GraphQLFieldConfigMap ,
4
+ GraphQLFieldMap ,
3
5
GraphQLInterfaceType ,
6
+ GraphQLList ,
4
7
GraphQLNamedType ,
8
+ GraphQLNonNull ,
5
9
GraphQLObjectType ,
6
10
GraphQLSchema ,
11
+ GraphQLSemanticNonNull ,
12
+ GraphQLType ,
13
+ GraphQLUnionType ,
7
14
printSchema ,
8
15
validateSchema ,
9
16
} from "graphql" ;
10
17
import type { Maybe } from "graphql/jsutils/Maybe" ;
18
+ import { ObjMap } from "graphql/jsutils/ObjMap" ;
11
19
import { readFile , writeFile } from "node:fs/promises" ;
12
20
import { parseArgs } from "node:util" ;
13
21
@@ -48,6 +56,9 @@ export async function main(toStrict = false) {
48
56
query : convertType ( config . query ) ,
49
57
mutation : convertType ( config . mutation ) ,
50
58
subscription : convertType ( config . subscription ) ,
59
+ types : config . types
60
+ . filter ( ( t ) => ! t . name . startsWith ( "__" ) )
61
+ . map ( ( t ) => convertType ( t ) ) ,
51
62
} ) ;
52
63
53
64
const newSdl = printSchema ( derivedSchema ) ;
@@ -58,14 +69,64 @@ export async function main(toStrict = false) {
58
69
function makeConvertType ( toStrict : boolean ) {
59
70
const cache = new Map < string , GraphQLNamedType > ( ) ;
60
71
72
+ function convertFields ( fields : GraphQLFieldConfigMap < any , any > ) {
73
+ return ( ) => {
74
+ return Object . fromEntries (
75
+ Object . entries ( fields ) . map ( ( [ fieldName , spec ] ) => [
76
+ fieldName ,
77
+ {
78
+ ...spec ,
79
+ type : convertType ( spec . type ) ,
80
+ } ,
81
+ ] ) ,
82
+ ) as any ;
83
+ } ;
84
+ }
85
+
86
+ function convertTypes (
87
+ types : readonly GraphQLInterfaceType [ ] | null | undefined ,
88
+ ) : undefined | ( ( ) => readonly GraphQLInterfaceType [ ] ) ;
89
+ function convertTypes (
90
+ types : readonly GraphQLObjectType [ ] ,
91
+ ) : ( ) => readonly GraphQLObjectType [ ] ;
92
+ function convertTypes (
93
+ types : readonly GraphQLNamedType [ ] ,
94
+ ) : undefined | ( ( ) => readonly GraphQLNamedType [ ] ) ;
95
+ function convertTypes (
96
+ types : readonly GraphQLNamedType [ ] | undefined ,
97
+ ) : undefined | ( ( ) => readonly GraphQLNamedType [ ] ) ;
98
+ function convertTypes (
99
+ types : readonly GraphQLNamedType [ ] | null | undefined ,
100
+ ) : undefined | ( ( ) => readonly GraphQLNamedType [ ] ) {
101
+ if ( ! types ) return undefined ;
102
+ return ( ) => types . map ( ( t ) => convertType ( t ) ) ;
103
+ }
104
+
61
105
function convertType ( type : null | undefined ) : null | undefined ;
62
106
function convertType ( type : GraphQLObjectType ) : GraphQLObjectType ;
63
107
function convertType (
64
108
type : Maybe < GraphQLObjectType > ,
65
109
) : Maybe < GraphQLObjectType > ;
66
110
function convertType ( type : GraphQLNamedType ) : GraphQLNamedType ;
67
- function convertType ( type : GraphQLNamedType | null | undefined ) {
111
+ function convertType ( type : GraphQLType ) : GraphQLType ;
112
+ function convertType ( type : GraphQLType | null | undefined ) {
68
113
if ( ! type ) return type ;
114
+ if ( type instanceof GraphQLSemanticNonNull ) {
115
+ const unwrapped = convertType ( type . ofType ) ;
116
+ // Here's where we do our thing!
117
+ if ( toStrict ) {
118
+ return new GraphQLNonNull ( unwrapped ) ;
119
+ } else {
120
+ return unwrapped ;
121
+ }
122
+ } else if ( type instanceof GraphQLNonNull ) {
123
+ return new GraphQLNonNull ( convertType ( type . ofType ) ) ;
124
+ } else if ( type instanceof GraphQLList ) {
125
+ return new GraphQLList ( convertType ( type . ofType ) ) ;
126
+ }
127
+ if ( type . name . startsWith ( "__" ) ) {
128
+ return null ;
129
+ }
69
130
if ( cache . has ( type . name ) ) {
70
131
return cache . get ( type . name ) ;
71
132
}
@@ -74,11 +135,21 @@ function makeConvertType(toStrict: boolean) {
74
135
const config = type . toConfig ( ) ;
75
136
return new GraphQLObjectType ( {
76
137
...config ,
138
+ fields : convertFields ( config . fields ) ,
139
+ interfaces : convertTypes ( config . interfaces ) ,
77
140
} ) ;
78
141
} else if ( type instanceof GraphQLInterfaceType ) {
79
142
const config = type . toConfig ( ) ;
80
143
return new GraphQLInterfaceType ( {
81
144
...config ,
145
+ fields : convertFields ( config . fields ) ,
146
+ interfaces : convertTypes ( config . interfaces ) ,
147
+ } ) ;
148
+ } else if ( type instanceof GraphQLUnionType ) {
149
+ const config = type . toConfig ( ) ;
150
+ return new GraphQLUnionType ( {
151
+ ...config ,
152
+ types : convertTypes ( config . types ) ,
82
153
} ) ;
83
154
} else {
84
155
return type ;
0 commit comments