Skip to content

Commit 1ee5907

Browse files
committed
Clarify types of types, remove some flow casting
1 parent fc9cb5a commit 1ee5907

File tree

7 files changed

+36
-30
lines changed

7 files changed

+36
-30
lines changed

src/execution/values.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,15 @@ function coerceValue(type: GraphQLInputType, value: any): any {
126126
if (type instanceof GraphQLNonNull) {
127127
// Note: we're not checking that the result of coerceValue is non-null.
128128
// We only call this function after calling isValidJSValue.
129-
const nullableType: GraphQLInputType = (type.ofType: any);
130-
return coerceValue(nullableType, value);
129+
return coerceValue(type.ofType, value);
131130
}
132131

133132
if (isNullish(value)) {
134133
return null;
135134
}
136135

137136
if (type instanceof GraphQLList) {
138-
const itemType: GraphQLInputType = (type.ofType: any);
137+
const itemType = type.ofType;
139138
// TODO: support iterable input
140139
if (Array.isArray(value)) {
141140
return value.map(item => coerceValue(itemType, item));

src/type/definition.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ export type GraphQLInputType =
5656
GraphQLScalarType |
5757
GraphQLEnumType |
5858
GraphQLInputObjectType |
59-
GraphQLList |
60-
GraphQLNonNull;
59+
GraphQLList<GraphQLInputType> |
60+
GraphQLNonNull<
61+
GraphQLScalarType |
62+
GraphQLEnumType |
63+
GraphQLInputObjectType |
64+
GraphQLList<GraphQLInputType>
65+
>;
6166

6267
export function isInputType(type: ?GraphQLType): boolean {
6368
const namedType = getNamedType(type);
@@ -77,8 +82,15 @@ export type GraphQLOutputType =
7782
GraphQLInterfaceType |
7883
GraphQLUnionType |
7984
GraphQLEnumType |
80-
GraphQLList |
81-
GraphQLNonNull;
85+
GraphQLList<GraphQLOutputType> |
86+
GraphQLNonNull<
87+
GraphQLScalarType |
88+
GraphQLObjectType |
89+
GraphQLInterfaceType |
90+
GraphQLUnionType |
91+
GraphQLEnumType |
92+
GraphQLList<GraphQLOutputType>
93+
>;
8294

8395
export function isOutputType(type: ?GraphQLType): boolean {
8496
const namedType = getNamedType(type);
@@ -995,10 +1007,10 @@ export type InputObjectFieldMap = {
9951007
* })
9961008
*
9971009
*/
998-
export class GraphQLList {
999-
ofType: GraphQLType;
1010+
export class GraphQLList<T: GraphQLType> {
1011+
ofType: T;
10001012

1001-
constructor(type: GraphQLType) {
1013+
constructor(type: T) {
10021014
invariant(
10031015
isType(type),
10041016
`Can only create List of a GraphQLType but got: ${type}.`
@@ -1032,10 +1044,10 @@ export class GraphQLList {
10321044
*
10331045
* Note: the enforcement of non-nullability occurs within the executor.
10341046
*/
1035-
export class GraphQLNonNull {
1036-
ofType: GraphQLNullableType;
1047+
export class GraphQLNonNull<T: GraphQLNullableType> {
1048+
ofType: T;
10371049

1038-
constructor(type: GraphQLNullableType) {
1050+
constructor(type: T) {
10391051
invariant(
10401052
isType(type) && !(type instanceof GraphQLNonNull),
10411053
`Can only create NonNull of a Nullable GraphQLType but got: ${type}.`

src/utilities/TypeInfo.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,7 @@ export class TypeInfo {
171171
case Kind.LIST:
172172
const listType = getNullableType(this.getInputType());
173173
this._inputTypeStack.push(
174-
listType instanceof GraphQLList ?
175-
((listType.ofType: any): GraphQLInputType) :
176-
undefined
174+
listType instanceof GraphQLList ? listType.ofType : undefined
177175
);
178176
break;
179177
case Kind.OBJECT_FIELD:

src/utilities/astFromValue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export function astFromValue(
7373
// Because GraphQL will accept single values as a "list of one" when
7474
// expecting a list, if there's a non-array value and an expected list type,
7575
// create an AST using the list's item type.
76-
return astFromValue(value, (type: any).ofType);
76+
return astFromValue(value, type.ofType);
7777
}
7878

7979
if (typeof value === 'boolean') {

src/utilities/isValidJSValue.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ import type { GraphQLInputType } from '../type/definition';
2828
export function isValidJSValue(value: any, type: GraphQLInputType): [ string ] {
2929
// A value must be provided if the type is non-null.
3030
if (type instanceof GraphQLNonNull) {
31-
const ofType: GraphQLInputType = (type.ofType: any);
3231
if (isNullish(value)) {
33-
if (ofType.name) {
34-
return [ `Expected "${ofType.name}!", found null.` ];
32+
if (type.ofType.name) {
33+
return [ `Expected "${type.ofType.name}!", found null.` ];
3534
}
3635
return [ 'Expected non-null value, found null.' ];
3736
}
38-
return isValidJSValue(value, ofType);
37+
return isValidJSValue(value, type.ofType);
3938
}
4039

4140
if (isNullish(value)) {
@@ -44,7 +43,7 @@ export function isValidJSValue(value: any, type: GraphQLInputType): [ string ] {
4443

4544
// Lists accept a non-list value as a list of one.
4645
if (type instanceof GraphQLList) {
47-
const itemType: GraphQLInputType = (type.ofType: any);
46+
const itemType = type.ofType;
4847
if (Array.isArray(value)) {
4948
return value.reduce((acc, item, index) => {
5049
const errors = isValidJSValue(item, itemType);

src/utilities/isValidLiteralValue.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@ export function isValidLiteralValue(
4141
): [ string ] {
4242
// A value must be provided if the type is non-null.
4343
if (type instanceof GraphQLNonNull) {
44-
const ofType: GraphQLInputType = (type.ofType: any);
4544
if (!valueAST) {
46-
if (ofType.name) {
47-
return [ `Expected "${ofType.name}!", found null.` ];
45+
if (type.ofType.name) {
46+
return [ `Expected "${type.ofType.name}!", found null.` ];
4847
}
4948
return [ 'Expected non-null value, found null.' ];
5049
}
51-
return isValidLiteralValue(ofType, valueAST);
50+
return isValidLiteralValue(type.ofType, valueAST);
5251
}
5352

5453
if (!valueAST) {
@@ -63,7 +62,7 @@ export function isValidLiteralValue(
6362

6463
// Lists accept a non-list value as a list of one.
6564
if (type instanceof GraphQLList) {
66-
const itemType: GraphQLInputType = (type.ofType: any);
65+
const itemType = type.ofType;
6766
if (valueAST.kind === LIST) {
6867
return (valueAST: ListValue).values.reduce((acc, itemAST, index) => {
6968
const errors = isValidLiteralValue(itemType, itemAST);

src/utilities/valueFromAST.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,10 @@ export function valueFromAST(
4949
variables?: ?{ [key: string]: any }
5050
): any {
5151
if (type instanceof GraphQLNonNull) {
52-
const nullableType: GraphQLInputType = (type.ofType: any);
5352
// Note: we're not checking that the result of valueFromAST is non-null.
5453
// We're assuming that this query has been validated and the value used
5554
// here is of the correct type.
56-
return valueFromAST(valueAST, nullableType, variables);
55+
return valueFromAST(valueAST, type.ofType, variables);
5756
}
5857

5958
if (!valueAST) {
@@ -72,7 +71,7 @@ export function valueFromAST(
7271
}
7372

7473
if (type instanceof GraphQLList) {
75-
const itemType: GraphQLInputType = (type.ofType: any);
74+
const itemType = type.ofType;
7675
if (valueAST.kind === Kind.LIST) {
7776
return (valueAST: ListValue).values.map(
7877
itemAST => valueFromAST(itemAST, itemType, variables)

0 commit comments

Comments
 (0)