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

Commit 9fe26b9

Browse files
committed
feat(relay): Upgrade to version 4
- remove graphql-compiler references and types, fix #111 - fix RelayCompilerPublic change, fix #95 - update transforms based on #109 - update relay version based on #106 - fix facebook/relay#2717 - fix facebook/relay#2710
1 parent 13a29dc commit 9fe26b9

18 files changed

+471
-163
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"peerDependencies": {
5353
"@types/react-relay": ">=1.3.9",
5454
"@types/relay-runtime": ">=1.3.5",
55-
"relay-compiler": ">=2.0.0",
55+
"relay-compiler": ">=4.0.0",
5656
"relay-runtime": "*",
5757
"typescript": ">=2.9.1"
5858
},
@@ -69,9 +69,9 @@
6969
"jest-cli": "^23.6.0",
7070
"lint-staged": "^7.3.0",
7171
"prettier": "^1.10.2",
72-
"relay-compiler": "^2.0.0",
73-
"relay-runtime": "^2.0.0",
74-
"relay-test-utils": "^2.0.0",
72+
"relay-compiler": "^4.0.0",
73+
"relay-runtime": "^4.0.0",
74+
"relay-test-utils": "^4.0.0",
7575
"ts-jest": "^22.0.1",
7676
"tslint": "^5.11.0",
7777
"tslint-config-prettier": "^1.15.0",

src/TypeScriptGenerator.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import {
2+
Fragment,
23
IRTransforms,
4+
IRVisitor,
5+
Root,
6+
SchemaUtils,
37
TypeGenerator,
48
TypeGeneratorOptions
59
} from "relay-compiler";
6-
import * as RelayCompilerPublic from "relay-compiler/lib/RelayCompilerPublic";
10+
import * as RelayCompilerPublic from "relay-compiler";
711

812
import { GraphQLNonNull } from "graphql";
913
import * as ts from "typescript";
@@ -14,13 +18,6 @@ import {
1418
transformScalarType
1519
} from "./TypeScriptTypeTransformers";
1620

17-
// Get the types
18-
import * as GraphQLCompilerTypes from "graphql-compiler";
19-
20-
const GraphQLCompiler: typeof GraphQLCompilerTypes = RelayCompilerPublic;
21-
22-
const { IRVisitor, SchemaUtils } = GraphQLCompiler;
23-
2421
const { isAbstractType } = SchemaUtils;
2522

2623
const REF_TYPE = " $refType";
@@ -241,7 +238,7 @@ function mergeSelections(a: SelectionMap, b: SelectionMap): SelectionMap {
241238
return merged;
242239
}
243240

244-
function isPlural(node: GraphQLCompilerTypes.Fragment): boolean {
241+
function isPlural(node: Fragment): boolean {
245242
return Boolean(node.metadata && node.metadata.plural);
246243
}
247244

@@ -472,10 +469,7 @@ function generateInputObjectTypes(state: State) {
472469
});
473470
}
474471

