Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions compiler/crates/relay-typegen/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2389,6 +2389,24 @@ pub(crate) fn raw_response_visit_selections(
),
}
}

if let Some(concrete_type) = enclosing_linked_field_concrete_type {
// If we are generating for a concrete field type, we should 1. remove
// any selections that are for a different concrete type, since they are
// not applicable to our field, and 2. mark any selections without a
// concrete type as being for our field's concrete type, so that
// `raw_response_selections_to_babel` doesn't generate redundant types.
type_selections.retain_mut(|type_selection| {
match type_selection.get_enclosing_concrete_type() {
Some(selection_concrete_type) => selection_concrete_type == concrete_type,
None => {
type_selection.set_concrete_type(concrete_type);
true
}
}
});
}

type_selections
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ export type relayResolver_Query$data = {|
|},
|};
export type relayResolver_Query$rawResponse = {|
+me: ?({|
+id: string,
|} | {|
+me: ?{|
+id: string,
+name: ?string,
|}),
|},
|};
export type relayResolver_Query = {|
rawResponse: relayResolver_Query$rawResponse,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
==================================== INPUT ====================================
fragment MyFragment on Actor {
name
... on User {
canViewerLike
}
... on Page {
subscribeStatus
}
}

query MyQuery @raw_response_type {
me {
canViewerComment
...MyFragment
}
}

mutation MyMutation @raw_response_type {
setName(name: "test") {
canViewerComment
...MyFragment
}
}
==================================== OUTPUT ===================================
import type { MyFragment$fragmentType } from "MyFragment.graphql";
export type MyMutation$variables = {||};
export type MyMutation$data = {|
+setName: ?{|
+canViewerComment: ?CustomBoolean,
+$fragmentSpreads: MyFragment$fragmentType,
|},
|};
export type MyMutation$rawResponse = {|
+setName: ?{|
+__isActor: "User",
+canViewerComment: ?CustomBoolean,
+canViewerLike: ?CustomBoolean,
+id: string,
+name: ?string,
|},
|};
export type MyMutation = {|
rawResponse: MyMutation$rawResponse,
response: MyMutation$data,
variables: MyMutation$variables,
|};
-------------------------------------------------------------------------------
import type { MyFragment$fragmentType } from "MyFragment.graphql";
export type MyQuery$variables = {||};
export type MyQuery$data = {|
+me: ?{|
+canViewerComment: ?CustomBoolean,
+$fragmentSpreads: MyFragment$fragmentType,
|},
|};
export type MyQuery$rawResponse = {|
+me: ?{|
+__isActor: "User",
+canViewerComment: ?CustomBoolean,
+canViewerLike: ?CustomBoolean,
+id: string,
+name: ?string,
|},
|};
export type MyQuery = {|
rawResponse: MyQuery$rawResponse,
response: MyQuery$data,
variables: MyQuery$variables,
|};
-------------------------------------------------------------------------------
import type { FragmentType } from "relay-runtime";
declare export opaque type MyFragment$fragmentType: FragmentType;
export type MyFragment$data = {|
+canViewerLike?: ?CustomBoolean,
+name: ?string,
+subscribeStatus?: ?string,
+$fragmentType: MyFragment$fragmentType,
|};
export type MyFragment$key = {
+$data?: MyFragment$data,
+$fragmentSpreads: MyFragment$fragmentType,
...
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
fragment MyFragment on Actor {
name
... on User {
canViewerLike
}
... on Page {
subscribeStatus
}
}

query MyQuery @raw_response_type {
me {
canViewerComment
...MyFragment
}
}

mutation MyMutation @raw_response_type {
setName(name: "test") {
canViewerComment
...MyFragment
}
}
7 changes: 7 additions & 0 deletions compiler/crates/relay-typegen/tests/generate_flow_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,13 @@ async fn simple() {
test_fixture(transform_fixture, file!(), "simple.graphql", "generate_flow/fixtures/simple.expected", input, expected).await;
}

#[tokio::test]
async fn spread_interface_fragment_on_concrete_raw_type() {
let input = include_str!("generate_flow/fixtures/spread-interface-fragment-on-concrete-raw-type.graphql");
let expected = include_str!("generate_flow/fixtures/spread-interface-fragment-on-concrete-raw-type.expected");
test_fixture(transform_fixture, file!(), "spread-interface-fragment-on-concrete-raw-type.graphql", "generate_flow/fixtures/spread-interface-fragment-on-concrete-raw-type.expected", input, expected).await;
}

#[tokio::test]
async fn typename_in_union_with_other_fields() {
let input = include_str!("generate_flow/fixtures/typename-in-union-with-other-fields.graphql");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ export type Foo_user$rawResponse = {
readonly id: string;
readonly lastName: string | null | undefined;
}>;
} | {
readonly id: string;
} | null | undefined;
};
export type Foo_user = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
==================================== INPUT ====================================
fragment MyFragment on Actor {
name
... on User {
canViewerLike
}
... on Page {
subscribeStatus
}
}

query MyQuery @raw_response_type {
me {
canViewerComment
...MyFragment
}
}

mutation MyMutation @raw_response_type {
setName(name: "test") {
canViewerComment
...MyFragment
}
}
==================================== OUTPUT ===================================
import { FragmentRefs } from "relay-runtime";
export type MyMutation$variables = Record<PropertyKey, never>;
export type MyMutation$data = {
readonly setName: {
readonly canViewerComment: boolean | null | undefined;
readonly " $fragmentSpreads": FragmentRefs<"MyFragment">;
} | null | undefined;
};
export type MyMutation$rawResponse = {
readonly setName: {
readonly __isActor: "User";
readonly canViewerComment: boolean | null | undefined;
readonly canViewerLike: boolean | null | undefined;
readonly id: string;
readonly name: string | null | undefined;
} | null | undefined;
};
export type MyMutation = {
rawResponse: MyMutation$rawResponse;
response: MyMutation$data;
variables: MyMutation$variables;
};
-------------------------------------------------------------------------------
import { FragmentRefs } from "relay-runtime";
export type MyQuery$variables = Record<PropertyKey, never>;
export type MyQuery$data = {
readonly me: {
readonly canViewerComment: boolean | null | undefined;
readonly " $fragmentSpreads": FragmentRefs<"MyFragment">;
} | null | undefined;
};
export type MyQuery$rawResponse = {
readonly me: {
readonly __isActor: "User";
readonly canViewerComment: boolean | null | undefined;
readonly canViewerLike: boolean | null | undefined;
readonly id: string;
readonly name: string | null | undefined;
} | null | undefined;
};
export type MyQuery = {
rawResponse: MyQuery$rawResponse;
response: MyQuery$data;
variables: MyQuery$variables;
};
-------------------------------------------------------------------------------
import { FragmentRefs } from "relay-runtime";
export type MyFragment$data = {
readonly canViewerLike?: boolean | null | undefined;
readonly name: string | null | undefined;
readonly subscribeStatus?: string | null | undefined;
readonly " $fragmentType": "MyFragment";
};
export type MyFragment$key = {
readonly " $data"?: MyFragment$data;
readonly " $fragmentSpreads": FragmentRefs<"MyFragment">;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
fragment MyFragment on Actor {
name
... on User {
canViewerLike
}
... on Page {
subscribeStatus
}
}

query MyQuery @raw_response_type {
me {
canViewerComment
...MyFragment
}
}

mutation MyMutation @raw_response_type {
setName(name: "test") {
canViewerComment
...MyFragment
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,13 @@ async fn simple_use_import_type_syntax() {
test_fixture(transform_fixture, file!(), "simple-use-import-type-syntax.graphql", "generate_typescript/fixtures/simple-use-import-type-syntax.expected", input, expected).await;
}

#[tokio::test]
async fn spread_interface_fragment_on_concrete_raw_type() {
let input = include_str!("generate_typescript/fixtures/spread-interface-fragment-on-concrete-raw-type.graphql");
let expected = include_str!("generate_typescript/fixtures/spread-interface-fragment-on-concrete-raw-type.expected");
test_fixture(transform_fixture, file!(), "spread-interface-fragment-on-concrete-raw-type.graphql", "generate_typescript/fixtures/spread-interface-fragment-on-concrete-raw-type.expected", input, expected).await;
}

#[tokio::test]
async fn typename_in_union_with_other_fields() {
let input = include_str!("generate_typescript/fixtures/typename-in-union-with-other-fields.graphql");
Expand Down