Skip to content

Commit 2917840

Browse files
committed
Switch to TS syntax (#3090)
1 parent daf11ba commit 2917840

File tree

123 files changed

+1936
-1841
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+1936
-1841
lines changed

src/__testUtils__/__tests__/genFuzzStrings-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { describe, it } from 'mocha';
44
import { genFuzzStrings } from '../genFuzzStrings';
55

66
function expectFuzzStrings(options: {
7-
allowedChars: Array<string>,
8-
maxLength: number,
7+
allowedChars: Array<string>;
8+
maxLength: number;
99
}) {
1010
return expect([...genFuzzStrings(options)]);
1111
}

src/__testUtils__/dedent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export function dedentString(string: string): string {
2727
* str === "{\n test\n}";
2828
*/
2929
export function dedent(
30-
strings: $ReadOnlyArray<string>,
31-
...values: $ReadOnlyArray<string>
30+
strings: ReadonlyArray<string>,
31+
...values: ReadonlyArray<string>
3232
): string {
3333
let str = strings[0];
3434

src/__testUtils__/genFuzzStrings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* Generator that produces all possible combinations of allowed characters.
33
*/
44
export function* genFuzzStrings(options: {
5-
allowedChars: Array<string>,
6-
maxLength: number,
5+
allowedChars: Array<string>;
6+
maxLength: number;
77
}): Generator<string, void, void> {
88
const { allowedChars, maxLength } = options;
99
const numAllowedChars = allowedChars.length;

src/__testUtils__/inspectStr.ts

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

src/__tests__/starWarsData.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,30 @@
22
* These are types which correspond to the schema.
33
* They represent the shape of the data visited during field resolution.
44
*/
5-
export type Character = {
6-
id: string,
7-
name: string,
8-
friends: Array<string>,
9-
appearsIn: Array<number>,
10-
...
11-
};
5+
export interface Character {
6+
id: string;
7+
name: string;
8+
friends: Array<string>;
9+
appearsIn: Array<number>;
10+
}
1211

13-
export type Human = {
14-
type: 'Human',
15-
id: string,
16-
name: string,
17-
friends: Array<string>,
18-
appearsIn: Array<number>,
19-
homePlanet?: string,
20-
};
12+
export interface Human {
13+
type: 'Human';
14+
id: string;
15+
name: string;
16+
friends: Array<string>;
17+
appearsIn: Array<number>;
18+
homePlanet?: string;
19+
}
2120

22-
export type Droid = {
23-
type: 'Droid',
24-
id: string,
25-
name: string,
26-
friends: Array<string>,
27-
appearsIn: Array<number>,
28-
primaryFunction: string,
29-
};
21+
export interface Droid {
22+
type: 'Droid';
23+
id: string;
24+
name: string;
25+
friends: Array<string>;
26+
appearsIn: Array<number>;
27+
primaryFunction: string;
28+
}
3029

3130
/**
3231
* This defines a basic set of data for our Star Wars Schema.

src/error/GraphQLError.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// flowlint uninitialized-instance-property:off
33

44
import { isObjectLike } from '../jsutils/isObjectLike';
5+
import type { Maybe } from '../jsutils/Maybe';
56

67
import type { ASTNode } from '../language/ast';
78
import type { Source } from '../language/source';
@@ -35,53 +36,53 @@ export class GraphQLError extends Error {
3536
*
3637
* Enumerable, and appears in the result of JSON.stringify().
3738
*/
38-
+locations: $ReadOnlyArray<SourceLocation> | void;
39+
readonly locations?: ReadonlyArray<SourceLocation>;
3940

4041
/**
4142
* An array describing the JSON-path into the execution response which
4243
* corresponds to this error. Only included for errors during execution.
4344
*
4445
* Enumerable, and appears in the result of JSON.stringify().
4546
*/
46-
+path: $ReadOnlyArray<string | number> | void;
47+
readonly path?: ReadonlyArray<string | number>;
4748

4849
/**
4950
* An array of GraphQL AST Nodes corresponding to this error.
5051
*/
51-
+nodes: $ReadOnlyArray<ASTNode> | void;
52+
readonly nodes?: ReadonlyArray<ASTNode>;
5253

5354
/**
5455
* The source GraphQL document for the first location of this error.
5556
*
5657
* Note that if this Error represents more than one node, the source may not
5758
* represent nodes after the first node.
5859
*/
59-
+source: Source | void;
60+
readonly source?: Source;
6061

6162
/**
6263
* An array of character offsets within the source GraphQL document
6364
* which correspond to this error.
6465
*/
65-
+positions: $ReadOnlyArray<number> | void;
66+
readonly positions?: ReadonlyArray<number>;
6667

6768
/**
6869
* The original error thrown from a field resolver during execution.
6970
*/
70-
+originalError: ?Error;
71+
readonly originalError: Maybe<Error>;
7172

7273
/**
7374
* Extension fields to add to the formatted error.
7475
*/
75-
+extensions: { [key: string]: mixed, ... } | void;
76+
readonly extensions?: { [key: string]: unknown };
7677

7778
constructor(
7879
message: string,
79-
nodes?: $ReadOnlyArray<ASTNode> | ASTNode | void | null,
80-
source?: ?Source,
81-
positions?: ?$ReadOnlyArray<number>,
82-
path?: ?$ReadOnlyArray<string | number>,
83-
originalError?: ?(Error & { +extensions?: mixed, ... }),
84-
extensions?: ?{ [key: string]: mixed, ... },
80+
nodes?: ReadonlyArray<ASTNode> | ASTNode | null,
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
) {
8687
super(message);
8788

@@ -100,8 +101,10 @@ export class GraphQLError extends Error {
100101
_source = _nodes[0].loc?.source;
101102
}
102103

103-
let _positions = positions;
104-
if (!_positions && _nodes) {
104+
let _positions;
105+
if (positions) {
106+
_positions = positions;
107+
} else if (_nodes) {
105108
_positions = [];
106109
for (const node of _nodes) {
107110
if (node.loc) {
@@ -133,7 +136,6 @@ export class GraphQLError extends Error {
133136
}
134137
}
135138

136-
// $FlowFixMe[cannot-write] FIXME
137139
Object.defineProperties(this, {
138140
name: { value: 'GraphQLError' },
139141
message: {
@@ -212,7 +214,6 @@ export class GraphQLError extends Error {
212214
}
213215

214216
// FIXME: workaround to not break chai comparisons, should be remove in v16
215-
// $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
216217
get [Symbol.toStringTag](): string {
217218
return 'Object';
218219
}

src/error/__tests__/formatError-test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { GraphQLError } from '../GraphQLError';
66

77
describe('formatError: default error formatter', () => {
88
it('uses default message', () => {
9-
// $FlowExpectedError[incompatible-call]
9+
// @ts-expect-error
1010
const e = new GraphQLError();
1111

1212
expect(formatError(e)).to.deep.equal({
@@ -45,12 +45,12 @@ describe('formatError: default error formatter', () => {
4545
});
4646

4747
it('rejects null and undefined errors', () => {
48-
// $FlowExpectedError[incompatible-call]
48+
// @ts-expect-error
4949
expect(() => formatError(undefined)).to.throw(
5050
'Received null or undefined error.',
5151
);
5252

53-
// $FlowExpectedError[incompatible-call]
53+
// @ts-expect-error
5454
expect(() => formatError(null)).to.throw(
5555
'Received null or undefined error.',
5656
);

src/error/__tests__/locatedError-test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ describe('locatedError', () => {
1818

1919
it('passes GraphQLError-ish through', () => {
2020
const e = new Error();
21-
// $FlowExpectedError[prop-missing]
21+
// @ts-expect-error
2222
e.locations = [];
23-
// $FlowExpectedError[prop-missing]
23+
// @ts-expect-error
2424
e.path = [];
25-
// $FlowExpectedError[prop-missing]
25+
// @ts-expect-error
2626
e.nodes = [];
27-
// $FlowExpectedError[prop-missing]
27+
// @ts-expect-error
2828
e.source = null;
29-
// $FlowExpectedError[prop-missing]
29+
// @ts-expect-error
3030
e.positions = [];
3131
e.name = 'GraphQLError';
3232

@@ -35,7 +35,7 @@ describe('locatedError', () => {
3535

3636
it('does not pass through elasticsearch-like errors', () => {
3737
const e = new Error('I am from elasticsearch');
38-
// $FlowExpectedError[prop-missing]
38+
// @ts-expect-error
3939
e.path = '/something/feed/_search';
4040

4141
expect(locatedError(e, [], [])).to.not.deep.equal(e);

src/error/formatError.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,28 @@ export function formatError(error: GraphQLError): GraphQLFormattedError {
2323
/**
2424
* @see https://github.com/graphql/graphql-spec/blob/master/spec/Section%207%20--%20Response.md#errors
2525
*/
26-
export type GraphQLFormattedError = {
26+
export interface GraphQLFormattedError {
2727
/**
2828
* A short, human-readable summary of the problem that **SHOULD NOT** change
2929
* from occurrence to occurrence of the problem, except for purposes of
3030
* localization.
3131
*/
32-
+message: string,
32+
readonly message: string;
3333
/**
3434
* If an error can be associated to a particular point in the requested
3535
* GraphQL document, it should contain a list of locations.
3636
*/
37-
+locations: $ReadOnlyArray<SourceLocation> | void,
37+
readonly locations?: ReadonlyArray<SourceLocation>;
3838
/**
3939
* If an error can be associated to a particular field in the GraphQL result,
4040
* it _must_ contain an entry with the key `path` that details the path of
4141
* the response field which experienced the error. This allows clients to
4242
* identify whether a null result is intentional or caused by a runtime error.
4343
*/
44-
+path: $ReadOnlyArray<string | number> | void,
44+
readonly path?: ReadonlyArray<string | number>;
4545
/**
4646
* Reserved for implementors to extend the protocol however they see fit,
4747
* and hence there are no additional restrictions on its contents.
4848
*/
49-
+extensions?: { [key: string]: mixed, ... },
50-
};
49+
readonly extensions?: { [key: string]: unknown };
50+
}

src/error/locatedError.ts

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

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

@@ -10,9 +11,9 @@ import { GraphQLError } from './GraphQLError';
1011
* document responsible for the original Error.
1112
*/
1213
export function locatedError(
13-
rawOriginalError: mixed,
14-
nodes: ASTNode | $ReadOnlyArray<ASTNode> | void | null,
15-
path?: ?$ReadOnlyArray<string | number>,
14+
rawOriginalError: unknown,
15+
nodes: ASTNode | ReadonlyArray<ASTNode> | undefined | null,
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 =
@@ -22,17 +23,17 @@ export function locatedError(
2223

2324
// Note: this uses a brand-check to support GraphQL errors originating from other contexts.
2425
if (Array.isArray(originalError.path)) {
25-
// $FlowExpectedError[incompatible-return]
26+
// @ts-expect-error
2627
return originalError;
2728
}
2829

2930
return new GraphQLError(
3031
originalError.message,
31-
// $FlowFixMe[prop-missing] FIXME
32+
// @ts-expect-error FIXME
3233
originalError.nodes ?? nodes,
33-
// $FlowFixMe[prop-missing] FIXME
34+
// @ts-expect-error FIXME
3435
originalError.source,
35-
// $FlowFixMe[prop-missing] FIXME
36+
// @ts-expect-error FIXME
3637
originalError.positions,
3738
path,
3839
originalError,

src/execution/__tests__/abstract-test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import { buildSchema } from '../../utilities/buildASTSchema';
1818
import { executeSync, execute } from '../execute';
1919

2020
async function executeQuery(args: {
21-
schema: GraphQLSchema,
22-
query: string,
23-
rootValue?: mixed,
21+
schema: GraphQLSchema;
22+
query: string;
23+
rootValue?: unknown;
2424
}) {
2525
const { schema, query, rootValue } = args;
2626
const document = parse(query);
@@ -534,7 +534,7 @@ describe('Execute: Handles execution of abstract types', () => {
534534
}
535535
`);
536536

537-
function expectError({ forTypeName }: { forTypeName: mixed }) {
537+
function expectError({ forTypeName }: { forTypeName: unknown }) {
538538
const rootValue = { pet: { __typename: forTypeName } };
539539
const result = executeSync({ schema, document, rootValue });
540540
return {
@@ -570,15 +570,15 @@ describe('Execute: Handles execution of abstract types', () => {
570570
);
571571

572572
// FIXME: workaround since we can't inject resolveType into SDL
573-
// $FlowExpectedError[incompatible-type]
573+
// @ts-expect-error
574574
assertInterfaceType(schema.getType('Pet')).resolveType = () => [];
575575
expectError({ forTypeName: undefined }).toEqual(
576576
'Abstract type "Pet" must resolve to an Object type at runtime for field "Query.pet" with value { __typename: undefined }, received "[]".',
577577
);
578578

579579
// FIXME: workaround since we can't inject resolveType into SDL
580580
assertInterfaceType(schema.getType('Pet')).resolveType =
581-
// $FlowExpectedError[incompatible-type]
581+
// @ts-expect-error
582582
() => schema.getType('Cat');
583583
expectError({ forTypeName: undefined }).toEqual(
584584
'Support for returning GraphQLObjectType from resolveType was removed in [email protected] please return type name instead.',

0 commit comments

Comments
 (0)