@@ -4,15 +4,16 @@ import type {
4
4
GraphQLInterfaceType ,
5
5
GraphQLInputObjectType ,
6
6
GraphQLObjectType ,
7
- GraphQLScalarType ,
8
7
GraphQLSchema ,
9
8
GraphQLUnionType ,
9
+ GraphQLNamedInputType ,
10
10
GraphQLNamedType ,
11
11
GraphQLInputType ,
12
12
GraphQLLeafType ,
13
13
GraphQLType ,
14
14
GraphQLNullableType ,
15
15
GraphQLOutputType ,
16
+ GraphQLScalarType ,
16
17
OperationTypeNode ,
17
18
TypeNode ,
18
19
} from 'graphql' ;
@@ -183,25 +184,93 @@ class TypeTree {
183
184
}
184
185
}
185
186
186
- function getPossibleInputTypes (
187
+ interface InputTypeInfo {
188
+ nonNullListWrappers : Array < boolean > ;
189
+ nonNull : boolean ;
190
+ namedType : GraphQLNamedInputType ;
191
+ }
192
+
193
+ function getInputTypeInfo (
187
194
type : GraphQLInputType ,
188
- ) : Array < GraphQLInputType > {
189
- if ( _isListType ( type ) ) {
190
- return [
191
- ... getPossibleInputTypes ( type . ofType ) . map (
192
- ( possibleType ) => new GraphQLList ( possibleType ) ,
193
- ) ,
194
- ... getPossibleInputTypes ( type . ofType ) . map (
195
- ( possibleType ) => new GraphQLNonNull ( new GraphQLList ( possibleType ) ) ,
196
- ) ,
197
- ] ;
195
+ wrapper ?:
196
+ | GraphQLNonNull < GraphQLNullableInputType >
197
+ | GraphQLList < GraphQLInputType > ,
198
+ ) : InputTypeInfo {
199
+ if ( ! _isNonNullType ( type ) && ! _isListType ( type ) ) {
200
+ return {
201
+ nonNullListWrappers : [ ] ,
202
+ nonNull : _isNonNullType ( wrapper ) ,
203
+ namedType : type ,
204
+ } ;
198
205
}
199
206
207
+ const inputTypeInfo = getInputTypeInfo ( type . ofType , type ) ;
200
208
if ( _isNonNullType ( type ) ) {
201
- return [ ...getPossibleInputTypes ( type . ofType ) ] ;
209
+ return inputTypeInfo ;
210
+ }
211
+
212
+ inputTypeInfo . nonNullListWrappers . push ( _isNonNullType ( wrapper ) ) ;
213
+
214
+ return inputTypeInfo ;
215
+ }
216
+
217
+ function getPossibleSequences (
218
+ nonNullListWrappers : Array < boolean > ,
219
+ ) : Array < Array < boolean > > {
220
+ if ( ! nonNullListWrappers . length ) {
221
+ return [ [ ] ] ;
222
+ }
223
+
224
+ const nonNull = nonNullListWrappers . pop ( ) ;
225
+ if ( nonNull ) {
226
+ return getPossibleSequences ( nonNullListWrappers ) . map ( ( sequence ) => [
227
+ true ,
228
+ ...sequence ,
229
+ ] ) ;
230
+ }
231
+
232
+ return [
233
+ ...getPossibleSequences ( nonNullListWrappers ) . map ( ( sequence ) => [
234
+ true ,
235
+ ...sequence ,
236
+ ] ) ,
237
+ ...getPossibleSequences ( nonNullListWrappers ) . map ( ( sequence ) => [
238
+ false ,
239
+ ...sequence ,
240
+ ] ) ,
241
+ ] ;
242
+ }
243
+
244
+ function inputTypesFromSequences (
245
+ sequences : Array < Array < boolean > > ,
246
+ inputType : GraphQLInputType ,
247
+ ) : Array < GraphQLInputType > {
248
+ return sequences . map ( ( sequence ) =>
249
+ sequence . reduce ( ( acc , nonNull ) => {
250
+ let wrapped = new GraphQLList ( acc ) ;
251
+ if ( nonNull ) {
252
+ wrapped = new GraphQLNonNull ( wrapped ) ;
253
+ }
254
+ return wrapped ;
255
+ } , inputType ) ,
256
+ ) ;
257
+ }
258
+
259
+ function getPossibleInputTypes (
260
+ type : GraphQLInputType ,
261
+ ) : Array < GraphQLInputType > {
262
+ const { nonNullListWrappers, nonNull, namedType } = getInputTypeInfo ( type ) ;
263
+ const sequences = getPossibleSequences ( nonNullListWrappers ) ;
264
+
265
+ const wrapped = new GraphQLNonNull ( namedType ) ;
266
+ if ( nonNull ) {
267
+ return inputTypesFromSequences ( sequences , wrapped ) ;
202
268
}
203
269
204
- return [ new GraphQLNonNull ( type ) , type ] ;
270
+ return [
271
+ ...inputTypesFromSequences ( sequences , namedType ) ,
272
+ ...inputTypesFromSequences ( sequences , wrapped ) ,
273
+ ] ;
205
274
}
206
275
207
276
function _toExecutorSchema ( schema : GraphQLSchema ) : ExecutorSchema {
0 commit comments