Skip to content

Commit b8a3b35

Browse files
committed
(WIP) Subscriptions support
For now this is mostly about cleaning up how we handle the operations from the query. This paves the way for validating that we only have one field in a subscription (as per the spec) and for selecting an operation for a multi-operation document later.
1 parent 6a425cb commit b8a3b35

File tree

5 files changed

+35
-18
lines changed

5 files changed

+35
-18
lines changed

graphql_query_derive/src/constants.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ pub fn typename_field() -> GqlObjectField {
2222
type_: FieldType::Named(string_type()),
2323
}
2424
}
25+
26+
pub const MULTIPLE_SUBSCRIPTION_FIELDS_ERROR: &str = r##"
27+
Multiple-field queries on the root subscription field are forbidden by the spec.
28+
29+
See: https://github.com/facebook/graphql/blob/master/spec/Section%205%20--%20Validation.md#subscription-operation-definitions
30+
"##;

graphql_query_derive/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod inputs;
2424
mod interfaces;
2525
mod introspection_response;
2626
mod objects;
27+
mod operations;
2728
mod query;
2829
mod scalars;
2930
mod schema;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use selection::Selection;
2+
3+
pub enum OperationType {
4+
Query,
5+
Mutation,
6+
Subscription,
7+
}
8+
9+
#[derive(Debug)]
10+
pub struct Operation {
11+
pub name: String,
12+
pub selection: Selection,
13+
}
14+
15+
pub struct Operations(Vec<Operation>);
16+
17+
impl Operations {
18+
fn from_document(doc: ::graphql_parser::query::Document) -> Result<Self, ::failure::Error> {
19+
unimplemented!()
20+
}
21+
}

graphql_query_derive/src/query.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ use variables::Variable;
99

1010
/// This holds all the information we need during the code generation phase.
1111
pub struct QueryContext {
12-
pub _subscription_root: Option<Vec<TokenStream>>,
12+
pub root: Option<Vec<TokenStream>>,
1313
pub fragments: BTreeMap<String, GqlFragment>,
14-
pub mutation_root: Option<Vec<TokenStream>>,
15-
pub query_root: Option<Vec<TokenStream>>,
1614
pub schema: Schema,
1715
pub variables: Vec<Variable>,
1816
}
@@ -21,10 +19,8 @@ impl QueryContext {
2119
/// Create a QueryContext with the given Schema.
2220
pub fn new(schema: Schema) -> QueryContext {
2321
QueryContext {
24-
_subscription_root: None,
22+
root: None,
2523
fragments: BTreeMap::new(),
26-
mutation_root: None,
27-
query_root: None,
2824
schema,
2925
variables: Vec::new(),
3026
}
@@ -72,10 +68,8 @@ impl QueryContext {
7268
#[cfg(test)]
7369
pub fn new_empty() -> QueryContext {
7470
QueryContext {
75-
_subscription_root: None,
7671
fragments: BTreeMap::new(),
77-
mutation_root: None,
78-
query_root: None,
72+
root: None,
7973
schema: Schema::new(),
8074
variables: Vec::new(),
8175
}

graphql_query_derive/src/schema.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Schema {
7878
for definition in query.definitions {
7979
match definition {
8080
query::Definition::Operation(query::OperationDefinition::Query(q)) => {
81-
context.query_root = {
81+
context.root = {
8282
let definition = context
8383
.schema
8484
.query_type
@@ -101,7 +101,7 @@ impl Schema {
101101
context.register_variables(&q.variable_definitions);
102102
}
103103
query::Definition::Operation(query::OperationDefinition::Mutation(q)) => {
104-
context.mutation_root = {
104+
context.root = {
105105
let definition = context
106106
.schema
107107
.mutation_type
@@ -124,7 +124,7 @@ impl Schema {
124124
context.register_variables(&q.variable_definitions);
125125
}
126126
query::Definition::Operation(query::OperationDefinition::Subscription(q)) => {
127-
context._subscription_root = {
127+
context.root = {
128128
let definition = context
129129
.schema
130130
.subscription_type
@@ -173,12 +173,7 @@ impl Schema {
173173
.collect();
174174
let fragment_definitions = fragment_definitions?;
175175
let variables_struct = context.expand_variables();
176-
let response_data_fields = context
177-
.query_root
178-
.as_ref()
179-
.or_else(|| context.mutation_root.as_ref())
180-
.or_else(|| context._subscription_root.as_ref())
181-
.expect("no selection defined");
176+
let response_data_fields = context.root.as_ref().expect("no selection defined");
182177

183178
let input_object_definitions: Result<Vec<TokenStream>, _> = context
184179
.schema

0 commit comments

Comments
 (0)