Skip to content

Commit f06051e

Browse files
committed
More specific return types from methods in Schema
1 parent 516dfb9 commit f06051e

File tree

3 files changed

+36
-39
lines changed

3 files changed

+36
-39
lines changed

src/type/definition.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ export type GraphQLNullableType =
160160
GraphQLInputObjectType |
161161
GraphQLList;
162162

163-
export function getNullableType(type: ?GraphQLType): ?GraphQLNullableType {
163+
export function getNullableType<T: GraphQLType>(
164+
type: ?T
165+
): ?(T & GraphQLNullableType) {
164166
return type instanceof GraphQLNonNull ? type.ofType : type;
165167
}
166168

src/type/schema.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import {
1616
GraphQLList,
1717
GraphQLNonNull
1818
} from './definition';
19-
import type { GraphQLType, GraphQLAbstractType } from './definition';
19+
import type {
20+
GraphQLType,
21+
GraphQLNamedType,
22+
GraphQLAbstractType
23+
} from './definition';
2024
import { GraphQLDirective, specifiedDirectives } from './directives';
2125
import { __Schema } from './introspection';
2226
import find from '../jsutils/find';
@@ -104,7 +108,7 @@ export class GraphQLSchema {
104108
this._directives = config.directives || specifiedDirectives;
105109

106110
// Build type map now to detect any errors within this schema.
107-
let initialTypes: Array<?GraphQLType> = [
111+
let initialTypes: Array<?GraphQLNamedType> = [
108112
this.getQueryType(),
109113
this.getMutationType(),
110114
this.getSubscriptionType(),
@@ -164,7 +168,7 @@ export class GraphQLSchema {
164168
return this._typeMap;
165169
}
166170

167-
getType(name: string): ?GraphQLType {
171+
getType(name: string): ?GraphQLNamedType {
168172
return this.getTypeMap()[name];
169173
}
170174

@@ -214,13 +218,13 @@ export class GraphQLSchema {
214218
}
215219
}
216220

217-
type TypeMap = { [typeName: string]: GraphQLType }
221+
type TypeMap = { [typeName: string]: GraphQLNamedType }
218222

219223
type GraphQLSchemaConfig = {
220224
query: GraphQLObjectType;
221225
mutation?: ?GraphQLObjectType;
222226
subscription?: ?GraphQLObjectType;
223-
types?: ?Array<GraphQLType>;
227+
types?: ?Array<GraphQLNamedType>;
224228
directives?: ?Array<GraphQLDirective>;
225229
}
226230

src/utilities/extendSchema.js

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,16 @@ export function extendSchema(
190190
};
191191

192192
// Get the root Query, Mutation, and Subscription object types.
193-
const queryType = getObjectTypeFromDef(schema.getQueryType());
193+
const queryType = getTypeFromDef(schema.getQueryType());
194194

195195
const existingMutationType = schema.getMutationType();
196196
const mutationType = existingMutationType ?
197-
getObjectTypeFromDef(existingMutationType) :
197+
getTypeFromDef(existingMutationType) :
198198
null;
199199

200200
const existingSubscriptionType = schema.getSubscriptionType();
201201
const subscriptionType = existingSubscriptionType ?
202-
getObjectTypeFromDef(existingSubscriptionType) :
202+
getTypeFromDef(existingSubscriptionType) :
203203
null;
204204

205205
// Iterate through all types, getting the type definition for each, ensuring
@@ -227,10 +227,10 @@ export function extendSchema(
227227
// Below are functions used for producing this schema that have closed over
228228
// this scope and have access to the schema, cache, and newly defined types.
229229

230-
function getTypeFromDef(typeDef: GraphQLNamedType): GraphQLNamedType {
230+
function getTypeFromDef<T: GraphQLNamedType>(typeDef: T): T {
231231
const type = _getNamedType(typeDef.name);
232-
invariant(type, 'Invalid schema');
233-
return type;
232+
invariant(type, 'Missing type from schema');
233+
return (type: any);
234234
}
235235

236236
function getTypeFromAST(astNode: NamedType): GraphQLNamedType {
@@ -245,29 +245,15 @@ export function extendSchema(
245245
return type;
246246
}
247247

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-
254248
function getObjectTypeFromAST(astNode: NamedType): GraphQLObjectType {
255249
const type = getTypeFromAST(astNode);
256250
invariant(type instanceof GraphQLObjectType, 'Must be Object type.');
257251
return type;
258252
}
259253

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-
268254
function getInterfaceTypeFromAST(astNode: NamedType): GraphQLInterfaceType {
269255
const type = getTypeFromAST(astNode);
270-
invariant(type instanceof GraphQLInterfaceType, 'Must be Object type.');
256+
invariant(type instanceof GraphQLInterfaceType, 'Must be Interface type.');
271257
return type;
272258
}
273259

@@ -308,7 +294,7 @@ export function extendSchema(
308294

309295
// Given a type's introspection result, construct the correct
310296
// GraphQLType instance.
311-
function extendType(type: GraphQLType): GraphQLType {
297+
function extendType(type: GraphQLNamedType): GraphQLNamedType {
312298
if (type instanceof GraphQLObjectType) {
313299
return extendObjectType(type);
314300
}
@@ -345,15 +331,15 @@ export function extendSchema(
345331
return new GraphQLUnionType({
346332
name: type.name,
347333
description: type.description,
348-
types: type.getTypes().map(getObjectTypeFromDef),
334+
types: type.getTypes().map(getTypeFromDef),
349335
resolveType: cannotExecuteClientSchema,
350336
});
351337
}
352338

353339
function extendImplementedInterfaces(
354340
type: GraphQLObjectType
355341
): Array<GraphQLInterfaceType> {
356-
const interfaces = type.getInterfaces().map(getInterfaceTypeFromDef);
342+
const interfaces = type.getInterfaces().map(getTypeFromDef);
357343

358344
// If there are any extensions to the interfaces, apply those here.
359345
const extensions = typeExtensionsMap[type.name];
@@ -415,17 +401,17 @@ export function extendSchema(
415401
return newFieldMap;
416402
}
417403

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);
421407
}
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);
424410
}
425-
return getTypeFromDef(type);
411+
return getTypeFromDef(typeDef);
426412
}
427413

428-
function buildType(typeAST: TypeDefinition): GraphQLType {
414+
function buildType(typeAST: TypeDefinition): GraphQLNamedType {
429415
switch (typeAST.kind) {
430416
case OBJECT_TYPE_DEFINITION: return buildObjectType(typeAST);
431417
case INTERFACE_TYPE_DEFINITION: return buildInterfaceType(typeAST);
@@ -434,6 +420,7 @@ export function extendSchema(
434420
case ENUM_TYPE_DEFINITION: return buildEnumType(typeAST);
435421
case INPUT_OBJECT_TYPE_DEFINITION: return buildInputObjectType(typeAST);
436422
}
423+
throw new TypeError('Unknown type kind ' + typeAST.kind);
437424
}
438425

439426
function buildObjectType(typeAST: ObjectTypeDefinition): GraphQLObjectType {
@@ -523,7 +510,9 @@ export function extendSchema(
523510
return new GraphQLList(buildInputFieldType(typeAST.type));
524511
}
525512
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);
527516
}
528517
return getInputTypeFromAST(typeAST);
529518
}
@@ -533,7 +522,9 @@ export function extendSchema(
533522
return new GraphQLList(buildOutputFieldType(typeAST.type));
534523
}
535524
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);
537528
}
538529
return getOutputTypeFromAST(typeAST);
539530
}

0 commit comments

Comments
 (0)