Handle list merging

This commit is contained in:
Carlos Diaz-Padron 2018-02-18 16:45:31 -08:00 committed by theduke
parent 509a6f12b9
commit 63d8a3d1a0
2 changed files with 74 additions and 12 deletions

View file

@ -203,12 +203,22 @@ mod merge_parallel_inline_fragments {
use types::scalars::EmptyMutation;
struct Type;
struct Other;
graphql_object!(Type: () |&self| {
field a() -> &str { "Apple" }
field b() -> &str { "Banana" }
field c() -> &str { "Cherry" }
field deep() -> Type { Type }
field other() -> Vec<Other> { vec![Other, Other] }
});
graphql_object!(Other: () |&self| {
field a() -> &str { "Apple" }
field b() -> &str { "Banana" }
field c() -> &str { "Cherry" }
field deep() -> Type { Type }
field other() -> Vec<Other> { vec![Other, Other] }
});
#[test]
@ -218,15 +228,24 @@ mod merge_parallel_inline_fragments {
{ a, ...FragOne }
fragment FragOne on Type {
b
deep {
deep: deep {
b
deeper: deep { b }
deeper: other {
deepest: deep {
b
}
}
... on Type {
c
deeper: deep { c }
deeper: other {
deepest: deep {
c
}
}
}
}
c
}";
@ -251,6 +270,12 @@ mod merge_parallel_inline_fragments {
("b", Value::string("Banana")),
(
"deeper",
Value::list(
vec![
Value::object(
vec![
(
"deepest",
Value::object(
vec![
("b", Value::string("Banana")),
@ -259,6 +284,25 @@ mod merge_parallel_inline_fragments {
.collect(),
),
),
].into_iter().collect()
),
Value::object(
vec![
(
"deepest",
Value::object(
vec![
("b", Value::string("Banana")),
("c", Value::string("Cherry")),
].into_iter()
.collect(),
),
),
].into_iter().collect()
),
]
),
),
("c", Value::string("Cherry")),
].into_iter()
.collect(),

View file

@ -505,11 +505,29 @@ fn is_excluded(directives: &Option<Vec<Spanning<Directive>>>, vars: &Variables)
fn merge_key_into(result: &mut OrderMap<String, Value>, response_name: &str, value: Value) {
match result.entry(response_name.to_owned()) {
Entry::Occupied(mut e) => match (e.get_mut().as_mut_object_value(), value) {
(Some(dest_obj), Value::Object(src_obj)) => {
Entry::Occupied(mut e) => {
match e.get_mut() {
&mut Value::Object(ref mut dest_obj) => {
if let Value::Object(src_obj) = value {
merge_maps(dest_obj, src_obj);
}
},
&mut Value::List(ref mut dest_list) => {
if let Value::List(src_list) = value {
dest_list.iter_mut().zip(src_list.into_iter()).for_each(|(d, s)| {
match d {
&mut Value::Object(ref mut d_obj) => {
if let Value::Object(s_obj) = s {
merge_maps(d_obj, s_obj);
}
},
_ => {},
}
});
}
},
_ => {}
}
},
Entry::Vacant(e) => {
e.insert(value);