@@ -199,17 +199,10 @@ export function extendSchema(
199
199
const extendTypeCache = Object . create ( null ) ;
200
200
201
201
// Get the extended root operation types.
202
- const existingQueryType = schema . getQueryType ( ) ;
203
- const existingMutationType = schema . getMutationType ( ) ;
204
- const existingSubscriptionType = schema . getSubscriptionType ( ) ;
205
202
const operationTypes = {
206
- query : existingQueryType ? getExtendedType ( existingQueryType ) : null ,
207
- mutation : existingMutationType
208
- ? getExtendedType ( existingMutationType )
209
- : null ,
210
- subscription : existingSubscriptionType
211
- ? getExtendedType ( existingSubscriptionType )
212
- : null ,
203
+ query : getExtendedMaybeType ( schema . getQueryType ( ) ) ,
204
+ mutation : getExtendedMaybeType ( schema . getMutationType ( ) ) ,
205
+ subscription : getExtendedMaybeType ( schema . getSubscriptionType ( ) ) ,
213
206
} ;
214
207
215
208
// Then, incorporate all schema extensions.
@@ -220,7 +213,11 @@ export function extendSchema(
220
213
if ( operationTypes [ operation ] ) {
221
214
throw new Error ( `Must provide only one ${ operation } type in schema.` ) ;
222
215
}
223
- operationTypes [ operation ] = astBuilder . buildType ( operationType . type ) ;
216
+ const typeRef = operationType . type ;
217
+ // Note: While this could make early assertions to get the correctly
218
+ // typed values, that would throw immediately while type system
219
+ // validation with validateSchema() will produce more actionable results.
220
+ operationTypes [ operation ] = ( astBuilder . buildType ( typeRef ) : any ) ;
224
221
} ) ;
225
222
}
226
223
} ) ;
@@ -265,6 +262,10 @@ export function extendSchema(
265
262
) ;
266
263
}
267
264
265
+ function getExtendedMaybeType < T : GraphQLNamedType > (type: ?T): ?T {
266
+ return type ? getExtendedType ( type ) : null ;
267
+ }
268
+
268
269
function getExtendedType< T : GraphQLNamedType > (type: T): T {
269
270
if ( ! extendTypeCache [ type . name ] ) {
270
271
extendTypeCache [ type . name ] = extendType ( type ) ;
0 commit comments