@@ -11,6 +11,7 @@ import {
11
11
GraphQLSemanticNonNull ,
12
12
GraphQLType ,
13
13
GraphQLUnionType ,
14
+ Kind ,
14
15
printSchema ,
15
16
validateSchema ,
16
17
} from "graphql" ;
@@ -59,6 +60,7 @@ export async function main(toStrict = false) {
59
60
types : config . types
60
61
. filter ( ( t ) => ! t . name . startsWith ( "__" ) )
61
62
. map ( ( t ) => convertType ( t ) ) ,
63
+ directives : config . directives . filter ( ( d ) => d . name !== "semanticNonNull" ) ,
62
64
} ) ;
63
65
64
66
const newSdl = printSchema ( derivedSchema ) ;
@@ -72,13 +74,33 @@ function makeConvertType(toStrict: boolean) {
72
74
function convertFields ( fields : GraphQLFieldConfigMap < any , any > ) {
73
75
return ( ) => {
74
76
return Object . fromEntries (
75
- Object . entries ( fields ) . map ( ( [ fieldName , spec ] ) => [
76
- fieldName ,
77
- {
78
- ...spec ,
79
- type : convertType ( spec . type ) ,
80
- } ,
81
- ] ) ,
77
+ Object . entries ( fields ) . map ( ( [ fieldName , spec ] ) => {
78
+ const directive = spec . astNode ?. directives ?. find (
79
+ ( d ) => d . name . value === "semanticNonNull" ,
80
+ ) ;
81
+ const levelsArg = directive ?. arguments ?. find (
82
+ ( a ) => a . name . value === "levels" ,
83
+ ) ;
84
+ const levels =
85
+ levelsArg ?. value ?. kind === Kind . LIST
86
+ ? levelsArg . value . values
87
+ . filter ( ( v ) => v . kind === Kind . INT )
88
+ . map ( ( v ) => Number ( v . value ) )
89
+ : [ 0 ] ;
90
+ return [
91
+ fieldName ,
92
+ {
93
+ ...spec ,
94
+ type : convertType ( spec . type , directive ? levels : undefined ) ,
95
+ astNode : spec . astNode && {
96
+ ...spec . astNode ,
97
+ directives : spec . astNode ?. directives ?. filter (
98
+ ( d ) => d . name . value !== "semanticNonNull" ,
99
+ ) ,
100
+ } ,
101
+ } ,
102
+ ] ;
103
+ } ) ,
82
104
) as any ;
83
105
} ;
84
106
}
@@ -102,14 +124,30 @@ function makeConvertType(toStrict: boolean) {
102
124
return ( ) => types . map ( ( t ) => convertType ( t ) ) ;
103
125
}
104
126
105
- function convertType ( type : null | undefined ) : null | undefined ;
106
- function convertType ( type : GraphQLObjectType ) : GraphQLObjectType ;
127
+ function convertType (
128
+ type : null | undefined ,
129
+ semanticNonNullLevels ?: number [ ] ,
130
+ ) : null | undefined ;
131
+ function convertType (
132
+ type : GraphQLObjectType ,
133
+ semanticNonNullLevels ?: number [ ] ,
134
+ ) : GraphQLObjectType ;
107
135
function convertType (
108
136
type : Maybe < GraphQLObjectType > ,
137
+ semanticNonNullLevels ?: number [ ] ,
109
138
) : Maybe < GraphQLObjectType > ;
110
- function convertType ( type : GraphQLNamedType ) : GraphQLNamedType ;
111
- function convertType ( type : GraphQLType ) : GraphQLType ;
112
- function convertType ( type : GraphQLType | null | undefined ) {
139
+ function convertType (
140
+ type : GraphQLNamedType ,
141
+ semanticNonNullLevels ?: number [ ] ,
142
+ ) : GraphQLNamedType ;
143
+ function convertType (
144
+ type : GraphQLType ,
145
+ semanticNonNullLevels ?: number [ ] ,
146
+ ) : GraphQLType ;
147
+ function convertType (
148
+ type : GraphQLType | null | undefined ,
149
+ semanticNonNullLevels ?: number [ ] ,
150
+ ) {
113
151
if ( ! type ) return type ;
114
152
if ( type instanceof GraphQLSemanticNonNull ) {
115
153
const unwrapped = convertType ( type . ofType ) ;
@@ -122,7 +160,16 @@ function makeConvertType(toStrict: boolean) {
122
160
} else if ( type instanceof GraphQLNonNull ) {
123
161
return new GraphQLNonNull ( convertType ( type . ofType ) ) ;
124
162
} else if ( type instanceof GraphQLList ) {
125
- return new GraphQLList ( convertType ( type . ofType ) ) ;
163
+ const innerLevels = semanticNonNullLevels ?. includes ( 1 ) ? [ 0 ] : undefined ;
164
+ if ( semanticNonNullLevels ?. includes ( 0 ) && toStrict ) {
165
+ return new GraphQLNonNull (
166
+ new GraphQLList ( convertType ( type . ofType , innerLevels ) ) ,
167
+ ) ;
168
+ } else {
169
+ return new GraphQLList ( convertType ( type . ofType , innerLevels ) ) ;
170
+ }
171
+ } else if ( semanticNonNullLevels ?. includes ( 0 ) && toStrict ) {
172
+ return new GraphQLNonNull ( convertType ( type ) ) ;
126
173
}
127
174
if ( type . name . startsWith ( "__" ) ) {
128
175
return null ;
0 commit comments