Skip to content

Commit 27ae522

Browse files
committed
Test subscriptions, fix ResponseData
ResponseData did not convert root fields to camel case when {ø,de}serializing.
1 parent 7a33496 commit 27ae522

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

graphql_query_derive/src/schema.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl Schema {
137137
let prefix = format!("RUST_{}", prefix);
138138
let selection = Selection::from(&q.selection_set);
139139

140-
if selection.0.len() > 0 {
140+
if selection.0.len() > 1 {
141141
Err(format_err!(
142142
"{}",
143143
::constants::MULTIPLE_SUBSCRIPTION_FIELDS_ERROR
@@ -216,6 +216,7 @@ impl Schema {
216216
#variables_struct
217217

218218
#[derive(Debug, Serialize, Deserialize)]
219+
#[serde(rename_all = "camelCase")]
219220
pub struct ResponseData {
220221
#(#response_data_fields)*,
221222
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
subscription Birthdays($filter: String) {
2+
dogBirthdays(filter: $filter) {
3+
name
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"dogBirthdays": [
3+
{
4+
"name": "Maya"
5+
},
6+
{
7+
"name": "Norbert"
8+
},
9+
{
10+
"name": "Strelka"
11+
},
12+
{
13+
"name": "Belka"
14+
}
15+
]
16+
}

tests/subscription/subscription_schema.graphql

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ type SimpleMutation {
1313
}
1414

1515
type SimpleSubscription {
16-
newDogs: Dog
17-
dogBirthdays(filter: String): DogBirthday
16+
newDogs: [Dog]
17+
dogBirthdays(filter: String): [DogBirthday!]
1818
}
1919

2020
type DogBirthday {
21+
name: String
2122
date: String
2223
age: Int
2324
treats: [String]

tests/subscriptions.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ extern crate serde_derive;
55
extern crate serde;
66
extern crate serde_json;
77

8-
// If you comment this out, it will not compile because the query is not valid. We need to investigate how we can make this a real test.
8+
const RESPONSE: &str = include_str!("subscription/subscription_query_response.json");
9+
10+
// If you uncomment this, it will not compile because the query is not valid. We need to investigate how we can make this a real test.
911
//
1012
// #[derive(GraphQLQuery)]
1113
// #[graphql(
@@ -14,7 +16,23 @@ extern crate serde_json;
1416
// )]
1517
// struct SubscriptionInvalidQuery;
1618

19+
#[derive(GraphQLQuery)]
20+
#[graphql(
21+
schema_path = "tests/subscription/subscription_schema.graphql",
22+
query_path = "tests/subscription/subscription_query.graphql",
23+
)]
24+
struct SubscriptionQuery;
25+
1726
#[test]
1827
fn subscriptions_work() {
19-
unimplemented!("subscriptions test");
28+
let response_data: subscription_query::ResponseData = serde_json::from_str(RESPONSE).unwrap();
29+
30+
let expected = r##"ResponseData { dog_birthdays: Some([RustBirthdaysDogBirthdays { name: Some("Maya") }, RustBirthdaysDogBirthdays { name: Some("Norbert") }, RustBirthdaysDogBirthdays { name: Some("Strelka") }, RustBirthdaysDogBirthdays { name: Some("Belka") }]) }"##;
31+
32+
assert_eq!(format!("{:?}", response_data), expected);
33+
34+
assert_eq!(
35+
response_data.dog_birthdays.map(|birthdays| birthdays.len()),
36+
Some(4)
37+
);
2038
}

0 commit comments

Comments
 (0)