Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.

Commit 61992cf

Browse files
committed
[TypeScriptGenerator] Fix type issues.
1 parent 56c2ad2 commit 61992cf

File tree

4 files changed

+38
-210
lines changed

4 files changed

+38
-210
lines changed

src/TypeScriptGenerator.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,18 @@ function aggregateRuntimeImports(ast: ts.Statement[]) {
7171

7272
const runtimeImports = importNodes.filter(
7373
importDeclaration =>
74-
// @ts-ignore
75-
importDeclaration.moduleSpecifier.text === "relay-runtime"
74+
(importDeclaration.moduleSpecifier as ts.StringLiteral).text ===
75+
"relay-runtime"
7676
);
7777

7878
if (runtimeImports.length > 1) {
7979
const namedImports: string[] = [];
8080
runtimeImports.map(node => {
81-
// @ts-ignore
82-
node.importClause.namedBindings.elements.map(element => {
83-
namedImports.push(element.name.escapedText);
84-
});
81+
(node.importClause!.namedBindings! as ts.NamedImports).elements.map(
82+
element => {
83+
namedImports.push(element.name.text);
84+
}
85+
);
8586
});
8687

8788
const importSpecifiers: ts.ImportSpecifier[] = [];
@@ -701,8 +702,10 @@ function makeRawResponseProp(
701702
) {
702703
if (kind === "ModuleImport") {
703704
// TODO: In flow one can extend an object type with spread, with TS we need an intersection (&)
704-
return ts.createSpread(ts.createIdentifier(key));
705-
// throw new Error("TODO!");
705+
// return ts.createSpread(ts.createIdentifier(key));
706+
throw new Error(
707+
"relay-compiler-language-typescript does not support @module yet"
708+
);
706709
}
707710
if (schemaName === "__typename" && concreteType) {
708711
value = ts.createLiteralTypeNode(ts.createLiteral(concreteType));
@@ -774,7 +777,7 @@ function selectionsToRawResponseBabel(
774777
}
775778
});
776779

777-
const types: ts.PropertySignature[][] = [];
780+
const types: ts.TypeNode[] = [];
778781

779782
if (Object.keys(byConcreteType).length) {
780783
const baseFieldsMap = selectionsToMap(baseFields);
@@ -788,31 +791,29 @@ function selectionsToRawResponseBabel(
788791
);
789792
types.push(
790793
exactObjectTypeAnnotation(
791-
// @ts-ignore
792794
mergedSeletions.map(selection =>
793795
makeRawResponseProp(schema, selection, state, concreteType)
794796
)
795-
) as any
797+
)
796798
);
797799
appendLocal3DPayload(types, mergedSeletions, schema, state, concreteType);
798800
}
799801
}
800802
if (baseFields.length > 0) {
801803
types.push(
802804
exactObjectTypeAnnotation(
803-
// @ts-ignore
804805
baseFields.map(selection =>
805806
makeRawResponseProp(schema, selection, state, nodeTypeName)
806807
)
807-
) as any
808+
)
808809
);
809810
appendLocal3DPayload(types, baseFields, schema, state, nodeTypeName);
810811
}
811-
return ts.createUnionTypeNode(types as any);
812+
return ts.createUnionTypeNode(types);
812813
}
813814

