Skip to content

Commit

Permalink
skip_redundant_nodes: do not mess with order of selections within inl…
Browse files Browse the repository at this point in the history
…ine fragments with client edge metadata

Reviewed By: captbaritone

Differential Revision: D50329555

fbshipit-source-id: 3c61cc2c0243d29adc8605879c147ace7139334b
  • Loading branch information
voideanvalue authored and facebook-github-bot committed Oct 17, 2023
1 parent 77304c2 commit 0b2791f
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions compiler/crates/relay-transforms/src/skip_redundant_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use graphql_ir::TransformedValue;
use schema::SDLSchema;

use crate::util::is_relay_custom_inline_fragment_directive;
use crate::ClientEdgeMetadataDirective;
use crate::RelayLocationAgnosticBehavior;
use crate::DEFER_STREAM_CONSTANTS;

Expand Down Expand Up @@ -277,7 +278,8 @@ impl<'s> SkipRedundantNodesTransform {
field: &LinkedField,
selection_map: &mut SelectionMap,
) -> Transformed<Arc<LinkedField>> {
let selections = self.transform_selections(&field.selections, selection_map);
let selections =
self.transform_selections(get_partitioned_selections(&field.selections), selection_map);
match selections {
TransformedValue::Keep => Transformed::Keep,
TransformedValue::Replace(selections) => {
Expand All @@ -298,7 +300,10 @@ impl<'s> SkipRedundantNodesTransform {
condition: &Condition,
selection_map: &mut SelectionMap,
) -> Transformed<Arc<Condition>> {
let selections = self.transform_selections(&condition.selections, selection_map);
let selections = self.transform_selections(
get_partitioned_selections(&condition.selections),
selection_map,
);
match selections {
TransformedValue::Keep => Transformed::Keep,
TransformedValue::Replace(selections) => {
Expand All @@ -319,7 +324,19 @@ impl<'s> SkipRedundantNodesTransform {
fragment: &InlineFragment,
selection_map: &mut SelectionMap,
) -> Transformed<Arc<InlineFragment>> {
let selections = self.transform_selections(&fragment.selections, selection_map);
let selections = self.transform_selections(
// we must not change the order of selections within inline fragments with ClientEdgeMetadataDirective
if fragment
.directives
.named(ClientEdgeMetadataDirective::directive_name())
.is_some()
{
Vec::from_iter(&fragment.selections)
} else {
get_partitioned_selections(&fragment.selections)
},
selection_map,
);
match selections {
TransformedValue::Keep => Transformed::Keep,
TransformedValue::Replace(selections) => {
Expand All @@ -335,18 +352,17 @@ impl<'s> SkipRedundantNodesTransform {
}
}

// Mostly a copy from Transformer::transform_list, but does partition and pass down `selection_map`.
// Mostly a copy from Transformer::transform_list, but with `selection_map` passed down
fn transform_selections(
&self,
selections: &[Selection],
selections: Vec<&Selection>,
selection_map: &mut SelectionMap,
) -> TransformedValue<Vec<Selection>> {
if selections.is_empty() {
return TransformedValue::Keep;
}
let mut result: Vec<Selection> = Vec::new();
let mut has_changes = false;
let selections = get_partitioned_selections(selections);

for (index, prev_item) in selections.iter().enumerate() {
let next_item = self.transform_selection(prev_item, selection_map);
Expand Down Expand Up @@ -389,7 +405,10 @@ impl<'s> SkipRedundantNodesTransform {
operation: &OperationDefinition,
) -> Transformed<OperationDefinition> {
let mut selection_map = Default::default();
let selections = self.transform_selections(&operation.selections, &mut selection_map);
let selections = self.transform_selections(
get_partitioned_selections(&operation.selections),
&mut selection_map,
);
match selections {
TransformedValue::Keep => Transformed::Keep,
TransformedValue::Replace(selections) => Transformed::Replace(OperationDefinition {
Expand All @@ -404,7 +423,10 @@ impl<'s> SkipRedundantNodesTransform {
fragment: &FragmentDefinition,
) -> Transformed<FragmentDefinition> {
let mut selection_map = Default::default();
let selections = self.transform_selections(&fragment.selections, &mut selection_map);
let selections = self.transform_selections(
get_partitioned_selections(&fragment.selections),
&mut selection_map,
);
match selections {
TransformedValue::Keep => Transformed::Keep,
TransformedValue::Replace(selections) => Transformed::Replace(FragmentDefinition {
Expand Down

0 comments on commit 0b2791f

Please sign in to comment.