Skip to content

Commit

Permalink
Remove path from @catch codegen
Browse files Browse the repository at this point in the history
Reviewed By: tyao1

Differential Revision: D65459455

fbshipit-source-id: 53b6d8d57e289301286ba579983f9ec0f5bf98a1
  • Loading branch information
captbaritone authored and facebook-github-bot committed Nov 11, 2024
1 parent a88c013 commit 460508a
Show file tree
Hide file tree
Showing 41 changed files with 67 additions and 162 deletions.
1 change: 0 additions & 1 deletion compiler/crates/relay-codegen/src/build_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,6 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> {
kind: Primitive::String(CODEGEN_CONSTANTS.catch_field),
field: primitive,
to: Primitive::String(catch_metadata.to.into()),
path: Primitive::String(catch_metadata.path),
}))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ fragment MyFragment on Node {
"name": "name",
"storageKey": null
},
"to": "RESULT",
"path": "name"
"to": "RESULT"
}
],
"type": "Node",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ fragment MyFragment on Node {
"kind": "AliasedInlineFragmentSpread",
"name": "User"
},
"to": "RESULT",
"path": "User"
"to": "RESULT"
}
],
"type": "Node",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ fragment MyFragment on Node {
"kind": "AliasedInlineFragmentSpread",
"name": "myAlias"
},
"to": "RESULT",
"path": "myAlias"
"to": "RESULT"
}
],
"type": "Node",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ fragment MyFragment on User {
"name": "street",
"storageKey": null
},
"to": "RESULT",
"path": "address.street"
"to": "RESULT"
}
],
"storageKey": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ fragment MyFragment on User {
],
"storageKey": null
},
"to": "RESULT",
"path": "address"
"to": "RESULT"
}
],
"type": "User",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ fragment MyFragment on User {
"name": "street",
"storageKey": null
},
"to": "RESULT",
"path": "address.street"
"to": "RESULT"
}
],
"storageKey": null
},
"to": "RESULT",
"path": "address"
"to": "RESULT"
}
],
"type": "User",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ fragment MyFragmentFirst on User {
"name": "lastName",
"storageKey": null
},
"to": "RESULT",
"path": "parents.lastName"
"to": "RESULT"
}
],
"storageKey": null
},
"to": "NULL",
"path": "parents"
"to": "NULL"
}
],
"type": "User",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@ fragment MyFragmentFirst on User {
"name": "lastName",
"storageKey": null
},
"to": "RESULT",
"path": "parents.lastName"
"to": "RESULT"
}
],
"storageKey": null
},
"to": "NULL",
"path": "parents"
"to": "NULL"
}
],
"type": "User",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ fragment MyFragment on Node {
"name": "name",
"storageKey": null
},
"to": "RESULT",
"path": "name"
"to": "RESULT"
}
],
"type": "Node",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ fragment MyFragment on Node {
"name": "name",
"storageKey": null
},
"to": "NULL",
"path": "name"
"to": "NULL"
}
],
"type": "Node",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ query Foo {
"name": "name",
"storageKey": null
},
"to": "RESULT",
"path": "me.name"
"to": "RESULT"
}
],
"storageKey": null
Expand Down
89 changes: 19 additions & 70 deletions compiler/crates/relay-transforms/src/catch_directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

use std::sync::Arc;

use ::intern::string_key::Intern;
use ::intern::string_key::StringKey;
use common::ArgumentName;
use common::Diagnostic;
Expand All @@ -29,8 +28,6 @@ use intern::intern;
use lazy_static::lazy_static;
mod catchable_node;
mod validation_message;
use graphql_ir::Field;
use intern::Lookup;

use self::catchable_node::CatchMetadata;
use self::catchable_node::CatchableNode;
Expand Down Expand Up @@ -75,7 +72,6 @@ impl From<CatchTo> for StringKey {
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct CatchMetadataDirective {
pub to: CatchTo,
pub path: StringKey,
}
associated_data_impl!(CatchMetadataDirective);

Expand Down Expand Up @@ -105,15 +101,13 @@ struct CatchDirective<'s> {
#[allow(dead_code)]
program: &'s Program,
errors: Vec<Diagnostic>,
path: Vec<&'s str>,
}