814815
function appendLocal3DPayload(
815-
types: any[],
816+
types: ts.TypeNode[],
816817
selections: ReadonlyArray<Selection>,
817818
schema: Schema,
818819
state: State,
@@ -824,9 +825,8 @@ function appendLocal3DPayload(
824825
state.runtimeImports.add("Local3DPayload");
825826
types.push(
826827
ts.createTypeReferenceNode(ts.createIdentifier("Local3DPayload"), [
827-
stringLiteralTypeAnnotation(moduleImport.documentName as any),
828+
stringLiteralTypeAnnotation(moduleImport.documentName!),
828829
exactObjectTypeAnnotation(
829-
// @ts-ignore
830830
selections
831831
.filter(sel => sel.schemaName !== "js")
832832
.map(selection =>
@@ -848,7 +848,6 @@ function createRawResponseTypeVisitor(
848848
Root(node) {
849849
return exportType(
850850
`${node.name}RawResponse`,
851-
// @ts-ignore
852851
selectionsToRawResponseBabel(
853852
schema,
854853
/* $FlowFixMe: selections have already been transformed */
@@ -918,7 +917,6 @@ function createRawResponseTypeVisitor(
918917
}
919918

920919
// Dedupe the generated type of module selections to reduce file size
921-
// @ts-ignore
922920
function visitRawResponseModuleImport(
923921
schema: Schema,
924922
node: any,
@@ -940,7 +938,7 @@ function visitRawResponseModuleImport(
940938
null
941939
);
942940

943-
state.matchFields.set(key, ast as any);
941+
state.matchFields.set(key, ast);
944942
}
945943

946944
return [

test/__snapshots__/TypeScriptGenerator-test.ts.snap

Lines changed: 0 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,97 +1282,6 @@ export type ScalarHandleField = {
12821282
12831283
`;
12841284
1285-
exports[`Snapshot tests TypeScriptGenerator with a single artifact directory matches expected output: query-with-module-field.graphql 1`] = `
1286-
~~~~~~~~~~ INPUT ~~~~~~~~~~
1287-
query Test @raw_response_type {
1288-
node(id: "1") {
1289-
...Test_user
1290-
}
1291-
}
1292-
1293-
fragment Test_user on User {
1294-
plainUserRenderer {
1295-
...Test_userRenderer @module(name: "Renderer.react")
1296-
}
1297-
}
1298-
1299-
fragment Test_userRenderer on PlainUserRenderer {
1300-
user {
1301-
username
1302-
}
1303-
}
1304-
1305-
~~~~~~~~~~ OUTPUT ~~~~~~~~~~
1306-
// Test.graphql
1307-
import { Local3DPayload, FragmentRefs } from "relay-runtime";
1308-
export type TestVariables = {};
1309-
export type TestResponse = {
1310-
readonly node: {
1311-
readonly " $fragmentRefs": FragmentRefs<"Test_user">;
1312-
} | null;
1313-
};
1314-
export type Test_userRenderer = {
1315-
readonly user: ({
1316-
readonly username: string | null;
1317-
readonly id: string | null;
1318-
}) | null;
1319-
};
1320-
export type TestRawResponse = {
1321-
readonly node: ({
1322-
readonly __typename: "User";
1323-
readonly id: string | null;
1324-
readonly plainUserRenderer: ({
1325-
readonly __module_operation_Test_user: unknown | null;
1326-
readonly __module_component_Test_user: unknown | null;
1327-
...Test_userRenderer
1328-
} | Local3DPayload<"Test_user", {
1329-
...Test_userRenderer
1330-
}>) | null;
1331-
} | {
1332-
readonly __typename: string | null;
1333-
readonly id: string | null;
1334-
}) | null;
1335-
};
1336-
export type Test = {
1337-
readonly response: TestResponse;
1338-
readonly variables: TestVariables;
1339-
readonly rawResponse: TestRawResponse;
1340-
};
1341-
1342-
1343-
// Test_user.graphql
1344-
import { FragmentRefs } from "relay-runtime";
1345-
export type Test_user = {
1346-
readonly plainUserRenderer: {
1347-
readonly __fragmentPropName?: string | null;
1348-
readonly __module_component?: string | null;
1349-
readonly " $fragmentRefs": FragmentRefs<"Test_userRenderer">;
1350-
} | null;
1351-
readonly " $refType": "Test_user";
1352-
};
1353-
export type Test_user$data = Test_user;
1354-
export type Test_user$key = {
1355-
readonly " $data"?: Test_user$data;
1356-
readonly " $fragmentRefs": FragmentRefs<"Test_user">;
1357-
};
1358-
1359-
1360-
// Test_userRenderer.graphql
1361-
import { FragmentRefs } from "relay-runtime";
1362-
export type Test_userRenderer = {
1363-
readonly user: {
1364-
readonly username: string | null;
1365-
} | null;
1366-
readonly " $refType": "Test_userRenderer";
1367-
};
1368-
export type Test_userRenderer$data = Test_userRenderer;
1369-
export type Test_userRenderer$key = {
1370-
readonly " $data"?: Test_userRenderer$data;
1371-
readonly " $fragmentRefs": FragmentRefs<"Test_userRenderer">;
1372-
};
1373-
1374-
`;
1375-
13761285
exports[`Snapshot tests TypeScriptGenerator with a single artifact directory matches expected output: query-with-raw-response-on-conditional.graphql 1`] = `
13771286
~~~~~~~~~~ INPUT ~~~~~~~~~~
13781287
query ExampleQuery($id: ID!, $condition: Boolean!) @raw_response_type {
@@ -3607,97 +3516,6 @@ export type ScalarHandleField = {
36073516
36083517
`;
36093518
3610-
exports[`Snapshot tests TypeScriptGenerator without a single artifact directory matches expected output: query-with-module-field.graphql 1`] = `
3611-
~~~~~~~~~~ INPUT ~~~~~~~~~~
3612-
query Test @raw_response_type {
3613-
node(id: "1") {
3614-
...Test_user
3615-
}
3616-
}
3617-
3618-
fragment Test_user on User {
3619-
plainUserRenderer {
3620-
...Test_userRenderer @module(name: "Renderer.react")
3621-
}
3622-
}
3623-
3624-
fragment Test_userRenderer on PlainUserRenderer {
3625-
user {
3626-
username
3627-
}
3628-
}
3629-
3630-
~~~~~~~~~~ OUTPUT ~~~~~~~~~~
3631-
// Test.graphql
3632-
import { Local3DPayload, FragmentRefs } from "relay-runtime";
3633-
export type TestVariables = {};
3634-
export type TestResponse = {
3635-
readonly node: {
3636-
readonly " $fragmentRefs": FragmentRefs<"Test_user">;
3637-
} | null;
3638-
};
3639-
export type Test_userRenderer = {
3640-
readonly user: ({
3641-
readonly username: string | null;
3642-
readonly id: string | null;
3643-
}) | null;
3644-
};
3645-
export type TestRawResponse = {
3646-
readonly node: ({
3647-
readonly __typename: "User";
3648-
readonly id: string | null;
3649-
readonly plainUserRenderer: ({
3650-
readonly __module_operation_Test_user: unknown | null;
3651-
readonly __module_component_Test_user: unknown | null;
3652-
...Test_userRenderer
3653-
} | Local3DPayload<"Test_user", {
3654-
...Test_userRenderer
3655-
}>) | null;
3656-
} | {
3657-
readonly __typename: string | null;
3658-
readonly id: string | null;
3659-
}) | null;
3660-
};
3661-
export type Test = {
3662-
readonly response: TestResponse;
3663-
readonly variables: TestVariables;
3664-
readonly rawResponse: TestRawResponse;
3665-
};
3666-
3667-
3668-
// Test_user.graphql
3669-
import { FragmentRefs } from "relay-runtime";
3670-
export type Test_user = {
3671-
readonly plainUserRenderer: {
3672-
readonly __fragmentPropName?: string | null;
3673-
readonly __module_component?: string | null;
3674-
readonly " $fragmentRefs": FragmentRefs<"Test_userRenderer">;
3675-
} | null;
3676-
readonly " $refType": "Test_user";
3677-
};
3678-
export type Test_user$data = Test_user;
3679-
export type Test_user$key = {
3680-
readonly " $data"?: Test_user$data;
3681-
readonly " $fragmentRefs": FragmentRefs<"Test_user">;
3682-
};
3683-
3684-
3685-
// Test_userRenderer.graphql
3686-
import { FragmentRefs } from "relay-runtime";
3687-
export type Test_userRenderer = {
3688-
readonly user: {
3689-
readonly username: string | null;
3690-
} | null;
3691-
readonly " $refType": "Test_userRenderer";
3692-
};
3693-
export type Test_userRenderer$data = Test_userRenderer;
3694-
export type Test_userRenderer$key = {
3695-
readonly " $data"?: Test_userRenderer$data;
3696-
readonly " $fragmentRefs": FragmentRefs<"Test_userRenderer">;
3697-
};
3698-
3699-
`;
3700-
37013519
exports[`Snapshot tests TypeScriptGenerator without a single artifact directory matches expected output: query-with-raw-response-on-conditional.graphql 1`] = `
37023520
~~~~~~~~~~ INPUT ~~~~~~~~~~
37033521
query ExampleQuery($id: ID!, $condition: Boolean!) @raw_response_type {

yarn.lock

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -790,9 +790,9 @@
790790
typescript "^3.0.0"
791791

792792
"@types/relay-runtime@*":
793-
version "6.0.7"
794-
resolved "https://registry.yarnpkg.com/@types/relay-runtime/-/relay-runtime-6.0.7.tgz#2c63bca39c47a4919a7e1530814b64a68c868895"
795-
integrity sha512-WNJEWhJI/H1cYy8n3tvyeIVbiDFmnBiQn7c4hptWfYA/zUsERxiwTYv+H+zzRIvWTt1tWLRWeVvGcXtfZy9y7Q==
793+
version "8.0.6"
794+
resolved "https://registry.yarnpkg.com/@types/relay-runtime/-/relay-runtime-8.0.6.tgz#931c640f9a37b4c328aaa4e5159e0c85e5ffbbfa"
795+
integrity sha512-05MJfbE/1TAz4ttx9a9x9xjaOe0KAYymWo/FcqGh+wRvz5ZvRdRPEssmIwcNsdJZVWTB3R/VBAYx6IvXMHIPNQ==
796796

797797
"@types/relay-runtime@^8.0.0":
798798
version "8.0.0"
@@ -2571,13 +2571,20 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.6
25712571
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.3.tgz#4a12ff1b60376ef09862c2093edd908328be8423"
25722572
integrity sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ==
25732573

2574-
graphql@*, graphql@^14.5.3, graphql@^14.5.8:
2574+
graphql@*, graphql@^14.5.8:
25752575
version "14.5.8"
25762576
resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.5.8.tgz#504f3d3114cb9a0a3f359bbbcf38d9e5bf6a6b3c"
25772577
integrity sha512-MMwmi0zlVLQKLdGiMfWkgQD7dY/TUKt4L+zgJ/aR0Howebod3aNgP5JkgvAULiR2HPVZaP2VEElqtdidHweLkg==
25782578
dependencies:
25792579
iterall "^1.2.2"
25802580

2581+
graphql@^14.5.3:
2582+
version "14.6.0"
2583+
resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.6.0.tgz#57822297111e874ea12f5cd4419616930cd83e49"
2584+
integrity sha512-VKzfvHEKybTKjQVpTFrA5yUq2S9ihcZvfJAtsDBBCuV6wauPu1xl/f9ehgVf0FcEJJs4vz6ysb/ZMkGigQZseg==
2585+
dependencies:
2586+
iterall "^1.2.2"
2587+
25812588
growly@^1.3.0:
25822589
version "1.3.0"
25832590
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
@@ -3215,9 +3222,9 @@ istanbul-reports@^2.2.6:
32153222
handlebars "^4.1.2"
32163223

32173224
iterall@^1.2.2:
3218-
version "1.2.2"
3219-
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.2.2.tgz#92d70deb8028e0c39ff3164fdbf4d8b088130cd7"
3220-
integrity sha512-yynBb1g+RFUPY64fTrFv7nsjRrENBQJaX2UL+2Szc9REFrSNm1rpSXHGzhmAy7a9uv3vlvgBlXnf9RqmPH1/DA==
3225+
version "1.3.0"
3226+
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.3.0.tgz#afcb08492e2915cbd8a0884eb93a8c94d0d72fea"
3227+
integrity sha512-QZ9qOMdF+QLHxy1QIpUHUU1D5pS2CG2P69LF6L6CPjPYA/XMOmKV3PZpawHoAjHNyB0swdVTRxdYT4tbBbxqwg==
32213228

32223229
jest-changed-files@^24.9.0:
32233230
version "24.9.0"
@@ -5917,11 +5924,16 @@ type-fest@^0.6.0:
59175924
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.6.0.tgz#8d2a2370d3df886eb5c90ada1c5bf6188acf838b"
59185925
integrity sha512-q+MB8nYR1KDLrgr4G5yemftpMC7/QLqVndBmEEdqzmNj5dcFOO4Oo8qlwZE3ULT3+Zim1F8Kq4cBnikNhlCMlg==
59195926

5920-
[email protected], typescript@^3.0.0:
5927+
59215928
version "3.6.4"
59225929
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.6.4.tgz#b18752bb3792bc1a0281335f7f6ebf1bbfc5b91d"
59235930
integrity sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==
59245931

5932+
typescript@^3.0.0:
5933+
version "3.7.5"
5934+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.5.tgz#0692e21f65fd4108b9330238aac11dd2e177a1ae"
5935+
integrity sha512-/P5lkRXkWHNAbcJIiHPfRoKqyd7bsyCma1hZNUGfn20qm64T6ZBlrzprymeu918H+mB/0rIg2gGK/BXkhhYgBw==
5936+
59255937
ua-parser-js@^0.7.18:
59265938
version "0.7.20"
59275939
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.20.tgz#7527178b82f6a62a0f243d1f94fd30e3e3c21098"

0 commit comments

Comments
 (0)