Skip to content

Commit a15b7e0

Browse files
committed
Write end-to-end test for union deserialization
1 parent b66bbeb commit a15b7e0

File tree

7 files changed

+102
-6
lines changed

7 files changed

+102
-6
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ graphql-parser = "0.2.0"
1414
serde = "1.0"
1515
serde_derive = "1.0"
1616

17+
[dev-dependencies]
18+
serde_json = "1.0"
19+
1720
[workspace]
1821
members = [
1922
".",

graphql_query_derive/src/shared.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ pub fn render_object_field(field_name: &str, field_type: TokenStream) -> TokenSt
55
let name_ident = Ident::new(&format!("{}_", field_name), Span::call_site());
66
return quote! {
77
#[serde(rename = #field_name)]
8-
#name_ident: #field_type
8+
pub #name_ident: #field_type
99
};
1010
}
1111

1212
let name_ident = Ident::new(field_name, Span::call_site());
1313

14-
quote!(#name_ident: #field_type)
14+
quote!(pub #name_ident: #field_type)
1515
}

graphql_query_derive/src/unions.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl GqlUnion {
103103
Ok(quote!{
104104
#(#children_definitions)*
105105

106-
#[derive(Deserialize)]
106+
#[derive(Debug, Serialize, Deserialize)]
107107
#[serde(tag = "__typename")]
108108
pub enum #struct_name {
109109
#(#variants),*
@@ -278,10 +278,10 @@ mod tests {
278278
result.unwrap().to_string(),
279279
vec![
280280
"# [ derive ( Debug , Serialize , Deserialize ) ] ",
281-
"pub struct MeowOnUser { first_name : String , } ",
281+
"pub struct MeowOnUser { pub first_name : String , } ",
282+
"# [ derive ( Debug , Serialize , Deserialize ) ] ",
283+
"pub struct MeowOnOrganization { pub title : String , } ",
282284
"# [ derive ( Debug , Serialize , Deserialize ) ] ",
283-
"pub struct MeowOnOrganization { title : String , } ",
284-
"# [ derive ( Deserialize ) ] ",
285285
"# [ serde ( tag = \"__typename\" ) ] ",
286286
"pub enum Meow { User ( MeowOnUser ) , Organization ( MeowOnOrganization ) }",
287287
].into_iter()

tests/union_query.graphql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
query MyQuery {
2+
names {
3+
__typename
4+
...on Dog {
5+
name
6+
}
7+
...on Person {
8+
firstName
9+
lastName
10+
}
11+
...on Organization {
12+
title
13+
}
14+
}
15+
}

tests/union_query.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#[macro_use]
2+
extern crate graphql_client;
3+
#[macro_use]
4+
extern crate serde_derive;
5+
extern crate serde;
6+
extern crate serde_json;
7+
8+
const RESPONSE: &'static str = include_str!("union_query_response.json");
9+
10+
#[derive(GraphQLQuery)]
11+
#[GraphQLQuery(
12+
query_path = "tests/union_query.graphql",
13+
schema_path = "tests/union_schema.graphql",
14+
)]
15+
struct UnionQuery;
16+
17+
#[test]
18+
fn union_query_deserialization() {
19+
let response_data: union_query::ResponseData = serde_json::from_str(RESPONSE).unwrap();
20+
21+
println!("{:?}", response_data);
22+
23+
let expected = r##"ResponseData { names: Some([Some(Person(RustMyQueryNamesOnPerson { firstName: Some("Audrey"), lastName: Some("Lorde") })), Some(Dog(RustMyQueryNamesOnDog { name: Some("Laïka") })), Some(Organization(RustMyQueryNamesOnOrganization { title: Some("Mozilla") })), Some(Dog(RustMyQueryNamesOnDog { name: Some("Norbert") }))]) }"##;
24+
25+
assert_eq!(format!("{:?}", response_data), expected);
26+
27+
assert_eq!(response_data.names.map(|names| names.len()), Some(4));
28+
29+
}

tests/union_query_response.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"names": [
3+
{
4+
"__typename": "Person",
5+
"firstName": "Audrey",
6+
"lastName": "Lorde"
7+
},
8+
{
9+
"__typename": "Dog",
10+
"name": "Laïka"
11+
},
12+
{
13+
"__typename": "Organization",
14+
"title": "Mozilla"
15+
},
16+
{
17+
"__typename": "Dog",
18+
"name": "Norbert"
19+
}
20+
]
21+
}

tests/union_schema.graphql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
schema {
2+
query: UnionQuery
3+
}
4+
5+
type Person {
6+
firstName: String!
7+
lastName: String
8+
birthday: String
9+
}
10+
11+
type Organization {
12+
title: String!
13+
createdAt: String
14+
}
15+
16+
type Dog {
17+
name: String!
18+
"""
19+
Always returns true
20+
"""
21+
isGoodDog: Boolean!
22+
}
23+
24+
union NamedThing = Person | Dog | Organization
25+
26+
type UnionQuery {
27+
names: [NamedThing!]
28+
}

0 commit comments

Comments
 (0)