@@ -190,16 +190,16 @@ export function extendSchema(
190
190
} ;
191
191
192
192
// Get the root Query, Mutation, and Subscription object types.
193
- const queryType = getObjectTypeFromDef ( schema . getQueryType ( ) ) ;
193
+ const queryType = getTypeFromDef ( schema . getQueryType ( ) ) ;
194
194
195
195
const existingMutationType = schema . getMutationType ( ) ;
196
196
const mutationType = existingMutationType ?
197
- getObjectTypeFromDef ( existingMutationType ) :
197
+ getTypeFromDef ( existingMutationType ) :
198
198
null ;
199
199
200
200
const existingSubscriptionType = schema . getSubscriptionType ( ) ;
201
201
const subscriptionType = existingSubscriptionType ?
202
- getObjectTypeFromDef ( existingSubscriptionType ) :
202
+ getTypeFromDef ( existingSubscriptionType ) :
203
203
null ;
204
204
205
205
// Iterate through all types, getting the type definition for each, ensuring
@@ -227,10 +227,10 @@ export function extendSchema(
227
227
// Below are functions used for producing this schema that have closed over
228
228
// this scope and have access to the schema, cache, and newly defined types.
229
229
230
- function getTypeFromDef ( typeDef : GraphQLNamedType ) : GraphQLNamedType {
230
+ function getTypeFromDef < T : GraphQLNamedType > (typeDef: T ): T {
231
231
const type = _getNamedType ( typeDef . name ) ;
232
- invariant ( type , 'Invalid schema' ) ;
233
- return type ;
232
+ invariant ( type , 'Missing type from schema' ) ;
233
+ return ( type : any ) ;
234
234
}
235
235
236
236
function getTypeFromAST ( astNode : NamedType ) : GraphQLNamedType {
@@ -245,29 +245,15 @@ export function extendSchema(
245
245
return type ;
246
246
}
247
247
248
- function getObjectTypeFromDef ( typeDef : GraphQLObjectType ) : GraphQLObjectType {
249
- const type = getTypeFromDef ( typeDef ) ;
250
- invariant ( type instanceof GraphQLObjectType , 'Must be Object type.' ) ;
251
- return type ;
252
- }
253
-
254
248
function getObjectTypeFromAST ( astNode : NamedType ) : GraphQLObjectType {
255
249
const type = getTypeFromAST ( astNode ) ;
256
250
invariant ( type instanceof GraphQLObjectType , 'Must be Object type.' ) ;
257
251
return type ;
258
252
}
259
253
260
- function getInterfaceTypeFromDef (
261
- typeDef : GraphQLInterfaceType
262
- ) : GraphQLInterfaceType {
263
- const type = getTypeFromDef ( typeDef ) ;
264
- invariant ( type instanceof GraphQLInterfaceType , 'Must be Object type.' ) ;
265
- return type ;
266
- }
267
-
268
254
function getInterfaceTypeFromAST ( astNode : NamedType ) : GraphQLInterfaceType {
269
255
const type = getTypeFromAST ( astNode ) ;
270
- invariant ( type instanceof GraphQLInterfaceType , 'Must be Object type.' ) ;
256
+ invariant ( type instanceof GraphQLInterfaceType , 'Must be Interface type.' ) ;
271
257
return type ;
272
258
}
273
259
@@ -308,7 +294,7 @@ export function extendSchema(
308
294
309
295
// Given a type's introspection result, construct the correct
310
296
// GraphQLType instance.
311
- function extendType ( type : GraphQLType ) : GraphQLType {
297
+ function extendType ( type : GraphQLNamedType ) : GraphQLNamedType {
312
298
if ( type instanceof GraphQLObjectType ) {
313
299
return extendObjectType ( type ) ;
314
300
}
@@ -345,15 +331,15 @@ export function extendSchema(
345
331
return new GraphQLUnionType ( {
346
332
name : type . name ,
347
333
description : type . description ,
348
- types : type . getTypes ( ) . map ( getObjectTypeFromDef ) ,
334
+ types : type . getTypes ( ) . map ( getTypeFromDef ) ,
349
335
resolveType : cannotExecuteClientSchema ,
350
336
} ) ;
351
337
}
352
338
353
339
function extendImplementedInterfaces (
354
340
type : GraphQLObjectType
355
341
) : Array < GraphQLInterfaceType > {
356
- const interfaces = type . getInterfaces ( ) . map ( getInterfaceTypeFromDef ) ;
342
+ const interfaces = type . getInterfaces ( ) . map ( getTypeFromDef ) ;
357
343
358
344
// If there are any extensions to the interfaces, apply those here.
359
345
const extensions = typeExtensionsMap [ type . name ] ;
@@ -415,17 +401,17 @@ export function extendSchema(
415
401
return newFieldMap ;
416
402
}
417
403
418
- function extendFieldType ( type : GraphQLType ) : GraphQLType {
419
- if ( type instanceof GraphQLList ) {
420
- return new GraphQLList ( extendFieldType ( type . ofType ) ) ;
404
+ function extendFieldType < T : GraphQLType > (typeDef: T ): T {
405
+ if ( typeDef instanceof GraphQLList ) {
406
+ return ( new GraphQLList ( extendFieldType ( typeDef . ofType ) ) : any ) ;
421
407
}
422
- if ( type instanceof GraphQLNonNull ) {
423
- return new GraphQLNonNull ( extendFieldType ( type . ofType ) ) ;
408
+ if ( typeDef instanceof GraphQLNonNull ) {
409
+ return ( new GraphQLNonNull ( extendFieldType ( typeDef . ofType ) ) : any ) ;
424
410
}
425
- return getTypeFromDef ( type ) ;
411
+ return getTypeFromDef ( typeDef ) ;
426
412
}
427
413
428
- function buildType ( typeAST : TypeDefinition ) : GraphQLType {
414
+ function buildType ( typeAST : TypeDefinition ) : GraphQLNamedType {
429
415
switch ( typeAST . kind ) {
430
416
case OBJECT_TYPE_DEFINITION : return buildObjectType ( typeAST ) ;
431
417
case INTERFACE_TYPE_DEFINITION : return buildInterfaceType ( typeAST ) ;
@@ -434,6 +420,7 @@ export function extendSchema(
434
420
case ENUM_TYPE_DEFINITION : return buildEnumType ( typeAST ) ;
435
421
case INPUT_OBJECT_TYPE_DEFINITION : return buildInputObjectType ( typeAST ) ;
436
422
}
423
+ throw new TypeError ( 'Unknown type kind ' + typeAST . kind ) ;
437
424
}
438
425
439
426
function buildObjectType ( typeAST : ObjectTypeDefinition ) : GraphQLObjectType {
@@ -523,7 +510,9 @@ export function extendSchema(
523
510
return new GraphQLList ( buildInputFieldType ( typeAST . type ) ) ;
524
511
}
525
512
if ( typeAST . kind === NON_NULL_TYPE ) {
526
- return new GraphQLNonNull ( buildInputFieldType ( typeAST . type ) ) ;
513
+ const nullableType = buildInputFieldType ( typeAST . type ) ;
514
+ invariant ( ! ( nullableType instanceof GraphQLNonNull ) , 'Must be nullable' ) ;
515
+ return new GraphQLNonNull ( nullableType ) ;
527
516
}
528
517
return getInputTypeFromAST ( typeAST ) ;
529
518
}
@@ -533,7 +522,9 @@ export function extendSchema(
533
522
return new GraphQLList ( buildOutputFieldType ( typeAST . type ) ) ;
534
523
}
535
524
if ( typeAST . kind === NON_NULL_TYPE ) {
536
- return new GraphQLNonNull ( buildOutputFieldType ( typeAST . type ) ) ;
525
+ const nullableType = buildOutputFieldType ( typeAST . type ) ;
526
+ invariant ( ! ( nullableType instanceof GraphQLNonNull ) , 'Must be nullable' ) ;
527
+ return new GraphQLNonNull ( nullableType ) ;
537
528
}
538
529
return getOutputTypeFromAST ( typeAST ) ;
539
530
}
0 commit comments