Skip to content

Commit 7a33496

Browse files
committed
Forbid subscription queries with more than 1 root field
As per spec. See error message in the commit.
1 parent b8a3b35 commit 7a33496

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

graphql_query_derive/src/schema.rs

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

140+
if selection.0.len() > 0 {
141+
Err(format_err!(
142+
"{}",
143+
::constants::MULTIPLE_SUBSCRIPTION_FIELDS_ERROR
144+
))?
145+
}
146+
140147
definitions.extend(
141148
definition.field_impls_for_selection(&context, &selection, &prefix)?,
142149
);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
subscription InvalidSubscription($filter: String) {
2+
newDogs {
3+
name
4+
}
5+
dogBirthdays(filter: $filter) {
6+
name
7+
}
8+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
schema {
2+
query: SimpleQuery
3+
mutation: SimpleMutation
4+
subscription: SimpleSubscription
5+
}
6+
7+
type SimpleQuery {
8+
dogByName(name: String): Dog
9+
}
10+
11+
type SimpleMutation {
12+
petDog(dogName: String): Dog
13+
}
14+
15+
type SimpleSubscription {
16+
newDogs: Dog
17+
dogBirthdays(filter: String): DogBirthday
18+
}
19+
20+
type DogBirthday {
21+
date: String
22+
age: Int
23+
treats: [String]
24+
}
25+
26+
type Dog {
27+
name: String!
28+
"""
29+
Always returns true
30+
"""
31+
isGoodDog: Boolean!
32+
}

tests/subscriptions.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
// 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.
9+
//
10+
// #[derive(GraphQLQuery)]
11+
// #[graphql(
12+
// schema_path = "tests/subscription/subscription_schema.graphql",
13+
// query_path = "tests/subscription/subscription_invalid_query.graphql"
14+
// )]
15+
// struct SubscriptionInvalidQuery;
16+
17+
#[test]
18+
fn subscriptions_work() {
19+
unimplemented!("subscriptions test");
20+
}

0 commit comments

Comments
 (0)