Skip to content

Commit 9a9de3e

Browse files
authored
fix flow errors by annotating null prototypes (#1048)
1 parent a501a9c commit 9a9de3e

File tree

15 files changed

+79
-67
lines changed

15 files changed

+79
-67
lines changed

.flowconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
experimental.const_params=true
1313

1414
[version]
15-
^0.54.0
15+
^0.56.0

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"eslint": "4.4.1",
5757
"eslint-plugin-babel": "4.1.2",
5858
"eslint-plugin-flowtype": "2.35.0",
59-
"flow-bin": "^0.54.0",
59+
"flow-bin": "0.56.0",
6060
"isparta": "4.0.0",
6161
"mocha": "3.5.0",
6262
"sane": "2.0.0"

src/execution/execute.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
*/
99

1010
import { forEach, isCollection } from 'iterall';
11-
1211
import { GraphQLError, locatedError } from '../error';
1312
import invariant from '../jsutils/invariant';
1413
import isNullish from '../jsutils/isNullish';
14+
import type {ObjMap} from '../jsutils/ObjMap';
15+
1516
import { typeFromAST } from '../utilities/typeFromAST';
1617
import * as Kind from '../language/kinds';
1718
import {
@@ -84,11 +85,11 @@ import type {
8485
*/
8586
export type ExecutionContext = {
8687
schema: GraphQLSchema;
87-
fragments: {[key: string]: FragmentDefinitionNode};
88+
fragments: ObjMap<FragmentDefinitionNode>;
8889
rootValue: mixed;
8990
contextValue: mixed;
9091
operation: OperationDefinitionNode;
91-
variableValues: {[key: string]: mixed};
92+
variableValues: ObjMap<mixed>,
9293
fieldResolver: GraphQLFieldResolver<any, any>;
9394
errors: Array<GraphQLError>;
9495
};
@@ -278,14 +279,13 @@ export function buildExecutionContext(
278279
document: DocumentNode,
279280
rootValue: mixed,
280281
contextValue: mixed,
281-
rawVariableValues: ?{[key: string]: mixed},
282+
rawVariableValues: ?ObjMap<mixed>,
282283
operationName: ?string,
283284
fieldResolver: ?GraphQLFieldResolver<any, any>
284285
): ExecutionContext {
285286
const errors: Array<GraphQLError> = [];
286287
let operation: ?OperationDefinitionNode;
287-
const fragments: {[name: string]: FragmentDefinitionNode} =
288-
Object.create(null);
288+
const fragments: ObjMap<FragmentDefinitionNode> = Object.create(null);
289289
document.definitions.forEach(definition => {
290290
switch (definition.kind) {
291291
case Kind.OPERATION_DEFINITION:
@@ -340,7 +340,7 @@ function executeOperation(
340340
exeContext: ExecutionContext,
341341
operation: OperationDefinitionNode,
342342
rootValue: mixed
343-
): ?{[key: string]: mixed} {
343+
): ?(Promise<?ObjMap<mixed>> | ObjMap<mixed>) {
344344
const type = getOperationRootType(exeContext.schema, operation);
345345
const fields = collectFields(
346346
exeContext,
@@ -420,8 +420,8 @@ function executeFieldsSerially(
420420
parentType: GraphQLObjectType,
421421
sourceValue: mixed,
422422
path: ResponsePath,
423-
fields: {[key: string]: Array<FieldNode>}
424-
): Promise<{[key: string]: mixed}> {
423+
fields: ObjMap<Array<FieldNode>>,
424+
): Promise<ObjMap<mixed>> {
425425
return Object.keys(fields).reduce(
426426
(prevPromise, responseName) => prevPromise.then(results => {
427427
const fieldNodes = fields[responseName];
@@ -459,8 +459,8 @@ function executeFields(
459459
parentType: GraphQLObjectType,
460460
sourceValue: mixed,
461461
path: ResponsePath,
462-
fields: {[key: string]: Array<FieldNode>}
463-
): {[key: string]: mixed} {
462+
fields: ObjMap<Array<FieldNode>>,
463+
): Promise<ObjMap<mixed>> | ObjMap<mixed> {
464464
let containsPromise = false;
465465

466466
const finalResults = Object.keys(fields).reduce(
@@ -510,9 +510,9 @@ export function collectFields(
510510
exeContext: ExecutionContext,
511511
runtimeType: GraphQLObjectType,
512512
selectionSet: SelectionSetNode,
513-
fields: {[key: string]: Array<FieldNode>},
514-
visitedFragmentNames: {[key: string]: boolean}
515-
): {[key: string]: Array<FieldNode>} {
513+
fields: ObjMap<Array<FieldNode>>,
514+
visitedFragmentNames: ObjMap<boolean>,
515+
): ObjMap<Array<FieldNode>> {
516516
for (let i = 0; i < selectionSet.selections.length; i++) {
517517
const selection = selectionSet.selections[i];
518518
switch (selection.kind) {
@@ -621,9 +621,7 @@ function doesFragmentConditionMatch(
621621
* This is akin to bluebird's `Promise.props`, but implemented only using
622622
* `Promise.all` so it will work with any implementation of ES6 promises.
623623
*/
624-
function promiseForObject<T>(
625-
object: {[key: string]: Promise<T>}
626-
): Promise<{[key: string]: T}> {
624+
function promiseForObject<T>(object: ObjMap<Promise<T>>): Promise<ObjMap<T>> {
627625
const keys = Object.keys(object);
628626
const valuesAndPromises = keys.map(name => object[name]);
629627
return Promise.all(valuesAndPromises).then(

src/execution/values.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ import type {
5151
export function getVariableValues(
5252
schema: GraphQLSchema,
5353
varDefNodes: Array<VariableDefinitionNode>,
54-
inputs: { [key: string]: mixed }
55-
): { [key: string]: mixed } {
54+
inputs: { [key: string]: mixed, __proto__: null }
55+
): { [key: string]: mixed, __proto__: null } {
5656
const coercedValues = Object.create(null);
5757
for (let i = 0; i < varDefNodes.length; i++) {
5858
const varDefNode = varDefNodes[i];
@@ -105,8 +105,8 @@ export function getVariableValues(
105105
export function getArgumentValues(
106106
def: GraphQLField<*, *> | GraphQLDirective,
107107
node: FieldNode | DirectiveNode,
108-
variableValues?: ?{ [key: string]: mixed }
109-
): { [key: string]: mixed } {
108+
variableValues?: ?{ [key: string]: mixed, __proto__: null }
109+
): { [key: string]: mixed, __proto__: null } {
110110
const argDefs = def.args;
111111
const argNodes = node.arguments;
112112
if (!argDefs || !argNodes) {
@@ -174,8 +174,8 @@ export function getArgumentValues(
174174
export function getDirectiveValues(
175175
directiveDef: GraphQLDirective,
176176
node: { directives?: ?Array<DirectiveNode> },
177-
variableValues?: ?{ [key: string]: mixed }
178-
): void | { [key: string]: mixed } {
177+
variableValues?: ?{ [key: string]: mixed, __proto__: null }
178+
): void | { [key: string]: mixed, __proto__: null } {
179179
const directiveNode = node.directives && find(
180180
node.directives,
181181
directive => directive.name.value === directiveDef.name

src/jsutils/ObjMap.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
export type ObjMap<T> = { [key: string]: T, __proto__: null };

src/jsutils/keyMap.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @flow
88
*/
99

10+
import type {ObjMap} from './ObjMap';
11+
1012
/**
1113
* Creates a keyed JS object from an array, given a function to produce the keys
1214
* for each value in the array.
@@ -33,7 +35,7 @@
3335
export default function keyMap<T>(
3436
list: Array<T>,
3537
keyFn: (item: T) => string
36-
): {[key: string]: T} {
38+
): ObjMap<T> {
3739
return list.reduce(
3840
(map, item) => ((map[keyFn(item)] = item), map),
3941
Object.create(null)

src/jsutils/keyValMap.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @flow
88
*/
99

10+
import type {ObjMap} from './ObjMap';
11+
1012
/**
1113
* Creates a keyed JS object from an array, given a function to produce the keys
1214
* and a function to produce the values from each item in the array.
@@ -28,7 +30,7 @@ export default function keyValMap<T, V>(
2830
list: Array<T>,
2931
keyFn: (item: T) => string,
3032
valFn: (item: T) => V
31-
): {[key: string]: V} {
33+
): ObjMap<V> {
3234
return list.reduce(
3335
(map, item) => ((map[keyFn(item)] = valFn(item)), map),
3436
Object.create(null)

src/type/definition.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import invariant from '../jsutils/invariant';
1111
import isNullish from '../jsutils/isNullish';
12+
import type {ObjMap} from '../jsutils/ObjMap';
1213
import * as Kind from '../language/kinds';
1314
import { assertValidName } from '../utilities/assertValidName';
1415
import type {
@@ -624,7 +625,7 @@ export type GraphQLIsTypeOfFn<TSource, TContext> = (
624625

625626
export type GraphQLFieldResolver<TSource, TContext> = (
626627
source: TSource,
627-
args: { [argName: string]: any },
628+
args: ObjMap<any>,
628629
context: TContext,
629630
info: GraphQLResolveInfo
630631
) => mixed;
@@ -636,10 +637,10 @@ export type GraphQLResolveInfo = {
636637
parentType: GraphQLCompositeType;
637638
path: ResponsePath;
638639
schema: GraphQLSchema;
639-
fragments: { [fragmentName: string]: FragmentDefinitionNode };
640+
fragments: ObjMap<FragmentDefinitionNode>;
640641
rootValue: mixed;
641642
operation: OperationDefinitionNode;
642-
variableValues: { [variableName: string]: mixed };
643+
variableValues: ObjMap<mixed>;
643644
};
644645

645646
export type ResponsePath = { prev: ResponsePath, key: string | number } | void;
@@ -654,9 +655,7 @@ export type GraphQLFieldConfig<TSource, TContext> = {
654655
astNode?: ?FieldDefinitionNode;
655656
};
656657

657-
export type GraphQLFieldConfigArgumentMap = {
658-
[argName: string]: GraphQLArgumentConfig;
659-
};
658+
export type GraphQLFieldConfigArgumentMap = ObjMap<GraphQLArgumentConfig>;
660659

661660
export type GraphQLArgumentConfig = {
662661
type: GraphQLInputType;
@@ -665,9 +664,8 @@ export type GraphQLArgumentConfig = {
665664
astNode?: ?InputValueDefinitionNode;
666665
};
667666

668-
export type GraphQLFieldConfigMap<TSource, TContext> = {
669-
[fieldName: string]: GraphQLFieldConfig<TSource, TContext>;
670-
};
667+
export type GraphQLFieldConfigMap<TSource, TContext> =
668+
ObjMap<GraphQLFieldConfig<TSource, TContext>>;
671669

672670
export type GraphQLField<TSource, TContext> = {
673671
name: string;
@@ -689,9 +687,8 @@ export type GraphQLArgument = {
689687
astNode?: ?InputValueDefinitionNode;
690688
};
691689

692-
export type GraphQLFieldMap<TSource, TContext> = {
693-
[fieldName: string]: GraphQLField<TSource, TContext>;
694-
};
690+
export type GraphQLFieldMap<TSource, TContext> =
691+
ObjMap<GraphQLField<TSource, TContext>>;
695692

696693

697694

@@ -918,7 +915,7 @@ export class GraphQLEnumType/* <T> */ {
918915
_enumConfig: GraphQLEnumTypeConfig/* <T> */;
919916
_values: Array<GraphQLEnumValue/* <T> */>;
920917
_valueLookup: Map<any/* T */, GraphQLEnumValue>;
921-
_nameLookup: { [valueName: string]: GraphQLEnumValue };
918+
_nameLookup: ObjMap<GraphQLEnumValue>;
922919

923920
constructor(config: GraphQLEnumTypeConfig/* <T> */): void {
924921
this.name = config.name;
@@ -981,7 +978,7 @@ export class GraphQLEnumType/* <T> */ {
981978
return this._valueLookup;
982979
}
983980

984-
_getNameLookup(): { [valueName: string]: GraphQLEnumValue } {
981+
_getNameLookup(): ObjMap<GraphQLEnumValue> {
985982
if (!this._nameLookup) {
986983
const lookup = Object.create(null);
987984
this.getValues().forEach(value => {
@@ -1055,9 +1052,8 @@ export type GraphQLEnumTypeConfig/* <T> */ = {
10551052
isIntrospection?: boolean;
10561053
};
10571054

1058-
export type GraphQLEnumValueConfigMap/* <T> */ = {
1059-
[valueName: string]: GraphQLEnumValueConfig/* <T> */;
1060-
};
1055+
export type GraphQLEnumValueConfigMap/* <T> */ =
1056+
ObjMap<GraphQLEnumValueConfig/* <T> */>;
10611057

10621058
export type GraphQLEnumValueConfig/* <T> */ = {
10631059
value?: any/* T */;
@@ -1179,9 +1175,8 @@ export type GraphQLInputFieldConfig = {
11791175
astNode?: ?InputValueDefinitionNode;
11801176
};
11811177

1182-
export type GraphQLInputFieldConfigMap = {
1183-
[fieldName: string]: GraphQLInputFieldConfig;
1184-
};
1178+
export type GraphQLInputFieldConfigMap =
1179+
ObjMap<GraphQLInputFieldConfig>;
11851180

11861181
export type GraphQLInputField = {
11871182
name: string;
@@ -1191,9 +1186,8 @@ export type GraphQLInputField = {
11911186
astNode?: ?InputValueDefinitionNode;
11921187
};
11931188

1194-
export type GraphQLInputFieldMap = {
1195-
[fieldName: string]: GraphQLInputField;
1196-
};
1189+
export type GraphQLInputFieldMap =
1190+
ObjMap<GraphQLInputField>;
11971191

11981192

11991193

src/type/schema.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { GraphQLDirective, specifiedDirectives } from './directives';
2525
import { __Schema } from './introspection';
2626
import find from '../jsutils/find';
2727
import invariant from '../jsutils/invariant';
28+
import type {ObjMap} from '../jsutils/ObjMap';
2829
import { isEqualType, isTypeSubTypeOf } from '../utilities/typeComparators';
2930

3031

@@ -61,10 +62,8 @@ export class GraphQLSchema {
6162
_subscriptionType: ?GraphQLObjectType;
6263
_directives: Array<GraphQLDirective>;
6364
_typeMap: TypeMap;
64-
_implementations: { [interfaceName: string]: Array<GraphQLObjectType> };
65-
_possibleTypeMap: ?{
66-
[abstractName: string]: { [possibleName: string]: boolean }
67-
};
65+
_implementations: ObjMap<Array<GraphQLObjectType>>;
66+
_possibleTypeMap: ?ObjMap<{[possibleName: string]: boolean}>;
6867

6968
constructor(config: GraphQLSchemaConfig): void {
7069
invariant(
@@ -221,7 +220,7 @@ export class GraphQLSchema {
221220
}
222221
}
223222

224-
type TypeMap = { [typeName: string]: GraphQLNamedType };
223+
type TypeMap = ObjMap<GraphQLNamedType>;
225224

226225
type GraphQLSchemaConfig = {
227226
query: GraphQLObjectType;

src/utilities/buildASTSchema.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import invariant from '../jsutils/invariant';
1111
import keyValMap from '../jsutils/keyValMap';
12+
import type {ObjMap} from '../jsutils/ObjMap';
1213
import { valueFromAST } from './valueFromAST';
1314
import { TokenKind } from '../language/lexer';
1415
import { parse } from '../language/parser';
@@ -133,7 +134,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
133134
let schemaDef: ?SchemaDefinitionNode;
134135

135136
const typeDefs: Array<TypeDefinitionNode> = [];
136-
const nodeMap: {[name: string]: TypeDefinitionNode} = Object.create(null);
137+
const nodeMap: ObjMap<TypeDefinitionNode> = Object.create(null);
137138
const directiveDefs: Array<DirectiveDefinitionNode> = [];
138139
for (let i = 0; i < ast.definitions.length; i++) {
139140
const d = ast.definitions[i];

0 commit comments

Comments
 (0)