Skip to content

Commit

Permalink
Refactor extra typegen config fields into TypegenOptions struct
Browse files Browse the repository at this point in the history
Summary:
follow up from D51639151 -

We have a few bool fields in TypegenContext that would make more sense in a separate `TypegenOptions` struct, this diff does the refactor.

Reviewed By: alunyov

Differential Revision: D51761410

fbshipit-source-id: fe6e02e23d6b8a9a2387a608ce3e0449644cbf94
  • Loading branch information
monicatang authored and facebook-github-bot committed Dec 4, 2023
1 parent e694ba8 commit 400cfa8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
42 changes: 26 additions & 16 deletions compiler/crates/relay-typegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ fn generate_fragment_type_exports_section_impl(
.is_some(),
fragment_definition.name.map(|x| x.0),
fragment_locations,
false,
is_extra_artifact_branch_module,
TypegenOptions {
no_optional_fields_in_raw_response_type: false,
is_extra_artifact_branch_module,
},
);
let mut writer = new_writer_from_config(&project_config.typegen_config);
write_fragment_type_exports_section(&typegen_context, fragment_definition, &mut writer)
Expand All @@ -180,8 +182,10 @@ pub fn generate_named_validator_export(
.is_some(),
fragment_definition.name.map(|x| x.0),
fragment_locations,
false,
false,
TypegenOptions {
no_optional_fields_in_raw_response_type: false,
is_extra_artifact_branch_module: false,
},
);
let mut writer = new_writer_from_config(&project_config.typegen_config);
write_validator_function(&typegen_context, fragment_definition, &mut writer).unwrap();
Expand Down Expand Up @@ -217,8 +221,10 @@ pub fn generate_operation_type_exports_section(
typegen_operation.name.item.0,
),
fragment_locations,
false,
false,
TypegenOptions {
no_optional_fields_in_raw_response_type: false,
is_extra_artifact_branch_module: false,
},
);
let mut writer = new_writer_from_config(&project_config.typegen_config);
write_operation_type_exports_section(
Expand Down Expand Up @@ -252,8 +258,10 @@ pub fn generate_split_operation_type_exports_section(
typegen_operation.name.item.0,
),
fragment_locations,
no_optional_fields_in_raw_response_type,
false,
TypegenOptions {
no_optional_fields_in_raw_response_type,
is_extra_artifact_branch_module: false,
},
);
let mut writer = new_writer_from_config(&project_config.typegen_config);

Expand All @@ -276,10 +284,7 @@ struct TypegenContext<'a> {
has_unified_output: bool,
generating_updatable_types: bool,
definition_source_location: WithLocation<StringKey>,
// All keys in raw response should be required
no_optional_fields_in_raw_response_type: bool,
// Some extra artifacts require special type generation
is_extra_artifact_branch_module: bool,
typegen_options: TypegenOptions,
}

impl<'a> TypegenContext<'a> {
Expand All @@ -289,8 +294,7 @@ impl<'a> TypegenContext<'a> {
generating_updatable_types: bool,
definition_source_location: WithLocation<StringKey>,
fragment_locations: &'a FragmentLocations,
no_optional_fields_in_raw_response_type: bool,
is_extra_artifact_branch_module: bool,
typegen_options: TypegenOptions,
) -> Self {
Self {
schema,
Expand All @@ -299,8 +303,14 @@ impl<'a> TypegenContext<'a> {
has_unified_output: project_config.output.is_some(),
generating_updatable_types,
definition_source_location,
no_optional_fields_in_raw_response_type,
is_extra_artifact_branch_module,
typegen_options,
}
}
}

struct TypegenOptions {
// All keys in raw response should be required
no_optional_fields_in_raw_response_type: bool,
// Some extra artifacts require special type generation
is_extra_artifact_branch_module: bool,
}
6 changes: 4 additions & 2 deletions compiler/crates/relay-typegen/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1682,8 +1682,10 @@ fn raw_response_make_prop(
runtime_imports: &mut RuntimeImports,
custom_scalars: &mut CustomScalarsImports,
) -> Prop {
let optional =
!typegen_context.no_optional_fields_in_raw_response_type && type_selection.is_conditional();
let optional = !typegen_context
.typegen_options
.no_optional_fields_in_raw_response_type
&& type_selection.is_conditional();
match type_selection {
TypeSelection::ModuleDirective(module_directive) => Prop::Spread(SpreadProp {
value: module_directive.fragment_name.0,
Expand Down
5 changes: 4 additions & 1 deletion compiler/crates/relay-typegen/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,10 @@ pub(crate) fn write_fragment_type_exports_section(
if !is_assignable_fragment {
writer.write_export_type(&data_type_name, &data_type)?;
writer.write_export_type(&format!("{}$key", fragment_definition.name.item), &ref_type)?;
} else if typegen_context.is_extra_artifact_branch_module {
} else if typegen_context
.typegen_options
.is_extra_artifact_branch_module
{
writer.write_export_type(&data_type_name, &data_type)?;
}

Expand Down

0 comments on commit 400cfa8

Please sign in to comment.