475-
function generateInputVariablesType(
476-
node: GraphQLCompilerTypes.Root,
477-
state: State
478-
) {
472+
function generateInputVariablesType(node: Root, state: State) {
479473
return exportType(
480474
`${node.name}Variables`,
481475
exactObjectTypeAnnotation(
@@ -593,8 +587,12 @@ function getRefTypeName(name: string): string {
593587
return `${name}$ref`;
594588
}
595589

590+
// Should match FLOW_TRANSFORMS array
591+
// https://github.com/facebook/relay/blob/master/packages/relay-compiler/language/javascript/RelayFlowGenerator.js#L660
596592
export const transforms: TypeGenerator["transforms"] = [
597-
IRTransforms.commonTransforms[2], // RelayRelayDirectiveTransform.transform
598-
IRTransforms.commonTransforms[3], // RelayMaskTransform.transform
599-
IRTransforms.printTransforms[0] // FlattenTransform.transformWithOptions({})
593+
IRTransforms.commonTransforms[1], // RelayRelayDirectiveTransform.transform,
594+
IRTransforms.commonTransforms[2], // RelayMaskTransform.transform,
595+
IRTransforms.commonTransforms[3], // RelayMatchTransform.transform,
596+
IRTransforms.printTransforms[3], // FlattenTransform.transformWithOptions({}),
597+
IRTransforms.commonTransforms[4] // RelayRefetchableFragmentTransform.transform,
600598
];

test/__snapshots__/TypeScriptGenerator-test.ts.snap

Lines changed: 213 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ fragment FragmentSpread on Node {
5151
fragment ConcreateTypes on Viewer {
5252
actor {
5353
__typename
54-
id
5554
... on Page {
55+
id
5656
...PageFragment
5757
}
5858
... on User {
@@ -94,11 +94,12 @@ export type ConcreateTypes$ref = typeof _ConcreateTypes$ref;
9494
export type ConcreateTypes = {
9595
readonly actor: ({
9696
readonly __typename: string;
97-
readonly id: string;
97+
readonly id?: string;
9898
readonly name?: string | null;
9999
readonly " $fragmentRefs": PageFragment$ref;
100100
} & ({
101101
readonly __typename: "Page";
102+
readonly id: string;
102103
readonly " $fragmentRefs": PageFragment$ref;
103104
} | {
104105
readonly __typename: "User";
@@ -370,9 +371,15 @@ declare const _NameRendererFragment$ref: unique symbol;
370371
export type NameRendererFragment$ref = typeof _NameRendererFragment$ref;
371372
export type NameRendererFragment = {
372373
readonly id: string;
373-
readonly nameRenderer: {
374-
readonly " $fragmentRefs": PlainUserNameRenderer_name$ref & MarkdownUserNameRenderer_name$ref;
375-
} | null;
374+
readonly nameRenderer: ({
375+
readonly __module_component?: any | null;
376+
} & ({
377+
readonly __module_component: any | null;
378+
} | {
379+
/*This will never be '% other', but we need some
380+
value in case none of the concrete values match.*/
381+
readonly __typename: "%other";
382+
})) | null;
376383
readonly " $refType": NameRendererFragment$ref;
377384
};
378385
@@ -433,9 +440,15 @@ export type NameRendererQueryVariables = {
433440
};
434441
export type NameRendererQueryResponse = {
435442
readonly me: {
436-
readonly nameRenderer: {
437-
readonly " $fragmentRefs": PlainUserNameRenderer_name$ref & MarkdownUserNameRenderer_name$ref;
438-
} | null;
443+
readonly nameRenderer: ({
444+
readonly __module_component?: any | null;
445+
} & ({
446+
readonly __module_component: any | null;
447+
} | {
448+
/*This will never be '% other', but we need some
449+
value in case none of the concrete values match.*/
450+
readonly __typename: "%other";
451+
})) | null;
439452
} | null;
440453
};
441454
export type NameRendererQuery = {
@@ -725,6 +738,92 @@ export type ScalarField = {
725738
726739
`;
727740
741+
exports[`TypeScriptGenerator with a single artifact directory matches expected output: typename-inside-with-overlapping-fields.graphql 1`] = `
742+
~~~~~~~~~~ INPUT ~~~~~~~~~~
743+
fragment TypenameInsideWithOverlappingFields on Viewer {
744+
actor {
745+
__typename
746+
... on Page {
747+
id
748+
name
749+
}
750+
... on User {
751+
id
752+
name
753+
profile_picture {
754+
uri
755+
}
756+
}
757+
}
758+
}
759+
760+
~~~~~~~~~~ OUTPUT ~~~~~~~~~~
761+
declare const _TypenameInsideWithOverlappingFields$ref: unique symbol;
762+
export type TypenameInsideWithOverlappingFields$ref = typeof _TypenameInsideWithOverlappingFields$ref;
763+
export type TypenameInsideWithOverlappingFields = {
764+
readonly actor: ({
765+
readonly __typename: string;
766+
readonly id?: string;
767+
readonly name?: string | null;
768+
readonly profile_picture?: {
769+
readonly uri: string | null;
770+
} | null;
771+
} & ({
772+
readonly __typename: "Page";
773+
readonly id: string;
774+
readonly name: string | null;
775+
} | {
776+
readonly __typename: "User";
777+
readonly profile_picture: {
778+
readonly uri: string | null;
779+
} | null;
780+
} | {
781+
/*This will never be '% other', but we need some
782+
value in case none of the concrete values match.*/
783+
readonly __typename: "%other";
784+
})) | null;
785+
readonly " $refType": TypenameInsideWithOverlappingFields$ref;
786+
};
787+
788+
`;
789+
790+
exports[`TypeScriptGenerator with a single artifact directory matches expected output: typename-inside-with-overlapping-fields-full.graphql 1`] = `
791+
~~~~~~~~~~ INPUT ~~~~~~~~~~
792+
fragment TypenameInsideWithOverlappingFieldsFull on Viewer {
793+
actor {
794+
__typename
795+
... on Page {
796+
id
797+
name
798+
}
799+
... on User {
800+
id
801+
name
802+
profile_picture {
803+
uri
804+
}
805+
}
806+
... on ExtraUser {
807+
id
808+
name
809+
}
810+
}
811+
}
812+
813+
~~~~~~~~~~ OUTPUT ~~~~~~~~~~
814+
THROWN EXCEPTION:
815+
816+
Error: RelayParser: Encountered 1 error(s):
817+
- Unknown type: 'ExtraUser'.
818+
819+
GraphQL request (15:12)
820+
14: }
821+
15: ... on ExtraUser {
822+
^
823+
16: id
824+
825+
`;
826+
728827
exports[`TypeScriptGenerator with a single artifact directory matches expected output: typename-on-union.graphql 1`] = `
729828
~~~~~~~~~~ INPUT ~~~~~~~~~~
730829
fragment TypenameInside on Actor {
@@ -1086,8 +1185,8 @@ fragment FragmentSpread on Node {
10861185
fragment ConcreateTypes on Viewer {
10871186
actor {
10881187
__typename
1089-
id
10901188
... on Page {
1189+
id
10911190
...PageFragment
10921191
}
10931192
... on User {
@@ -1127,11 +1226,12 @@ export type ConcreateTypes$ref = any;
11271226
export type ConcreateTypes = {
11281227
readonly actor: ({
11291228
readonly __typename: string;
1130-
readonly id: string;
1229+
readonly id?: string;
11311230
readonly name?: string | null;
11321231
readonly " $fragmentRefs": PageFragment$ref;
11331232
} & ({
11341233
readonly __typename: "Page";
1234+
readonly id: string;
11351235
readonly " $fragmentRefs": PageFragment$ref;
11361236
} | {
11371237
readonly __typename: "User";
@@ -1397,9 +1497,15 @@ type PlainUserNameRenderer_name$ref = any;
13971497
export type NameRendererFragment$ref = any;
13981498
export type NameRendererFragment = {
13991499
readonly id: string;
1400-
readonly nameRenderer: {
1401-
readonly " $fragmentRefs": PlainUserNameRenderer_name$ref & MarkdownUserNameRenderer_name$ref;
1402-
} | null;
1500+
readonly nameRenderer: ({
1501+
readonly __module_component?: any | null;
1502+
} & ({
1503+
readonly __module_component: any | null;
1504+
} | {
1505+
/*This will never be '% other', but we need some
1506+
value in case none of the concrete values match.*/
1507+
readonly __typename: "%other";
1508+
})) | null;
14031509
readonly " $refType": NameRendererFragment$ref;
14041510
};
14051511
@@ -1458,9 +1564,15 @@ export type NameRendererQueryVariables = {
14581564
};
14591565
export type NameRendererQueryResponse = {
14601566
readonly me: {
1461-
readonly nameRenderer: {
1462-
readonly " $fragmentRefs": PlainUserNameRenderer_name$ref & MarkdownUserNameRenderer_name$ref;
1463-
} | null;
1567+
readonly nameRenderer: ({
1568+
readonly __module_component?: any | null;
1569+
} & ({
1570+
readonly __module_component: any | null;
1571+
} | {
1572+
/*This will never be '% other', but we need some
1573+
value in case none of the concrete values match.*/
1574+
readonly __typename: "%other";
1575+
})) | null;
14641576
} | null;
14651577
};
14661578
export type NameRendererQuery = {
@@ -1744,6 +1856,91 @@ export type ScalarField = {
17441856
17451857
`;
17461858
1859+
exports[`TypeScriptGenerator without a single artifact directory matches expected output: typename-inside-with-overlapping-fields.graphql 1`] = `
1860+
~~~~~~~~~~ INPUT ~~~~~~~~~~
1861+
fragment TypenameInsideWithOverlappingFields on Viewer {
1862+
actor {
1863+
__typename
1864+
... on Page {
1865+
id
1866+
name
1867+
}
1868+
... on User {
1869+
id
1870+
name
1871+
profile_picture {
1872+
uri
1873+
}
1874+
}
1875+
}
1876+
}
1877+
1878+
~~~~~~~~~~ OUTPUT ~~~~~~~~~~
1879+
export type TypenameInsideWithOverlappingFields$ref = any;
1880+
export type TypenameInsideWithOverlappingFields = {
1881+
readonly actor: ({
1882+
readonly __typename: string;
1883+
readonly id?: string;
1884+
readonly name?: string | null;
1885+
readonly profile_picture?: {
1886+
readonly uri: string | null;
1887+
} | null;
1888+
} & ({
1889+
readonly __typename: "Page";
1890+
readonly id: string;
1891+
readonly name: string | null;
1892+
} | {
1893+
readonly __typename: "User";
1894+
readonly profile_picture: {
1895+
readonly uri: string | null;
1896+
} | null;
1897+
} | {
1898+
/*This will never be '% other', but we need some
1899+
value in case none of the concrete values match.*/
1900+
readonly __typename: "%other";
1901+
})) | null;
1902+
readonly " $refType": TypenameInsideWithOverlappingFields$ref;
1903+
};
1904+
1905+
`;
1906+
1907+
exports[`TypeScriptGenerator without a single artifact directory matches expected output: typename-inside-with-overlapping-fields-full.graphql 1`] = `
1908+
~~~~~~~~~~ INPUT ~~~~~~~~~~
1909+
fragment TypenameInsideWithOverlappingFieldsFull on Viewer {
1910+
actor {
1911+
__typename
1912+
... on Page {
1913+
id
1914+
name
1915+
}
1916+
... on User {
1917+
id
1918+
name
1919+
profile_picture {
1920+
uri
1921+
}
1922+
}
1923+
... on ExtraUser {
1924+
id
1925+
name
1926+
}
1927+
}
1928+
}
1929+
1930+
~~~~~~~~~~ OUTPUT ~~~~~~~~~~
1931+
THROWN EXCEPTION:
1932+
1933+
Error: RelayParser: Encountered 1 error(s):
1934+
- Unknown type: 'ExtraUser'.
1935+
1936+
GraphQL request (15:12)
1937+
14: }
1938+
15: ... on ExtraUser {
1939+
^
1940+
16: id
1941+
1942+
`;
1943+
17471944
exports[`TypeScriptGenerator without a single artifact directory matches expected output: typename-on-union.graphql 1`] = `
17481945
~~~~~~~~~~ INPUT ~~~~~~~~~~
17491946
fragment TypenameInside on Actor {

0 commit comments

Comments
 (0)