Skip to content

Commit 14caa05

Browse files
committed
Use flow %checks to reduce occurance of :any
This uses a pre-release flow feature to provide predicate functions with more type checking power. It also breaks apart some functions into multiple declare function statements for cases where more specific input types result in more specific output types.
1 parent 8122ba7 commit 14caa05

13 files changed

+115
-97
lines changed

src/execution/execute.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,7 @@ function doesFragmentConditionMatch(
526526
return true;
527527
}
528528
if (isAbstractType(conditionalType)) {
529-
const abstractType = ((conditionalType: any): GraphQLAbstractType);
530-
return exeContext.schema.isPossibleType(abstractType, type);
529+
return exeContext.schema.isPossibleType(conditionalType, type);
531530
}
532531
return false;
533532
}

src/execution/values.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,14 @@ export function getVariableValues(
5757
for (let i = 0; i < varDefNodes.length; i++) {
5858
const varDefNode = varDefNodes[i];
5959
const varName = varDefNode.variable.name.value;
60-
let varType = typeFromAST(schema, varDefNode.type);
60+
const varType = typeFromAST(schema, varDefNode.type);
6161
if (!isInputType(varType)) {
6262
throw new GraphQLError(
6363
`Variable "$${varName}" expected value of type ` +
6464
`"${print(varDefNode.type)}" which cannot be used as an input type.`,
6565
[ varDefNode.type ]
6666
);
6767
}
68-
varType = ((varType: any): GraphQLInputType);
6968

7069
const value = inputs[varName];
7170
if (isInvalid(value)) {

src/type/definition.js

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ export type GraphQLInputType =
7272
GraphQLList<GraphQLInputType>
7373
>;
7474

75-
export function isInputType(type: ?GraphQLType): boolean {
76-
const namedType = getNamedType(type);
75+
export function isInputType(type: ?GraphQLType)/* : boolean %checks */ {
7776
return (
78-
namedType instanceof GraphQLScalarType ||
79-
namedType instanceof GraphQLEnumType ||
80-
namedType instanceof GraphQLInputObjectType
77+
type instanceof GraphQLScalarType ||
78+
type instanceof GraphQLEnumType ||
79+
type instanceof GraphQLInputObjectType ||
80+
type instanceof GraphQLNonNull && isInputType(type.ofType) ||
81+
type instanceof GraphQLList && isInputType(type.ofType)
8182
);
8283
}
8384

@@ -86,7 +87,7 @@ export function assertInputType(type: ?GraphQLType): GraphQLInputType {
8687
isInputType(type),
8788
`Expected ${String(type)} to be a GraphQL input type.`
8889
);
89-
return (type: any);
90+
return type;
9091
}
9192

9293
/**
@@ -108,14 +109,15 @@ export type GraphQLOutputType =
108109
GraphQLList<GraphQLOutputType>
109110
>;
110111

111-
export function isOutputType(type: ?GraphQLType): boolean {
112-
const namedType = getNamedType(type);
112+
export function isOutputType(type: ?GraphQLType)/* : boolean %checks */ {
113113
return (
114-
namedType instanceof GraphQLScalarType ||
115-
namedType instanceof GraphQLObjectType ||
116-
namedType instanceof GraphQLInterfaceType ||
117-
namedType instanceof GraphQLUnionType ||
118-
namedType instanceof GraphQLEnumType
114+
type instanceof GraphQLScalarType ||
115+
type instanceof GraphQLObjectType ||
116+
type instanceof GraphQLInterfaceType ||
117+
type instanceof GraphQLUnionType ||
118+
type instanceof GraphQLEnumType ||
119+
type instanceof GraphQLNonNull && isOutputType(type.ofType) ||
120+
type instanceof GraphQLList && isOutputType(type.ofType)
119121
);
120122
}
121123

@@ -124,7 +126,7 @@ export function assertOutputType(type: ?GraphQLType): GraphQLOutputType {
124126
isOutputType(type),
125127
`Expected ${String(type)} to be a GraphQL output type.`,
126128
);
127-
return (type: any);
129+
return type;
128130
}
129131

130132
/**
@@ -134,11 +136,10 @@ export type GraphQLLeafType =
134136
GraphQLScalarType |
135137
GraphQLEnumType;
136138

137-
export function isLeafType(type: ?GraphQLType): boolean {
138-
const namedType = getNamedType(type);
139+
export function isLeafType(type: ?GraphQLType)/* : boolean %checks */ {
139140
return (
140-
namedType instanceof GraphQLScalarType ||
141-
namedType instanceof GraphQLEnumType
141+
type instanceof GraphQLScalarType ||
142+
type instanceof GraphQLEnumType
142143
);
143144
}
144145

@@ -147,7 +148,7 @@ export function assertLeafType(type: ?GraphQLType): GraphQLLeafType {
147148
isLeafType(type),
148149
`Expected ${String(type)} to be a GraphQL leaf type.`,
149150
);
150-
return (type: any);
151+
return type;
151152
}
152153

153154
/**
@@ -158,7 +159,7 @@ export type GraphQLCompositeType =
158159
GraphQLInterfaceType |
159160
GraphQLUnionType;
160161

161-
export function isCompositeType(type: ?GraphQLType): boolean {
162+
export function isCompositeType(type: ?GraphQLType)/* : boolean %checks */ {
162163
return (
163164
type instanceof GraphQLObjectType ||
164165
type instanceof GraphQLInterfaceType ||
@@ -171,7 +172,7 @@ export function assertCompositeType(type: ?GraphQLType): GraphQLCompositeType {
171172
isCompositeType(type),
172173
`Expected ${String(type)} to be a GraphQL composite type.`,
173174
);
174-
return (type: any);
175+
return type;
175176
}
176177

177178
/**
@@ -181,7 +182,7 @@ export type GraphQLAbstractType =
181182
GraphQLInterfaceType |
182183
GraphQLUnionType;
183184

184-
export function isAbstractType(type: ?GraphQLType): boolean {
185+
export function isAbstractType(type: ?GraphQLType)/* : boolean %checks */ {
185186
return (
186187
type instanceof GraphQLInterfaceType ||
187188
type instanceof GraphQLUnionType
@@ -193,7 +194,7 @@ export function assertAbstractType(type: ?GraphQLType): GraphQLAbstractType {
193194
isAbstractType(type),
194195
`Expected ${String(type)} to be a GraphQL abstract type.`,
195196
);
196-
return (type: any);
197+
return type;
197198
}
198199

199200
/**
@@ -225,7 +226,7 @@ export type GraphQLNamedType =
225226
GraphQLEnumType |
226227
GraphQLInputObjectType;
227228

228-
export function isNamedType(type: ?GraphQLType): boolean {
229+
export function isNamedType(type: ?GraphQLType)/* : boolean %checks */ {
229230
return (
230231
type instanceof GraphQLScalarType ||
231232
type instanceof GraphQLObjectType ||
@@ -241,18 +242,24 @@ export function assertNamedType(type: ?GraphQLType): GraphQLNamedType {
241242
isNamedType(type),
242243
`Expected ${String(type)} to be a GraphQL named type.`,
243244
);
244-
return (type: any);
245+
return type;
245246
}
246247

247-
export function getNamedType(type: ?GraphQLType): ?GraphQLNamedType {
248-
let unmodifiedType = type;
249-
while (
250-
unmodifiedType instanceof GraphQLList ||
251-
unmodifiedType instanceof GraphQLNonNull
252-
) {
253-
unmodifiedType = unmodifiedType.ofType;
248+
/* eslint-disable no-redeclare */
249+
declare function getNamedType(type: void | null): void;
250+
declare function getNamedType(type: GraphQLType): GraphQLNamedType;
251+
export function getNamedType(type) {
252+
/* eslint-enable no-redeclare */
253+
if (type) {
254+
let unmodifiedType = type;
255+
while (
256+
unmodifiedType instanceof GraphQLList ||
257+
unmodifiedType instanceof GraphQLNonNull
258+
) {
259+
unmodifiedType = unmodifiedType.ofType;
260+
}
261+
return unmodifiedType;
254262
}
255-
return unmodifiedType;
256263
}
257264

258265

@@ -546,7 +553,7 @@ function isPlainObj(obj) {
546553
}
547554

548555
// If a resolver is defined, it must be a function.
549-
function isValidResolver(resolver: any): boolean {
556+
function isValidResolver(resolver: mixed): boolean {
550557
return (resolver == null || typeof resolver === 'function');
551558
}
552559

src/utilities/TypeInfo.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
GraphQLInputObjectType,
2020
GraphQLEnumType,
2121
GraphQLList,
22+
isInputType,
23+
isOutputType,
2224
} from '../type/definition';
2325
import type {
2426
GraphQLType,
@@ -117,12 +119,9 @@ export class TypeInfo {
117119
switch (node.kind) {
118120
case Kind.SELECTION_SET:
119121
const namedType = getNamedType(this.getType());
120-
let compositeType: ?GraphQLCompositeType;
121-
if (isCompositeType(namedType)) {
122-
// isCompositeType is a type refining predicate, so this is safe.
123-
compositeType = ((namedType: any): GraphQLCompositeType);
124-
}
125-
this._parentTypeStack.push(compositeType);
122+
this._parentTypeStack.push(
123+
isCompositeType(namedType) ? namedType : undefined
124+
);
126125
break;
127126
case Kind.FIELD:
128127
const parentType = this.getParentType();
@@ -153,11 +152,15 @@ export class TypeInfo {
153152
const outputType = typeConditionAST ?
154153
typeFromAST(schema, typeConditionAST) :
155154
this.getType();
156-
this._typeStack.push(((outputType: any): GraphQLOutputType));
155+
this._typeStack.push(
156+
isOutputType(outputType) ? outputType : undefined
157+
);
157158
break;
158159
case Kind.VARIABLE_DEFINITION:
159160
const inputType = typeFromAST(schema, node.type);
160-
this._inputTypeStack.push(((inputType: any): GraphQLInputType));
161+
this._inputTypeStack.push(
162+
isInputType(inputType) ? inputType : undefined
163+
);
161164
break;
162165
case Kind.ARGUMENT:
163166
let argDef;

src/utilities/buildASTSchema.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ import {
6868
GraphQLInputObjectType,
6969
GraphQLList,
7070
GraphQLNonNull,
71-
isInputType,
72-
isOutputType,
71+
assertInputType,
72+
assertOutputType,
7373
} from '../type/definition';
7474

7575
import type {
@@ -299,15 +299,11 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
299299
}
300300

301301
function produceInputType(typeNode: TypeNode): GraphQLInputType {
302-
const type = produceType(typeNode);
303-
invariant(isInputType(type), 'Expected Input type.');
304-
return (type: any);
302+
return assertInputType(produceType(typeNode));
305303
}
306304

307305
function produceOutputType(typeNode: TypeNode): GraphQLOutputType {
308-
const type = produceType(typeNode);
309-
invariant(isOutputType(type), 'Expected Output type.');
310-
return (type: any);
306+
return assertOutputType(produceType(typeNode));
311307
}
312308

313309
function produceObjectType(typeNode: TypeNode): GraphQLObjectType {

src/utilities/buildClientSchema.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export function buildClientSchema(
162162
isInputType(type),
163163
'Introspection must provide input type for arguments.'
164164
);
165-
return (type: any);
165+
return type;
166166
}
167167

168168
function getOutputType(typeRef: IntrospectionTypeRef): GraphQLOutputType {
@@ -171,7 +171,7 @@ export function buildClientSchema(
171171
isOutputType(type),
172172
'Introspection must provide output type for fields.'
173173
);
174-
return (type: any);
174+
return type;
175175
}
176176

177177
function getObjectType(typeRef: IntrospectionTypeRef): GraphQLObjectType {
@@ -180,7 +180,7 @@ export function buildClientSchema(
180180
type instanceof GraphQLObjectType,
181181
'Introspection must provide object type for possibleTypes.'
182182
);
183-
return (type: any);
183+
return type;
184184
}
185185

186186
function getInterfaceType(
@@ -191,7 +191,7 @@ export function buildClientSchema(
191191
type instanceof GraphQLInterfaceType,
192192
'Introspection must provide interface type for interfaces.'
193193
);
194-
return (type: any);
194+
return type;
195195
}
196196

197197

src/utilities/extendSchema.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import {
2525
GraphQLScalarType,
2626
GraphQLEnumType,
2727
GraphQLInputObjectType,
28-
isInputType,
29-
isOutputType,
28+
assertInputType,
29+
assertOutputType,
3030
} from '../type/definition';
3131

3232
import {
@@ -295,15 +295,11 @@ export function extendSchema(
295295
}
296296

297297
function getInputTypeFromAST(node: NamedTypeNode): GraphQLInputType {
298-
const type = getTypeFromAST(node);
299-
invariant(isInputType(type), 'Must be Input type.');
300-
return (type: any);
298+
return assertInputType(getTypeFromAST(node));
301299
}
302300

303301
function getOutputTypeFromAST(node: NamedTypeNode): GraphQLOutputType {
304-
const type = getTypeFromAST(node);
305-
invariant(isOutputType(type), 'Must be Output type.');
306-
return (type: any);
302+
return assertOutputType(getTypeFromAST(node));
307303
}
308304

309305
// Given a name, returns a type from either the existing schema or an
@@ -441,10 +437,10 @@ export function extendSchema(
441437

442438
function extendFieldType<T: GraphQLType>(typeDef: T): T {
443439
if (typeDef instanceof GraphQLList) {
444-
return (new GraphQLList(extendFieldType(typeDef.ofType)): any);
440+
return new GraphQLList(extendFieldType(typeDef.ofType));
445441
}
446442
if (typeDef instanceof GraphQLNonNull) {
447-
return (new GraphQLNonNull(extendFieldType(typeDef.ofType)): any);
443+
return new GraphQLNonNull(extendFieldType(typeDef.ofType));
448444
}
449445
return getTypeFromDef(typeDef);
450446
}

src/utilities/findBreakingChanges.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,7 @@ export function findFieldsThatChangedType(
165165
// Check if the field's type has changed in the new schema.
166166
const oldFieldType = getNamedType(oldTypeFieldsDef[fieldName].type);
167167
const newFieldType = getNamedType(newTypeFieldsDef[fieldName].type);
168-
if (oldFieldType &&
169-
newFieldType &&
170-
oldFieldType.name !== newFieldType.name) {
168+
if (oldFieldType.name !== newFieldType.name) {
171169
breakingFieldChanges.push({
172170
type: BreakingChangeType.FIELD_CHANGED_KIND,
173171
description: `${typeName}.${fieldName} changed type from ` +

src/utilities/typeComparators.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import {
1818
} from '../type/definition';
1919
import type {
2020
GraphQLType,
21-
GraphQLCompositeType,
22-
GraphQLAbstractType
21+
GraphQLCompositeType
2322
} from '../type/definition';
2423
import type {
2524
GraphQLSchema
@@ -89,10 +88,7 @@ export function isTypeSubTypeOf(
8988
// possible object type.
9089
if (isAbstractType(superType) &&
9190
maybeSubType instanceof GraphQLObjectType &&
92-
schema.isPossibleType(
93-
((superType: any): GraphQLAbstractType),
94-
maybeSubType
95-
)) {
91+
schema.isPossibleType(superType, maybeSubType)) {
9692
return true;
9793
}
9894

0 commit comments

Comments
 (0)