Skip to content

Commit d87eee2

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 88b8098 commit d87eee2

13 files changed

+105
-114
lines changed

src/execution/execute.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ import { typeFromAST } from '../utilities/typeFromAST';
1818
import * as Kind from '../language/kinds';
1919
import { getVariableValues, getArgumentValues } from './values';
2020
import {
21-
GraphQLScalarType,
2221
GraphQLObjectType,
23-
GraphQLEnumType,
2422
GraphQLList,
2523
GraphQLNonNull,
26-
GraphQLInterfaceType,
27-
GraphQLUnionType,
28-
isAbstractType
24+
isAbstractType,
25+
isLeafType,
2926
} from '../type/definition';
3027
import type {
3128
GraphQLType,
@@ -526,8 +523,7 @@ function doesFragmentConditionMatch(
526523
return true;
527524
}
528525
if (isAbstractType(conditionalType)) {
529-
const abstractType = ((conditionalType: any): GraphQLAbstractType);
530-
return exeContext.schema.isPossibleType(abstractType, type);
526+
return exeContext.schema.isPossibleType(conditionalType, type);
531527
}
532528
return false;
533529
}
@@ -827,15 +823,13 @@ function completeValue(
827823

828824
// If field type is a leaf type, Scalar or Enum, serialize to a valid value,
829825
// returning null if serialization is not possible.
830-
if (returnType instanceof GraphQLScalarType ||
831-
returnType instanceof GraphQLEnumType) {
826+
if (isLeafType(returnType)) {
832827
return completeLeafValue(returnType, result);
833828
}
834829

835830
// If field type is an abstract type, Interface or Union, determine the
836831
// runtime Object type and complete for that type.
837-
if (returnType instanceof GraphQLInterfaceType ||
838-
returnType instanceof GraphQLUnionType) {
832+
if (isAbstractType(returnType)) {
839833
return completeAbstractValue(
840834
exeContext,
841835
returnType,

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: 39 additions & 31 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,7 +136,7 @@ export type GraphQLLeafType =
134136
GraphQLScalarType |
135137
GraphQLEnumType;
136138

137-
export function isLeafType(type: ?GraphQLType): boolean {
139+
export function isLeafType(type: ?GraphQLType)/* : boolean %checks */ {
138140
return (
139141
type instanceof GraphQLScalarType ||
140142
type instanceof GraphQLEnumType
@@ -146,7 +148,7 @@ export function assertLeafType(type: ?GraphQLType): GraphQLLeafType {
146148
isLeafType(type),
147149
`Expected ${String(type)} to be a GraphQL leaf type.`,
148150
);
149-
return (type: any);
151+
return type;
150152
}
151153

152154
/**
@@ -157,7 +159,7 @@ export type GraphQLCompositeType =
157159
GraphQLInterfaceType |
158160
GraphQLUnionType;
159161

160-
export function isCompositeType(type: ?GraphQLType): boolean {
162+
export function isCompositeType(type: ?GraphQLType)/* : boolean %checks */ {
161163
return (
162164
type instanceof GraphQLObjectType ||
163165
type instanceof GraphQLInterfaceType ||
@@ -170,7 +172,7 @@ export function assertCompositeType(type: ?GraphQLType): GraphQLCompositeType {
170172
isCompositeType(type),
171173
`Expected ${String(type)} to be a GraphQL composite type.`,
172174
);
173-
return (type: any);
175+
return type;
174176
}
175177

176178
/**
@@ -180,7 +182,7 @@ export type GraphQLAbstractType =
180182
GraphQLInterfaceType |
181183
GraphQLUnionType;
182184

183-
export function isAbstractType(type: ?GraphQLType): boolean {
185+
export function isAbstractType(type: ?GraphQLType)/* : boolean %checks */ {
184186
return (
185187
type instanceof GraphQLInterfaceType ||
186188
type instanceof GraphQLUnionType
@@ -192,7 +194,7 @@ export function assertAbstractType(type: ?GraphQLType): GraphQLAbstractType {
192194
isAbstractType(type),
193195
`Expected ${String(type)} to be a GraphQL abstract type.`,
194196
);
195-
return (type: any);
197+
return type;
196198
}
197199

198200
/**
@@ -224,7 +226,7 @@ export type GraphQLNamedType =
224226
GraphQLEnumType |
225227
GraphQLInputObjectType;
226228

227-
export function isNamedType(type: ?GraphQLType): boolean {
229+
export function isNamedType(type: ?GraphQLType)/* : boolean %checks */ {
228230
return (
229231
type instanceof GraphQLScalarType ||
230232
type instanceof GraphQLObjectType ||
@@ -240,18 +242,24 @@ export function assertNamedType(type: ?GraphQLType): GraphQLNamedType {
240242
isNamedType(type),
241243
`Expected ${String(type)} to be a GraphQL named type.`,
242244
);
243-
return (type: any);
245+
return type;
244246
}
245247

246-
export function getNamedType(type: ?GraphQLType): ?GraphQLNamedType {
247-
let unmodifiedType = type;
248-
while (
249-
unmodifiedType instanceof GraphQLList ||
250-
unmodifiedType instanceof GraphQLNonNull
251-
) {
252-
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;
253262
}
254-
return unmodifiedType;
255263
}
256264

257265

@@ -545,7 +553,7 @@ function isPlainObj(obj) {
545553
}
546554

547555
// If a resolver is defined, it must be a function.
548-
function isValidResolver(resolver: any): boolean {
556+
function isValidResolver(resolver: mixed): boolean {
549557
return (resolver == null || typeof resolver === 'function');
550558
}
551559

src/type/introspection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
GraphQLInputObjectType,
2121
GraphQLList,
2222
GraphQLNonNull,
23+
isAbstractType,
2324
} from './definition';
2425
import { GraphQLString, GraphQLBoolean } from './scalars';
2526
import { DirectiveLocation } from './directives';
@@ -267,8 +268,7 @@ export const __Type = new GraphQLObjectType({
267268
possibleTypes: {
268269
type: new GraphQLList(new GraphQLNonNull(__Type)),
269270
resolve(type, args, context, { schema }) {
270-
if (type instanceof GraphQLInterfaceType ||
271-
type instanceof GraphQLUnionType) {
271+
if (isAbstractType(type)) {
272272
return schema.getPossibleTypes(type);
273273
}
274274
}

src/utilities/TypeInfo.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
getNamedType,
1818
GraphQLObjectType,
1919
GraphQLInterfaceType,
20-
GraphQLUnionType,
2120
GraphQLInputObjectType,
2221
GraphQLEnumType,
2322
GraphQLList,
@@ -120,9 +119,7 @@ export class TypeInfo {
120119
case Kind.SELECTION_SET:
121120
const namedType = getNamedType(this.getType());
122121
this._parentTypeStack.push(
123-
isCompositeType(namedType) ?
124-
((namedType: any): GraphQLCompositeType) :
125-
undefined
122+
isCompositeType(namedType) ? namedType : undefined
126123
);
127124
break;
128125
case Kind.FIELD:
@@ -155,17 +152,13 @@ export class TypeInfo {
155152
typeFromAST(schema, typeConditionAST) :
156153
this.getType();
157154
this._typeStack.push(
158-
isOutputType(outputType) ?
159-
((outputType: any): GraphQLOutputType) :
160-
undefined
155+
isOutputType(outputType) ? outputType : undefined
161156
);
162157
break;
163158
case Kind.VARIABLE_DEFINITION:
164159
const inputType = typeFromAST(schema, node.type);
165160
this._inputTypeStack.push(
166-
isInputType(inputType) ?
167-
((inputType: any): GraphQLInputType) :
168-
undefined
161+
isInputType(inputType) ? inputType : undefined
169162
);
170163
break;
171164
case Kind.ARGUMENT:
@@ -264,11 +257,7 @@ function getFieldDef(
264257
schema.getQueryType() === parentType) {
265258
return TypeMetaFieldDef;
266259
}
267-
if (name === TypeNameMetaFieldDef.name &&
268-
(parentType instanceof GraphQLObjectType ||
269-
parentType instanceof GraphQLInterfaceType ||
270-
parentType instanceof GraphQLUnionType)
271-
) {
260+
if (name === TypeNameMetaFieldDef.name && isCompositeType(parentType)) {
272261
return TypeNameMetaFieldDef;
273262
}
274263
if (parentType instanceof GraphQLObjectType ||

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

0 commit comments

Comments
 (0)