@@ -24,6 +24,8 @@ import {
24
24
GraphQLScalarType ,
25
25
GraphQLEnumType ,
26
26
GraphQLInputObjectType ,
27
+ isInputType ,
28
+ isOutputType ,
27
29
} from '../type/definition' ;
28
30
29
31
import {
@@ -61,6 +63,8 @@ import {
61
63
import type {
62
64
GraphQLType ,
63
65
GraphQLNamedType ,
66
+ GraphQLInputType ,
67
+ GraphQLOutputType ,
64
68
} from '../type/definition' ;
65
69
66
70
import type {
@@ -200,8 +204,9 @@ export function extendSchema(
200
204
201
205
// Iterate through all types, getting the type definition for each, ensuring
202
206
// that any type not directly referenced by a field will get created.
203
- const types = Object . keys ( schema . getTypeMap ( ) ) . map ( typeName =>
204
- getTypeFromDef ( schema . getType ( typeName ) )
207
+ const typeMap = schema . getTypeMap ( ) ;
208
+ const types = Object . keys ( typeMap ) . map ( typeName =>
209
+ getTypeFromDef ( typeMap [ typeName ] )
205
210
) ;
206
211
207
212
// Do the same with new types, appending to the list of defined types.
@@ -266,6 +271,18 @@ export function extendSchema(
266
271
return type ;
267
272
}
268
273
274
+ function getInputTypeFromAST ( astNode : NamedType ) : GraphQLInputType {
275
+ const type = getTypeFromAST ( astNode ) ;
276
+ invariant ( isInputType ( type ) , 'Must be Input type.' ) ;
277
+ return ( type : any ) ;
278
+ }
279
+
280
+ function getOutputTypeFromAST ( astNode : NamedType ) : GraphQLOutputType {
281
+ const type = getTypeFromAST ( astNode ) ;
282
+ invariant ( isOutputType ( type ) , 'Must be Output type.' ) ;
283
+ return ( type : any ) ;
284
+ }
285
+
269
286
// Given a name, returns a type from either the existing schema or an
270
287
// added type.
271
288
function _getNamedType ( typeName : string ) : ?GraphQLNamedType {
@@ -387,7 +404,7 @@ export function extendSchema(
387
404
) ;
388
405
}
389
406
newFieldMap [ fieldName ] = {
390
- type : buildFieldType ( field . type ) ,
407
+ type : buildOutputFieldType ( field . type ) ,
391
408
args : buildInputValues ( field . arguments ) ,
392
409
resolve : cannotExecuteClientSchema ,
393
410
} ;
@@ -480,7 +497,7 @@ export function extendSchema(
480
497
typeAST . fields ,
481
498
field => field . name . value ,
482
499
field => ( {
483
- type : buildFieldType ( field . type ) ,
500
+ type : buildOutputFieldType ( field . type ) ,
484
501
args : buildInputValues ( field . arguments ) ,
485
502
resolve : cannotExecuteClientSchema ,
486
503
} )
@@ -492,7 +509,7 @@ export function extendSchema(
492
509
values ,
493
510
value => value . name . value ,
494
511
value => {
495
- const type = buildFieldType ( value . type ) ;
512
+ const type = buildInputFieldType ( value . type ) ;
496
513
return {
497
514
type,
498
515
defaultValue : valueFromAST ( value . defaultValue , type )
@@ -501,14 +518,24 @@ export function extendSchema(
501
518
) ;
502
519
}
503
520
504
- function buildFieldType ( typeAST : Type ) : GraphQLType {
521
+ function buildInputFieldType ( typeAST : Type ) : GraphQLInputType {
522
+ if ( typeAST . kind === LIST_TYPE ) {
523
+ return new GraphQLList ( buildInputFieldType ( typeAST . type ) ) ;
524
+ }
525
+ if ( typeAST . kind === NON_NULL_TYPE ) {
526
+ return new GraphQLNonNull ( buildInputFieldType ( typeAST . type ) ) ;
527
+ }
528
+ return getInputTypeFromAST ( typeAST ) ;
529
+ }
530
+
531
+ function buildOutputFieldType ( typeAST : Type ) : GraphQLOutputType {
505
532
if ( typeAST . kind === LIST_TYPE ) {
506
- return new GraphQLList ( buildFieldType ( typeAST . type ) ) ;
533
+ return new GraphQLList ( buildOutputFieldType ( typeAST . type ) ) ;
507
534
}
508
535
if ( typeAST . kind === NON_NULL_TYPE ) {
509
- return new GraphQLNonNull ( buildFieldType ( typeAST . type ) ) ;
536
+ return new GraphQLNonNull ( buildOutputFieldType ( typeAST . type ) ) ;
510
537
}
511
- return getTypeFromAST ( typeAST ) ;
538
+ return getOutputTypeFromAST ( typeAST ) ;
512
539
}
513
540
}
514
541
0 commit comments