Skip to content

Commit 292319a

Browse files
committed
graphql: Fix aliased scalar fields
1 parent 8d98808 commit 292319a

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

core/tests/interfaces.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Tests for graphql interfaces.
22

33
use graph::prelude::*;
4+
use graph_graphql::prelude::object;
45
use test_store::*;
56

67
// `entities` is `(entity, type)`.
@@ -554,3 +555,48 @@ fn invalid_fragment() {
554555
e => panic!("error {} is not the expected one", e),
555556
}
556557
}
558+
559+
#[test]
560+
fn alias() {
561+
let subgraph_id = "Alias";
562+
let schema = "interface Legged { id: ID!, legs: Int! }
563+
type Animal implements Legged @entity {
564+
id: ID!
565+
legs: Int!
566+
parent: Legged
567+
}";
568+
569+
let query = "query { l: legged(id: \"child\") { ... on Animal { p: parent { i: id, t: __typename } } } }";
570+
571+
let parent = (
572+
Entity::from(vec![
573+
("id", Value::from("parent")),
574+
("legs", Value::from(4)),
575+
("parent", Value::Null),
576+
]),
577+
"Animal",
578+
);
579+
let child = (
580+
Entity::from(vec![
581+
("id", Value::from("child")),
582+
("legs", Value::from(3)),
583+
("parent", Value::String("parent".into())),
584+
]),
585+
"Animal",
586+
);
587+
588+
let res = insert_and_query(subgraph_id, schema, vec![parent, child], query).unwrap();
589+
590+
assert!(res.errors.is_none(), format!("{:#?}", res.errors));
591+
assert_eq!(
592+
res.data.unwrap(),
593+
object! {
594+
l: object! {
595+
p: object! {
596+
i: "parent",
597+
t: "Animal"
598+
}
599+
}
600+
}
601+
)
602+
}

graphql/src/execution/execution.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,10 @@ fn execute_selection_set_to_map(
303303
let field_value = prefetched_object
304304
.as_mut()
305305
.map(|o| {
306+
// Prefetched objects associated to `prefetch:response_key`,
307+
// while scalars are associated to the field name.
306308
o.remove(&format!("prefetch:{}", response_key))
307-
.or(o.remove(response_key))
309+
.or(o.remove(&fields[0].name))
308310
})
309311
.flatten();
310312
match execute_field(&ctx, object_type, field_value, &fields[0], field, fields) {

0 commit comments

Comments
 (0)