Skip to content

Commit 67fc118

Browse files
committed
convert ? (flow) to Maybe (TS)
1 parent 0b24728 commit 67fc118

38 files changed

+326
-285
lines changed

src/__testUtils__/inspectStr.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { Maybe } from '../jsutils/Maybe';
2+
13
/**
24
* Special inspect function to produce readable string literal for error messages in tests
35
*/
4-
export default function inspectStr(str: ?string): string {
6+
export default function inspectStr(str: Maybe<string>): string {
57
if (str == null) {
68
return 'null';
79
}

src/error/GraphQLError.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { Source } from '../language/source';
88
import type { SourceLocation } from '../language/location';
99
import { getLocation } from '../language/location';
1010
import { printLocation, printSourceLocation } from '../language/printLocation';
11+
import { Maybe } from '../jsutils/Maybe';
1112

1213
/**
1314
* A GraphQLError describes an Error found during the parse, validate, or
@@ -67,7 +68,7 @@ export class GraphQLError extends Error {
6768
/**
6869
* The original error thrown from a field resolver during execution.
6970
*/
70-
readonly originalError: ?Error;
71+
readonly originalError: Maybe<Error>;
7172

7273
/**
7374
* Extension fields to add to the formatted error.
@@ -77,11 +78,11 @@ export class GraphQLError extends Error {
7778
constructor(
7879
message: string,
7980
nodes?: ReadonlyArray<ASTNode> | ASTNode | void | null,
80-
source?: ?Source,
81-
positions?: ?ReadonlyArray<number>,
82-
path?: ?ReadonlyArray<string | number>,
83-
originalError?: ?(Error & { readonly extensions?: unknown, ... }),
84-
extensions?: ?{ [key: string]: unknown, ... },
81+
source?: Maybe<Source>,
82+
positions?: Maybe<ReadonlyArray<number>>,
83+
path?: Maybe<ReadonlyArray<string | number>>,
84+
originalError?: Maybe<(Error & { readonly extensions?: unknown, ... })>,
85+
extensions?: Maybe<{ [key: string]: unknown, ... }>,
8586
): void {
8687
super(message);
8788

src/error/locatedError.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect from '../jsutils/inspect';
2+
import { Maybe } from '../jsutils/Maybe';
23

34
import type { ASTNode } from '../language/ast';
45

@@ -12,7 +13,7 @@ import { GraphQLError } from './GraphQLError';
1213
export function locatedError(
1314
rawOriginalError: unknown,
1415
nodes: ASTNode | ReadonlyArray<ASTNode> | void | null,
15-
path?: ?ReadonlyArray<string | number>,
16+
path?: Maybe<ReadonlyArray<string | number>>,
1617
): GraphQLError {
1718
// Sometimes a non-error is thrown, wrap it as an Error instance to ensure a consistent Error interface.
1819
const originalError: Error | GraphQLError =

src/execution/execute.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import {
6565
getArgumentValues,
6666
getDirectiveValues,
6767
} from './values';
68+
import { Maybe } from '../jsutils/Maybe';
6869

6970
/**
7071
* Terminology
@@ -126,12 +127,12 @@ export type FormattedExecutionResult = {
126127
export type ExecutionArgs = {
127128
schema: GraphQLSchema,
128129
document: DocumentNode,
129-
rootValue?: mixed,
130-
contextValue?: mixed,
131-
variableValues?: ?{ readonly [variable: string]: unknown, ... },
132-
operationName?: ?string,
133-
fieldResolver?: ?GraphQLFieldResolver<any, any>,
134-
typeResolver?: ?GraphQLTypeResolver<any, any>,
130+
rootValue?: unknown,
131+
contextValue?: unknown,
132+
variableValues?: Maybe<{ readonly [variable: string]: unknown, ... }>,
133+
operationName?: Maybe<string>,
134+
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
135+
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>,
135136
};
136137

137138
/**
@@ -229,7 +230,7 @@ function buildResponse(
229230
export function assertValidExecutionArguments(
230231
schema: GraphQLSchema,
231232
document: DocumentNode,
232-
rawVariableValues: ?{ readonly [variable: string]: unknown, ... },
233+
rawVariableValues: Maybe<{ readonly [variable: string]: unknown, ... }>,
233234
): void {
234235
devAssert(document, 'Must provide document.');
235236

@@ -256,10 +257,10 @@ export function buildExecutionContext(
256257
document: DocumentNode,
257258
rootValue: unknown,
258259
contextValue: unknown,
259-
rawVariableValues: ?{ readonly [variable: string]: unknown, ... },
260-
operationName: ?string,
261-
fieldResolver: ?GraphQLFieldResolver<unknown, unknown>,
262-
typeResolver?: ?GraphQLTypeResolver<unknown, unknown>,
260+
rawVariableValues: Maybe<{ readonly [variable: string]: unknown, ... }>,
261+
operationName: Maybe<string>,
262+
fieldResolver: Maybe<GraphQLFieldResolver<unknown, unknown>>,
263+
typeResolver?: Maybe<GraphQLTypeResolver<unknown, unknown>>,
263264
): ReadonlyArray<GraphQLError> | ExecutionContext {
264265
let operation: OperationDefinitionNode | void;
265266
const fragments: ObjMap<FragmentDefinitionNode> = Object.create(null);
@@ -1186,7 +1187,7 @@ export function getFieldDef(
11861187
schema: GraphQLSchema,
11871188
parentType: GraphQLObjectType,
11881189
fieldName: string,
1189-
): ?GraphQLField<unknown, unknown> {
1190+
): Maybe<GraphQLField<unknown, unknown>> {
11901191
if (
11911192
fieldName === SchemaMetaFieldDef.name &&
11921193
schema.getQueryType() === parentType

src/execution/values.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { isInputType, isNonNullType } from '../type/definition';
2121
import { typeFromAST } from '../utilities/typeFromAST';
2222
import { valueFromAST } from '../utilities/valueFromAST';
2323
import { coerceInputValue } from '../utilities/coerceInputValue';
24+
import { Maybe } from '../jsutils/Maybe';
2425

2526
type CoercedVariableValues =
2627
| { errors: ReadonlyArray<GraphQLError> }
@@ -244,8 +245,8 @@ export function getArgumentValues(
244245
*/
245246
export function getDirectiveValues(
246247
directiveDef: GraphQLDirective,
247-
node: { +directives?: ReadonlyArray<DirectiveNode>, ... },
248-
variableValues?: ?ObjMap<unknown>,
248+
node: { readonly directives?: ReadonlyArray<DirectiveNode>, ... },
249+
variableValues?: Maybe<ObjMap<unknown>>,
249250
): void | { [argument: string]: unknown, ... } {
250251
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
251252
const directiveNode = node.directives?.find(

src/graphql.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { validateSchema } from './type/validate';
1515

1616
import type { ExecutionResult } from './execution/execute';
1717
import { execute } from './execution/execute';
18+
import { Maybe } from './jsutils/Maybe';
1819

1920
/**
2021
* This is the primary entry point function for fulfilling GraphQL operations
@@ -60,10 +61,10 @@ export type GraphQLArgs = {
6061
source: string | Source,
6162
rootValue?: unknown,
6263
contextValue?: unknown,
63-
variableValues?: ?{ readonly [variable: string]: unknown, ... },
64-
operationName?: ?string,
65-
fieldResolver?: ?GraphQLFieldResolver<any, any>,
66-
typeResolver?: ?GraphQLTypeResolver<any, any>,
64+
variableValues?: Maybe<{ readonly [variable: string]: unknown, ... }>,
65+
operationName?: Maybe<string>,
66+
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
67+
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>,
6768
};
6869

6970
export function graphql(args: GraphQLArgs): Promise<ExecutionResult> {

src/jsutils/Path.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Maybe } from './Maybe';
2+
13
export type Path = {
24
readonly prev: Path | void,
35
readonly key: string | number,
@@ -18,7 +20,7 @@ export function addPath(
1820
/**
1921
* Given a Path, return an Array of the path keys.
2022
*/
21-
export function pathToArray(path: ?Readonly<Path>): Array<string | number> {
23+
export function pathToArray(path: Maybe<Readonly<Path>>): Array<string | number> {
2224
const flattened = [];
2325
let curr = path;
2426
while (curr) {

src/language/experimentalOnlineParser/grammar.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Maybe } from "../../jsutils/Maybe";
2+
13
export type GraphQLGrammarType = {
24
[name: string]: GraphQLGrammarRule,
35
};
@@ -16,8 +18,8 @@ export type GraphQLGrammarRule =
1618
| GraphQLGrammarConstraintsSet;
1719
export interface GraphQLGrammarBaseRuleConstraint {
1820
butNot?:
19-
| ?GraphQLGrammarTokenConstraint
20-
| ?Array<GraphQLGrammarTokenConstraint>;
21+
| Maybe<GraphQLGrammarTokenConstraint>
22+
| Maybe<Array<GraphQLGrammarTokenConstraint>>;
2123
optional?: boolean;
2224
eatNextOnFail?: boolean;
2325
}
@@ -44,8 +46,8 @@ export interface GraphQLGrammarTokenConstraint
4446
| 'String'
4547
| 'BlockString'
4648
| 'Comment';
47-
ofValue?: ?string;
48-
oneOf?: ?Array<string>;
49+
ofValue?: Maybe<string>;
50+
oneOf?: Maybe<Array<string>>;
4951
tokenName?: string;
5052
definitionName?: boolean;
5153
typeName?: boolean;

src/language/experimentalOnlineParser/onlineParser.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
GraphQLGrammarPeekConstraint,
1313
GraphQLGrammarConstraintsSet,
1414
} from './grammar';
15+
import { Maybe } from '../../jsutils/Maybe';
1516

1617
export const TokenKind = {
1718
NAME: 'Name',
@@ -86,21 +87,21 @@ export type OnlineParserState = {
8687
type Token = {
8788
kind: string,
8889
value: string,
89-
tokenName?: ?string,
90-
ruleName?: ?string,
90+
tokenName?: Maybe<string>,
91+
ruleName?: Maybe<string>,
9192
};
9293

9394
type LexerToken = {
9495
kind: string,
95-
value: ?string,
96+
value: Maybe<string>,
9697
};
9798

9899
type OnlineParserConfig = {
99100
tabSize: number,
100101
};
101102

102103
type OnlineParserConfigOption = {
103-
tabSize: ?number,
104+
tabSize: Maybe<number>,
104105
};
105106

106107
export class OnlineParser {
@@ -452,7 +453,7 @@ export class OnlineParser {
452453
return this.state.rules[this.state.rules.length - 1] || null;
453454
}
454455

455-
_popMatchedRule(token: ?Token) {
456+
_popMatchedRule(token: Maybe<Token>) {
456457
const rule = this.state.rules.pop();
457458
if (!rule) {
458459
return;

src/language/parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import { TokenKind } from './tokenKind';
5454
import { Source, isSource } from './source';
5555
import { DirectiveLocation } from './directiveLocation';
5656
import { Lexer, isPunctuatorTokenKind } from './lexer';
57+
import { Maybe } from '../jsutils/Maybe';
5758

5859
/**
5960
* Configuration options to control parser behavior
@@ -151,7 +152,7 @@ export function parseType(
151152
* @internal
152153
*/
153154
export class Parser {
154-
_options: ?ParseOptions;
155+
_options: Maybe<ParseOptions>;
155156
_lexer: Lexer;
156157

157158
constructor(source: string | Source, options?: ParseOptions) {

src/language/printer.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ASTNode } from './ast';
22

33
import { visit } from './visitor';
44
import { printBlockString } from './blockString';
5+
import { Maybe } from '../jsutils/Maybe';
56

67
/**
78
* Converts an AST into a string, using one set of reasonable
@@ -257,22 +258,22 @@ function addDescription(cb) {
257258
* Given maybeArray, print an empty string if it is null or empty, otherwise
258259
* print all items together separated by separator if provided
259260
*/
260-
function join(maybeArray: ?Array<string>, separator = ''): string {
261+
function join(maybeArray: Maybe<Array<string>>, separator = ''): string {
261262
return maybeArray?.filter((x) => x).join(separator) ?? '';
262263
}
263264

264265
/**
265266
* Given array, print each item on its own line, wrapped in an
266267
* indented "{ }" block.
267268
*/
268-
function block(array: ?Array<string>): string {
269+
function block(array: Maybe<Array<string>>): string {
269270
return wrap('{\n', indent(join(array, '\n')), '\n}');
270271
}
271272

272273
/**
273274
* If maybeString is not null or empty, then wrap with start and end, otherwise print an empty string.
274275
*/
275-
function wrap(start: string, maybeString: ?string, end: string = ''): string {
276+
function wrap(start: string, maybeString: Maybe<string>, end: string = ''): string {
276277
return maybeString != null && maybeString !== ''
277278
? start + maybeString + end
278279
: '';
@@ -286,6 +287,6 @@ function isMultiline(str: string): boolean {
286287
return str.indexOf('\n') !== -1;
287288
}
288289

289-
function hasMultilineItems(maybeArray: ?Array<string>): boolean {
290+
function hasMultilineItems(maybeArray: Maybe<Array<string>>): boolean {
290291
return maybeArray != null && maybeArray.some(isMultiline);
291292
}

src/language/visitor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect from '../jsutils/inspect';
2+
import { Maybe } from '../jsutils/Maybe';
23

34
import type { ASTNode, ASTKindToNode } from './ast';
45
import { isNode } from './ast';
@@ -405,7 +406,7 @@ export function getVisitFn(
405406
visitor: Visitor<any>,
406407
kind: string,
407408
isLeaving: boolean,
408-
): ?VisitFn<any> {
409+
): Maybe<VisitFn<any>> {
409410
const kindVisitor = visitor[kind];
410411
if (kindVisitor) {
411412
if (!isLeaving && typeof kindVisitor === 'function') {

src/subscription/subscribe.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ import type { GraphQLFieldResolver } from '../type/definition';
2424
import { getOperationRootType } from '../utilities/getOperationRootType';
2525

2626
import mapAsyncIterator from './mapAsyncIterator';
27+
import { Maybe } from '../jsutils/Maybe';
2728

2829
export type SubscriptionArgs = {
2930
schema: GraphQLSchema,
3031
document: DocumentNode,
3132
rootValue?: unknown,
3233
contextValue?: unknown,
33-
variableValues?: ?{ readonly [variable: string]: unknown, ... },
34-
operationName?: ?string,
35-
fieldResolver?: ?GraphQLFieldResolver<any, any>,
36-
subscribeFieldResolver?: ?GraphQLFieldResolver<any, any>,
34+
variableValues?: Maybe<{ readonly [variable: string]: unknown, ... }>,
35+
operationName?: Maybe<string>,
36+
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
37+
subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
3738
};
3839

3940
/**
@@ -157,9 +158,9 @@ export function createSourceEventStream(
157158
document: DocumentNode,
158159
rootValue?: unknown,
159160
contextValue?: unknown,
160-
variableValues?: ?{ readonly [variable: string]: unknown, ... },
161-
operationName?: ?string,
162-
fieldResolver?: ?GraphQLFieldResolver<any, any>,
161+
variableValues?: Maybe<{ readonly [variable: string]: unknown, ... }>,
162+
operationName?: Maybe<string>,
163+
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
163164
): Promise<AsyncIterable<unknown> | ExecutionResult> {
164165
// If arguments are missing or incorrectly typed, this is an internal
165166
// developer mistake which should throw an early error.

0 commit comments

Comments
 (0)