Skip to content

Commit eb6bc69

Browse files
Fix conditional sub-selections in raw response type
1 parent e357063 commit eb6bc69

File tree

5 files changed

+55
-24
lines changed

5 files changed

+55
-24
lines changed

compiler/crates/relay-typegen/src/visit.rs

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,38 @@ fn visit_scalar_field(
12841284
}));
12851285
}
12861286

1287+
#[allow(clippy::too_many_arguments)]
1288+
fn raw_response_visit_condition(
1289+
typegen_context: &'_ TypegenContext<'_>,
1290+
type_selections: &mut Vec<TypeSelection>,
1291+
condition: &Condition,
1292+
encountered_enums: &mut EncounteredEnums,
1293+
match_fields: &mut MatchFields,
1294+
encountered_fragments: &mut EncounteredFragments,
1295+
imported_raw_response_types: &mut ImportedRawResponseTypes,
1296+
runtime_imports: &mut RuntimeImports,
1297+
custom_scalars: &mut CustomScalarsImports,
1298+
enclosing_linked_field_concrete_type: Option<Type>,
1299+
emit_semantic_types: bool,
1300+
) {
1301+
let mut selections = raw_response_visit_selections(
1302+
typegen_context,
1303+
&condition.selections,
1304+
encountered_enums,
1305+
match_fields,
1306+
encountered_fragments,
1307+
imported_raw_response_types,
1308+
runtime_imports,
1309+
custom_scalars,
1310+
enclosing_linked_field_concrete_type,
1311+
emit_semantic_types,
1312+
);
1313+
for selection in selections.iter_mut() {
1314+
selection.set_conditional(true);
1315+
}
1316+
type_selections.append(&mut selections);
1317+
}
1318+
12871319
#[allow(clippy::too_many_arguments)]
12881320
fn visit_condition(
12891321
typegen_context: &'_ TypegenContext<'_>,
@@ -2327,20 +2359,19 @@ pub(crate) fn raw_response_visit_selections(
23272359
enclosing_linked_field_concrete_type,
23282360
emit_semantic_types,
23292361
),
2330-
Selection::Condition(condition) => {
2331-
type_selections.extend(raw_response_visit_selections(
2332-
typegen_context,
2333-
&condition.selections,
2334-
encountered_enums,
2335-
match_fields,
2336-
encountered_fragments,
2337-
imported_raw_response_types,
2338-
runtime_imports,
2339-
custom_scalars,
2340-
enclosing_linked_field_concrete_type,
2341-
emit_semantic_types,
2342-
));
2343-
}
2362+
Selection::Condition(condition) => raw_response_visit_condition(
2363+
typegen_context,
2364+
&mut type_selections,
2365+
condition,
2366+
encountered_enums,
2367+
match_fields,
2368+
encountered_fragments,
2369+
imported_raw_response_types,
2370+
runtime_imports,
2371+
custom_scalars,
2372+
enclosing_linked_field_concrete_type,
2373+
emit_semantic_types,
2374+
),
23442375
}
23452376
}
23462377
type_selections

compiler/crates/relay-typegen/tests/generate_flow/fixtures/query-with-raw-response-on-conditional.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ export type ExampleQuery$data = {|
2929
export type ExampleQuery$rawResponse = {|
3030
+node: ?({|
3131
+__typename: "User",
32-
+feedback: ?{|
32+
+feedback?: ?{|
3333
+id: string,
3434
+name: ?string,
3535
|},
3636
+id: string,
37-
+lastName: ?string,
38-
+name: ?string,
37+
+lastName?: ?string,
38+
+name?: ?string,
3939
|} | {|
4040
+__typename: string,
4141
+id: string,

compiler/crates/relay-typegen/tests/generate_typescript/fixtures/query-with-raw-response-on-conditional.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ export type ExampleQuery$data = {
2929
export type ExampleQuery$rawResponse = {
3030
readonly node: {
3131
readonly __typename: "User";
32-
readonly feedback: {
32+
readonly feedback?: {
3333
readonly id: string;
3434
readonly name: string | null | undefined;
3535
} | null | undefined;
3636
readonly id: string;
37-
readonly lastName: string | null | undefined;
38-
readonly name: string | null | undefined;
37+
readonly lastName?: string | null | undefined;
38+
readonly name?: string | null | undefined;
3939
} | {
4040
readonly __typename: string;
4141
readonly id: string;

packages/relay-test-utils/__tests__/__generated__/RelayMockPayloadGeneratorTest_fragment59$normalization.graphql.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/relay-test-utils/__tests__/__generated__/RelayMockPayloadGeneratorTest_fragment61$normalization.graphql.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)