Skip to content

Commit 03d8eca

Browse files
committed
Fix fragments on unions
1 parent bfb27f0 commit 03d8eca

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

graphql_client/tests/union_query.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ const RESPONSE: &'static str = include_str!("unions/union_query_response.json");
1515
)]
1616
pub struct UnionQuery;
1717

18+
#[derive(GraphQLQuery)]
19+
#[graphql(
20+
query_path = "tests/unions/union_query.graphql",
21+
schema_path = "tests/unions/union_schema.graphql",
22+
response_derives = "PartialEq, Debug"
23+
)]
24+
pub struct FragmentOnUnion;
25+
1826
#[test]
1927
fn union_query_deserialization() {
2028
let response_data: union_query::ResponseData = serde_json::from_str(RESPONSE).unwrap();
@@ -41,3 +49,28 @@ fn union_query_deserialization() {
4149

4250
assert_eq!(response_data.names.map(|names| names.len()), Some(4));
4351
}
52+
53+
#[test]
54+
fn fragment_on_union() {
55+
let response_data: fragment_on_union::ResponseData = serde_json::from_str(RESPONSE).unwrap();
56+
57+
let expected = fragment_on_union::ResponseData {
58+
names: Some(vec![
59+
fragment_on_union::MyQueryNames::Person(fragment_on_union::MyQueryNamesOnPerson {
60+
first_name: "Audrey".to_string(),
61+
last_name: Some("Lorde".to_string()),
62+
}),
63+
fragment_on_union::MyQueryNames::Dog(fragment_on_union::MyQueryNamesOnDog {
64+
name: "Laïka".to_string(),
65+
}),
66+
fragment_on_union::MyQueryNames::Organization(fragment_on_union::MyQueryNamesOnOrganization {
67+
title: "Mozilla".to_string(),
68+
}),
69+
fragment_on_union::MyQueryNames::Dog(fragment_on_union::MyQueryNamesOnDog {
70+
name: "Norbert".to_string(),
71+
}),
72+
]),
73+
};
74+
75+
assert_eq!(response_data, expected);
76+
}

graphql_client/tests/unions/union_query.graphql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,23 @@ query MyQuery {
1313
}
1414
}
1515
}
16+
17+
fragment NamesFragment on NamedThing {
18+
__typename
19+
... on Dog {
20+
name
21+
}
22+
... on Person {
23+
firstName
24+
}
25+
... on Organization {
26+
title
27+
}
28+
}
29+
30+
query FragmentOnUnion {
31+
names {
32+
__typename
33+
...NamesFragment
34+
}
35+
}

0 commit comments

Comments
 (0)