impl<'program> CatchDirective<'program> {
fn new(program: &'program Program) -> Self {
Self {
program,
errors: Default::default(),
path: vec![],
}
}

Expand Down Expand Up @@ -158,7 +152,7 @@ impl<'s> Transformer for CatchDirective<'s> {
None => self.default_transform_operation(operation),
Some(catch_metadata) => {
let next_directives =
add_metadata_directive(&operation.directives, intern!(""), catch_metadata.to);
add_metadata_directive(&operation.directives, catch_metadata.to);

let selections = self.transform_selections(&operation.selections);

Expand All @@ -180,7 +174,7 @@ impl<'s> Transformer for CatchDirective<'s> {
None => self.default_transform_fragment(fragment),
Some(catch_metadata) => {
let next_directives =
add_metadata_directive(&fragment.directives, intern!(""), catch_metadata.to);
add_metadata_directive(&fragment.directives, catch_metadata.to);

let selections = self.transform_selections(&fragment.selections);

Expand All @@ -194,36 +188,23 @@ impl<'s> Transformer for CatchDirective<'s> {
}

fn transform_scalar_field(&mut self, field: &ScalarField) -> Transformed<Selection> {
let name = field.alias_or_name(&self.program.schema).lookup();
self.path.push(name);
let path_name: StringKey = self.path.join(".").intern();
self.path.pop();

match self.get_catch_metadata(field) {
None => Transformed::Keep,
Some(catch_metadata) => {
Transformed::Replace(Selection::ScalarField(Arc::new(ScalarField {
directives: add_metadata_directive(
&field.directives,
path_name,
catch_metadata.to,
),
directives: add_metadata_directive(&field.directives, catch_metadata.to),
..field.clone()
})))
}
}
}

fn transform_linked_field(&mut self, field: &LinkedField) -> Transformed<Selection> {
let name = field.alias_or_name(&self.program.schema).lookup();
self.path.push(name);

let maybe_catch_metadata = self.get_catch_metadata(field);

match maybe_catch_metadata {
None => {
let selections = self.transform_selections(&field.selections);
self.path.pop();
if selections.should_keep() {
Transformed::Keep
} else {
Expand All @@ -234,14 +215,10 @@ impl<'s> Transformer for CatchDirective<'s> {
}
}
Some(catch_metadata) => {
let path_name = self.path.join(".").intern();
let next_directives =
add_metadata_directive(&field.directives, path_name, catch_metadata.to);
let next_directives = add_metadata_directive(&field.directives, catch_metadata.to);

let selections = self.transform_selections(&field.selections);

self.path.pop();

Transformed::Replace(Selection::LinkedField(Arc::new(LinkedField {
directives: next_directives,
selections: selections.replace_or_else(|| field.selections.clone()),
Expand All @@ -257,44 +234,21 @@ impl<'s> Transformer for CatchDirective<'s> {
let maybe_catch_metadata = self.get_catch_metadata(fragment);

match alias {
Some(alias) => {
self.path.push(alias.alias.item.lookup());
match maybe_catch_metadata {
None => {
let selections = self.transform_selections(&fragment.selections);
self.path.pop();
if selections.should_keep() {
Transformed::Keep
} else {
Transformed::Replace(Selection::InlineFragment(Arc::new(
InlineFragment {
selections: selections
.replace_or_else(|| fragment.selections.clone()),
..fragment.clone()
},
)))
}
}
Some(catch_metadata) => {
let path_name = self.path.join(".").intern();
let next_directives = add_metadata_directive(
&fragment.directives,
path_name,
catch_metadata.to,
);

let selections = self.transform_selections(&fragment.selections);

self.path.pop();

Transformed::Replace(Selection::InlineFragment(Arc::new(InlineFragment {
directives: next_directives,
selections: selections.replace_or_else(|| fragment.selections.clone()),
..fragment.clone()
})))
}
Some(_alias) => match maybe_catch_metadata {
None => self.default_transform_inline_fragment(fragment),
Some(catch_metadata) => {
let next_directives =
add_metadata_directive(&fragment.directives, catch_metadata.to);

let selections = self.transform_selections(&fragment.selections);

Transformed::Replace(Selection::InlineFragment(Arc::new(InlineFragment {
directives: next_directives,
selections: selections.replace_or_else(|| fragment.selections.clone()),
..fragment.clone()
})))
}
}
},
None => {
if maybe_catch_metadata.is_some() {
self.errors.push(Diagnostic::error(
Expand All @@ -308,17 +262,12 @@ impl<'s> Transformer for CatchDirective<'s> {
}
}

fn add_metadata_directive(
directives: &[Directive],
path_name: StringKey,
to: Option<CatchTo>,
) -> Vec<Directive> {
fn add_metadata_directive(directives: &[Directive], to: Option<CatchTo>) -> Vec<Directive> {
let mut next_directives: Vec<Directive> = Vec::with_capacity(directives.len() + 1);
next_directives.extend(directives.iter().cloned());
next_directives.push(
CatchMetadataDirective {
to: catch_to_with_fallback(to),
path: path_name,
}
.into(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ fragment FragFoo on User {
@__CatchMetadataDirective
# CatchMetadataDirective {
# to: Result,
# path: "User",
# }
{
name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ query FragFoo @catch {
query FragFoo @catch @__CatchMetadataDirective
# CatchMetadataDirective {
# to: Result,
# path: "",
# }
{
me {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ fragment FragFoo on User @catch(to: RESULT) {
fragment FragFoo on User @catch(to: RESULT) @__CatchMetadataDirective
# CatchMetadataDirective {
# to: Result,
# path: "",
# }
{
name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ fragment FragFoo on User {
@__CatchMetadataDirective
# CatchMetadataDirective {
# to: Result,
# path: "User",
# }
{
name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ fragment MyFragmentSecond on User {
address @catch(to: RESULT) @__CatchMetadataDirective
# CatchMetadataDirective {
# to: Result,
# path: "address",
# }
{
street
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ fragment MyFragment on User {
address @catch(to: RESULT) @__CatchMetadataDirective
# CatchMetadataDirective {
# to: Result,
# path: "address",
# }
{
street
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ fragment MyFragmentFirst on User {
parents @catch @__CatchMetadataDirective
# CatchMetadataDirective {
# to: Result,
# path: "parents",
# }
{
lastName @catch @__CatchMetadataDirective
# CatchMetadataDirective {
# to: Result,
# path: "parents.lastName",
# }

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ query Foo {
name @catch @__CatchMetadataDirective
# CatchMetadataDirective {
# to: Result,
# path: "me.name",
# }

}
Expand Down
Loading

0 comments on commit 460508a

Please sign in to comment.