Skip to content

Commit

Permalink
Apply alias rewrites to arrays (#4958)
Browse files Browse the repository at this point in the history
[#2489](#2489) introduced
automatic aliasing rules to support `@interfaceObject`.

These rules now properly apply to lists.
  • Loading branch information
o0Ignition0o authored Apr 16, 2024
1 parent 6d36ef7 commit af404f2
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .changesets/fix_watcher_raccoon_meat_crop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### Apply alias rewrites to arrays ([PR #TODO](https://github.com/apollographql/router/pull/4958))


[#2489](https://github.com/apollographql/router/pull/2489) introduced automatic aliasing rules to support `@interfaceObject`.

These rules now properly apply to lists.

By [@o0ignition0o](https://github.com/o0ignition0o) in https://github.com/apollographql/router/pull/4958
124 changes: 124 additions & 0 deletions apollo-router/src/query_planner/rewrites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ impl DataRewrite {
obj.insert(renamer.rename_key_to.clone(), value);
}
}

if let Some(arr) = selected.as_array_mut() {
for item in arr {
if let Some(obj) = item.as_object_mut() {
if let Some(value) = obj.remove(k.as_str()) {
obj.insert(renamer.rename_key_to.clone(), value);
}
}
}
}
});
}
}
Expand All @@ -92,3 +102,117 @@ pub(crate) fn apply_rewrites(
}
}
}

#[cfg(test)]
mod tests {
use serde_json_bytes::json;

use super::*;

// The schema is not used for the tests
// but we need a valid one
const SCHEMA: &str = r#"
schema
@core(feature: "https://specs.apollo.dev/core/v0.1"),
@core(feature: "https://specs.apollo.dev/join/v0.1")
{
query: Query
}
directive @core(feature: String!) repeatable on SCHEMA
directive @join__graph(name: String!, url: String!) on ENUM_VALUE
enum join__Graph {
FAKE @join__graph(name:"fake" url: "http://localhost:4001/fake")
}
type Query {
i: [I]
}
interface I {
x: Int
}
type A implements I {
x: Int
}
type B {
y: Int
}
"#;

#[test]
fn test_key_renamer_object() {
let mut data = json!({
"data": {
"__typename": "TestType",
"testField__alias_0": {
"__typename": "TestField",
"field":"thisisatest"
}
}
});

let dr = DataRewrite::KeyRenamer(DataKeyRenamer {
path: "data/testField__alias_0".into(),
rename_key_to: "testField".to_string(),
});

dr.maybe_apply(
&Schema::parse_test(SCHEMA, &Default::default()).unwrap(),
&mut data,
);

assert_eq!(
json! {{
"data": {
"__typename": "TestType",
"testField": {
"__typename": "TestField",
"field":"thisisatest"
}
}
}},
data
);
}

#[test]
fn test_key_renamer_array() {
let mut data = json!(
{
"data": [{
"__typename": "TestType",
"testField__alias_0": {
"__typename": "TestField",
"field":"thisisatest"
}
}]
}
);

let dr = DataRewrite::KeyRenamer(DataKeyRenamer {
path: "data/testField__alias_0".into(),
rename_key_to: "testField".to_string(),
});

dr.maybe_apply(
&Schema::parse_test(SCHEMA, &Default::default()).unwrap(),
&mut data,
);

assert_eq!(
json! {{
"data": [{
"__typename": "TestType",
"testField": {
"__typename": "TestField",
"field":"thisisatest"
}
}]
}},
data
);
}
}

0 comments on commit af404f2

Please sign in to comment.