From a2f2295a4323aa85716ba28bd316715a61447cab Mon Sep 17 00:00:00 2001
From: tobias-tengler <45513122+tobias-tengler@users.noreply.github.com>
Date: Tue, 20 Aug 2024 17:52:06 +0200
Subject: [PATCH] Fix conditional sub-selections in raw response type

---
 compiler/crates/relay-typegen/src/visit.rs    | 57 ++++++++++++++-----
 ...-with-raw-response-on-conditional.expected |  6 +-
 ...-with-raw-response-on-conditional.expected |  6 +-
 ...orTest_fragment59$normalization.graphql.js |  4 +-
 ...orTest_fragment61$normalization.graphql.js |  4 +-
 5 files changed, 53 insertions(+), 24 deletions(-)

diff --git a/compiler/crates/relay-typegen/src/visit.rs b/compiler/crates/relay-typegen/src/visit.rs
index e7fb2f9162846..62f18610c1332 100644
--- a/compiler/crates/relay-typegen/src/visit.rs
+++ b/compiler/crates/relay-typegen/src/visit.rs
@@ -1215,6 +1215,37 @@ fn visit_scalar_field(
     }));
 }
 
+#[allow(clippy::too_many_arguments)]
+fn raw_response_visit_condition(
+    typegen_context: &'_ TypegenContext<'_>,
+    type_selections: &mut Vec<TypeSelection>,
+    condition: &Condition,
+    encountered_enums: &mut EncounteredEnums,
+    match_fields: &mut MatchFields,
+    encountered_fragments: &mut EncounteredFragments,
+    imported_raw_response_types: &mut ImportedRawResponseTypes,
+    runtime_imports: &mut RuntimeImports,
+    custom_scalars: &mut CustomScalarsImports,
+    is_throw_on_field_error: bool,
+) {
+    let mut selections = raw_response_visit_selections(
+        typegen_context,
+        &condition.selections,
+        encountered_enums,
+        match_fields,
+        encountered_fragments,
+        imported_raw_response_types,
+        runtime_imports,
+        custom_scalars,
+        None,
+        is_throw_on_field_error,
+    );
+    for selection in selections.iter_mut() {
+        selection.set_conditional(true);
+    }
+    type_selections.append(&mut selections);
+}
+
 #[allow(clippy::too_many_arguments)]
 fn visit_condition(
     typegen_context: &'_ TypegenContext<'_>,
@@ -2258,20 +2289,18 @@ pub(crate) fn raw_response_visit_selections(
                 enclosing_linked_field_concrete_type,
                 is_throw_on_field_error,
             ),
-            Selection::Condition(condition) => {
-                type_selections.extend(raw_response_visit_selections(
-                    typegen_context,
-                    &condition.selections,
-                    encountered_enums,
-                    match_fields,
-                    encountered_fragments,
-                    imported_raw_response_types,
-                    runtime_imports,
-                    custom_scalars,
-                    enclosing_linked_field_concrete_type,
-                    is_throw_on_field_error,
-                ));
-            }
+            Selection::Condition(condition) => raw_response_visit_condition(
+                typegen_context,
+                &mut type_selections,
+                condition,
+                encountered_enums,
+                match_fields,
+                encountered_fragments,
+                imported_raw_response_types,
+                runtime_imports,
+                custom_scalars,
+                is_throw_on_field_error,
+            ),
         }
     }
     type_selections
diff --git a/compiler/crates/relay-typegen/tests/generate_flow/fixtures/query-with-raw-response-on-conditional.expected b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/query-with-raw-response-on-conditional.expected
index 445990cd1e0a7..5963298259101 100644
--- a/compiler/crates/relay-typegen/tests/generate_flow/fixtures/query-with-raw-response-on-conditional.expected
+++ b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/query-with-raw-response-on-conditional.expected
@@ -29,13 +29,13 @@ export type ExampleQuery$data = {|
 export type ExampleQuery$rawResponse = {|
   +node: ?({|
     +__typename: "User",
-    +feedback: ?{|
+    +feedback?: ?{|
       +id: string,
       +name: ?string,
     |},
     +id: string,
-    +lastName: ?string,
-    +name: ?string,
+    +lastName?: ?string,
+    +name?: ?string,
   |} | {|
     +__typename: string,
     +id: string,
diff --git a/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/query-with-raw-response-on-conditional.expected b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/query-with-raw-response-on-conditional.expected
index 777565655be7b..4cd30d9a68dc7 100644
--- a/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/query-with-raw-response-on-conditional.expected
+++ b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/query-with-raw-response-on-conditional.expected
@@ -29,13 +29,13 @@ export type ExampleQuery$data = {
 export type ExampleQuery$rawResponse = {
   readonly node: {
     readonly __typename: "User";
-    readonly feedback: {
+    readonly feedback?: {
       readonly id: string;
       readonly name: string | null | undefined;
     } | null | undefined;
     readonly id: string;
-    readonly lastName: string | null | undefined;
-    readonly name: string | null | undefined;
+    readonly lastName?: string | null | undefined;
+    readonly name?: string | null | undefined;
   } | {
     readonly __typename: string;
     readonly id: string;
diff --git a/packages/relay-test-utils/__tests__/__generated__/RelayMockPayloadGeneratorTest_fragment59$normalization.graphql.js b/packages/relay-test-utils/__tests__/__generated__/RelayMockPayloadGeneratorTest_fragment59$normalization.graphql.js
index c7b2a6453db73..28fe8a13f328c 100644
--- a/packages/relay-test-utils/__tests__/__generated__/RelayMockPayloadGeneratorTest_fragment59$normalization.graphql.js
+++ b/packages/relay-test-utils/__tests__/__generated__/RelayMockPayloadGeneratorTest_fragment59$normalization.graphql.js
@@ -6,7 +6,7 @@
  *
  * @oncall relay
  *
- * @generated SignedSource<<e611708b926d554b0ade476854044390>>
+ * @generated SignedSource<<4cdda03c548724371f4b5575c7ac4495>>
  * @flow
  * @lightSyntaxTransform
  * @nogrep
@@ -21,7 +21,7 @@ import type { NormalizationSplitOperation } from 'relay-runtime';
 
 export type RelayMockPayloadGeneratorTest_fragment59$normalization = {|
   +id: string,
-  +name: ?string,
+  +name?: ?string,
 |};
 
 */
diff --git a/packages/relay-test-utils/__tests__/__generated__/RelayMockPayloadGeneratorTest_fragment61$normalization.graphql.js b/packages/relay-test-utils/__tests__/__generated__/RelayMockPayloadGeneratorTest_fragment61$normalization.graphql.js
index f74056539c116..01bf107fe0bc0 100644
--- a/packages/relay-test-utils/__tests__/__generated__/RelayMockPayloadGeneratorTest_fragment61$normalization.graphql.js
+++ b/packages/relay-test-utils/__tests__/__generated__/RelayMockPayloadGeneratorTest_fragment61$normalization.graphql.js
@@ -6,7 +6,7 @@
  *
  * @oncall relay
  *
- * @generated SignedSource<<eceadb8efbc75aa6f4d980309b354fb0>>
+ * @generated SignedSource<<10d99aad572cf708781d6a2425fc893e>>
  * @flow
  * @lightSyntaxTransform
  * @nogrep
@@ -21,7 +21,7 @@ import type { NormalizationSplitOperation } from 'relay-runtime';
 
 export type RelayMockPayloadGeneratorTest_fragment61$normalization = {|
   +id: string,
-  +name: ?string,
+  +name?: ?string,
 |};
 
 */