Skip to content

Commit

Permalink
Switch to TS syntax (#3090)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed May 25, 2021
1 parent daf11ba commit 2917840
Show file tree
Hide file tree
Showing 123 changed files with 1,936 additions and 1,841 deletions.
4 changes: 2 additions & 2 deletions src/__testUtils__/__tests__/genFuzzStrings-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { describe, it } from 'mocha';
import { genFuzzStrings } from '../genFuzzStrings';

function expectFuzzStrings(options: {
allowedChars: Array<string>,
maxLength: number,
allowedChars: Array<string>;
maxLength: number;
}) {
return expect([...genFuzzStrings(options)]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/__testUtils__/dedent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export function dedentString(string: string): string {
* str === "{\n test\n}";
*/
export function dedent(
strings: $ReadOnlyArray<string>,
...values: $ReadOnlyArray<string>
strings: ReadonlyArray<string>,
...values: ReadonlyArray<string>
): string {
let str = strings[0];

Expand Down
4 changes: 2 additions & 2 deletions src/__testUtils__/genFuzzStrings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Generator that produces all possible combinations of allowed characters.
*/
export function* genFuzzStrings(options: {
allowedChars: Array<string>,
maxLength: number,
allowedChars: Array<string>;
maxLength: number;
}): Generator<string, void, void> {
const { allowedChars, maxLength } = options;
const numAllowedChars = allowedChars.length;
Expand Down
4 changes: 3 additions & 1 deletion src/__testUtils__/inspectStr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { Maybe } from '../jsutils/Maybe';

/**
* Special inspect function to produce readable string literal for error messages in tests
*/
export function inspectStr(str: ?string): string {
export function inspectStr(str: Maybe<string>): string {
if (str == null) {
return 'null';
}
Expand Down
45 changes: 22 additions & 23 deletions src/__tests__/starWarsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,30 @@
* These are types which correspond to the schema.
* They represent the shape of the data visited during field resolution.
*/
export type Character = {
id: string,
name: string,
friends: Array<string>,
appearsIn: Array<number>,
...
};
export interface Character {
id: string;
name: string;
friends: Array<string>;
appearsIn: Array<number>;
}

export type Human = {
type: 'Human',
id: string,
name: string,
friends: Array<string>,
appearsIn: Array<number>,
homePlanet?: string,
};
export interface Human {
type: 'Human';
id: string;
name: string;
friends: Array<string>;
appearsIn: Array<number>;
homePlanet?: string;
}

export type Droid = {
type: 'Droid',
id: string,
name: string,
friends: Array<string>,
appearsIn: Array<number>,
primaryFunction: string,
};
export interface Droid {
type: 'Droid';
id: string;
name: string;
friends: Array<string>;
appearsIn: Array<number>;
primaryFunction: string;
}

/**
* This defines a basic set of data for our Star Wars Schema.
Expand Down
35 changes: 18 additions & 17 deletions src/error/GraphQLError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// flowlint uninitialized-instance-property:off

import { isObjectLike } from '../jsutils/isObjectLike';
import type { Maybe } from '../jsutils/Maybe';

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

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

/**
* An array of GraphQL AST Nodes corresponding to this error.
*/
+nodes: $ReadOnlyArray<ASTNode> | void;
readonly nodes?: ReadonlyArray<ASTNode>;

/**
* The source GraphQL document for the first location of this error.
*
* Note that if this Error represents more than one node, the source may not
* represent nodes after the first node.
*/
+source: Source | void;
readonly source?: Source;

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

/**
* The original error thrown from a field resolver during execution.
*/
+originalError: ?Error;
readonly originalError: Maybe<Error>;

/**
* Extension fields to add to the formatted error.
*/
+extensions: { [key: string]: mixed, ... } | void;
readonly extensions?: { [key: string]: unknown };

constructor(
message: string,
nodes?: $ReadOnlyArray<ASTNode> | ASTNode | void | null,
source?: ?Source,
positions?: ?$ReadOnlyArray<number>,
path?: ?$ReadOnlyArray<string | number>,
originalError?: ?(Error & { +extensions?: mixed, ... }),
extensions?: ?{ [key: string]: mixed, ... },
nodes?: ReadonlyArray<ASTNode> | ASTNode | null,
source?: Maybe<Source>,
positions?: Maybe<ReadonlyArray<number>>,
path?: Maybe<ReadonlyArray<string | number>>,
originalError?: Maybe<Error & { readonly extensions?: unknown }>,
extensions?: Maybe<{ [key: string]: unknown }>,
) {
super(message);

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

let _positions = positions;
if (!_positions && _nodes) {
let _positions;
if (positions) {
_positions = positions;
} else if (_nodes) {
_positions = [];
for (const node of _nodes) {
if (node.loc) {
Expand Down Expand Up @@ -133,7 +136,6 @@ export class GraphQLError extends Error {
}
}

// $FlowFixMe[cannot-write] FIXME
Object.defineProperties(this, {
name: { value: 'GraphQLError' },
message: {
Expand Down Expand Up @@ -212,7 +214,6 @@ export class GraphQLError extends Error {
}

// FIXME: workaround to not break chai comparisons, should be remove in v16
// $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
get [Symbol.toStringTag](): string {
return 'Object';
}
Expand Down
6 changes: 3 additions & 3 deletions src/error/__tests__/formatError-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GraphQLError } from '../GraphQLError';

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

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

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

// $FlowExpectedError[incompatible-call]
// @ts-expect-error
expect(() => formatError(null)).to.throw(
'Received null or undefined error.',
);
Expand Down
12 changes: 6 additions & 6 deletions src/error/__tests__/locatedError-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ describe('locatedError', () => {

it('passes GraphQLError-ish through', () => {
const e = new Error();
// $FlowExpectedError[prop-missing]
// @ts-expect-error
e.locations = [];
// $FlowExpectedError[prop-missing]
// @ts-expect-error
e.path = [];
// $FlowExpectedError[prop-missing]
// @ts-expect-error
e.nodes = [];
// $FlowExpectedError[prop-missing]
// @ts-expect-error
e.source = null;
// $FlowExpectedError[prop-missing]
// @ts-expect-error
e.positions = [];
e.name = 'GraphQLError';

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

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

expect(locatedError(e, [], [])).to.not.deep.equal(e);
Expand Down
12 changes: 6 additions & 6 deletions src/error/formatError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ export function formatError(error: GraphQLError): GraphQLFormattedError {
/**
* @see https://github.com/graphql/graphql-spec/blob/master/spec/Section%207%20--%20Response.md#errors
*/
export type GraphQLFormattedError = {
export interface GraphQLFormattedError {
/**
* A short, human-readable summary of the problem that **SHOULD NOT** change
* from occurrence to occurrence of the problem, except for purposes of
* localization.
*/
+message: string,
readonly message: string;
/**
* If an error can be associated to a particular point in the requested
* GraphQL document, it should contain a list of locations.
*/
+locations: $ReadOnlyArray<SourceLocation> | void,
readonly locations?: ReadonlyArray<SourceLocation>;
/**
* If an error can be associated to a particular field in the GraphQL result,
* it _must_ contain an entry with the key `path` that details the path of
* the response field which experienced the error. This allows clients to
* identify whether a null result is intentional or caused by a runtime error.
*/
+path: $ReadOnlyArray<string | number> | void,
readonly path?: ReadonlyArray<string | number>;
/**
* Reserved for implementors to extend the protocol however they see fit,
* and hence there are no additional restrictions on its contents.
*/
+extensions?: { [key: string]: mixed, ... },
};
readonly extensions?: { [key: string]: unknown };
}
15 changes: 8 additions & 7 deletions src/error/locatedError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { inspect } from '../jsutils/inspect';
import type { Maybe } from '../jsutils/Maybe';

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

Expand All @@ -10,9 +11,9 @@ import { GraphQLError } from './GraphQLError';
* document responsible for the original Error.
*/
export function locatedError(
rawOriginalError: mixed,
nodes: ASTNode | $ReadOnlyArray<ASTNode> | void | null,
path?: ?$ReadOnlyArray<string | number>,
rawOriginalError: unknown,
nodes: ASTNode | ReadonlyArray<ASTNode> | undefined | null,
path?: Maybe<ReadonlyArray<string | number>>,
): GraphQLError {
// Sometimes a non-error is thrown, wrap it as an Error instance to ensure a consistent Error interface.
const originalError: Error | GraphQLError =
Expand All @@ -22,17 +23,17 @@ export function locatedError(

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

return new GraphQLError(
originalError.message,
// $FlowFixMe[prop-missing] FIXME
// @ts-expect-error FIXME
originalError.nodes ?? nodes,
// $FlowFixMe[prop-missing] FIXME
// @ts-expect-error FIXME
originalError.source,
// $FlowFixMe[prop-missing] FIXME
// @ts-expect-error FIXME
originalError.positions,
path,
originalError,
Expand Down
12 changes: 6 additions & 6 deletions src/execution/__tests__/abstract-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import { buildSchema } from '../../utilities/buildASTSchema';
import { executeSync, execute } from '../execute';

async function executeQuery(args: {
schema: GraphQLSchema,
query: string,
rootValue?: mixed,
schema: GraphQLSchema;
query: string;
rootValue?: unknown;
}) {
const { schema, query, rootValue } = args;
const document = parse(query);
Expand Down Expand Up @@ -534,7 +534,7 @@ describe('Execute: Handles execution of abstract types', () => {
}
`);

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

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

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

0 comments on commit 2917840

Please sign in to comment.