Skip to content

Commit

Permalink
Make derived_from optional on SplitOperationMetadata
Browse files Browse the repository at this point in the history
Summary:
This will allow us to generate split operations that are not derived from fragments. It will be used for $normalization operations generated for Relay resolvers that want to return objects.

In this specific diff we do not change the observable behavior of the compiler. But the `panic!` is moved from `split_operation_metadata` to `generate_artifacts`.

Reviewed By: rbalicki2

Differential Revision: D39144124

fbshipit-source-id: b6105fd1bd63e6b7ef62c5228fa1269c8b719f2b
  • Loading branch information
alunyov authored and facebook-github-bot committed Sep 6, 2022
1 parent 6efa1a0 commit cdbacef
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ pub fn generate_artifacts(
{
// Generate normalization file for SplitOperation
let metadata = SplitOperationMetadata::from(directive);
let source_hash = source_hashes.get(&metadata.derived_from.0).cloned().unwrap();
let source_hash = metadata.derived_from.and_then(|derived_from|
source_hashes.get(&derived_from.0).cloned()).unwrap_or_else(|| panic!("Expected a source hash for split operation."));
let source_file = metadata.location.source_location();
let typegen_operation = if metadata.raw_response_type {
Some(Arc::clone(normalization))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ impl ApplyFragmentArgumentsTransform<'_, '_, '_> {
));
}
let mut metadata = SplitOperationMetadata {
derived_from: fragment.name.item,
derived_from: Some(fragment.name.item),
location: fragment.name.location,
parent_documents: Default::default(),
raw_response_type: is_raw_response_type_enabled(directive),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl Transformer for SplitModuleImportTransform<'_, '_> {
.collect();
(
SplitOperationMetadata {
derived_from: module_metadata.fragment_name,
derived_from: Some(module_metadata.fragment_name),
location: module_metadata.location,
parent_documents: Default::default(),
raw_response_type: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ lazy_static! {
pub struct SplitOperationMetadata {
/// Name of the fragment that this split operation represents. This is used
/// to determine the name of the generated artifact.
pub derived_from: FragmentDefinitionName,
pub derived_from: Option<FragmentDefinitionName>,

/// Location of the source file for this split operation
pub location: Location,
Expand All @@ -68,24 +68,25 @@ pub struct SplitOperationMetadata {

impl SplitOperationMetadata {
pub fn to_directive(&self) -> Directive {
let mut arguments = vec![
Argument {
let mut arguments = vec![];
if let Some(derived_from) = self.derived_from {
arguments.push(Argument {
name: WithLocation::generated(*ARG_DERIVED_FROM),
value: WithLocation::generated(Value::Constant(ConstantValue::String(
self.derived_from.0,
))),
},
Argument {
name: WithLocation::generated(*ARG_PARENT_DOCUMENTS),
value: WithLocation::generated(Value::Constant(ConstantValue::List(
self.parent_documents
.iter()
.cloned()
.map(ConstantValue::String)
.collect(),
derived_from.0,
))),
},
];
})
}
arguments.push(Argument {
name: WithLocation::generated(*ARG_PARENT_DOCUMENTS),
value: WithLocation::generated(Value::Constant(ConstantValue::List(
self.parent_documents
.iter()
.cloned()
.map(ConstantValue::String)
.collect(),
))),
});
if self.raw_response_type {
arguments.push(Argument {
name: WithLocation::generated(*ARG_RAW_RESPONSE_TYPE),
Expand All @@ -104,13 +105,12 @@ impl From<&Directive> for SplitOperationMetadata {
fn from(directive: &Directive) -> Self {
debug_assert!(directive.name.item == *DIRECTIVE_SPLIT_OPERATION);
let location = directive.name.location;
let derived_from_arg = directive
.arguments
.named(ARG_DERIVED_FROM.0)
.expect("Expected derived_from arg to exist");
let derived_from_arg = directive.arguments.named(ARG_DERIVED_FROM.0);

let derived_from = derived_from_arg.map(|derived_from_arg| {
FragmentDefinitionName(derived_from_arg.value.item.expect_string_literal())
});

let derived_from =
FragmentDefinitionName(derived_from_arg.value.item.expect_string_literal());
let parent_documents_arg = directive
.arguments
.named(ARG_PARENT_DOCUMENTS.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl<'program, 'flag> RelayClientComponentTransform<'program, 'flag> {
get_normalization_operation_name(spread.fragment.item.0).intern();
(
SplitOperationMetadata {
derived_from: spread.fragment.item,
derived_from: Some(spread.fragment.item),
location: spread.fragment.location,
parent_documents: Default::default(),
raw_response_type: false,
Expand Down

0 comments on commit cdbacef

Please sign in to comment.