Skip to content

Commit 55cdf63

Browse files
committed
graphql: Use execute_root_selection_set for subscriptions
1 parent 7658ed0 commit 55cdf63

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

graphql/src/execution/execution.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ where
172172
pub fn execute_root_selection_set(
173173
ctx: &ExecutionContext<impl Resolver>,
174174
selection_set: &q::SelectionSet,
175+
root_type: &s::ObjectType,
175176
) -> Result<BTreeMap<String, q::Value>, Vec<QueryExecutionError>> {
176177
// Cache the cache key to not have to calculate it twice - once for lookup
177178
// and once for insert.
@@ -193,12 +194,6 @@ pub fn execute_root_selection_set(
193194
}
194195
}
195196

196-
// Obtain the root Query type and fail if there isn't one
197-
let query_type = match sast::get_root_query_type(&ctx.query.schema.document) {
198-
Some(t) => t,
199-
None => return Err(vec![QueryExecutionError::NoRootQueryObjectType]),
200-
};
201-
202197
// Split the toplevel fields into introspection fields and
203198
// regular data fields
204199
let mut data_set = q::SelectionSet {
@@ -210,7 +205,7 @@ pub fn execute_root_selection_set(
210205
items: Vec::new(),
211206
};
212207

213-
for (_, fields) in collect_fields(ctx, query_type, iter::once(selection_set), None) {
208+
for (_, fields) in collect_fields(ctx, root_type, iter::once(selection_set), None) {
214209
let name = fields[0].name.clone();
215210
let selections = fields.into_iter().map(|f| q::Selection::Field(f.clone()));
216211
// See if this is an introspection or data field. We don't worry about
@@ -228,7 +223,7 @@ pub fn execute_root_selection_set(
228223
BTreeMap::default()
229224
} else {
230225
let initial_data = ctx.resolver.prefetch(&ctx, selection_set)?;
231-
execute_selection_set_to_map(&ctx, iter::once(&data_set), query_type, initial_data)?
226+
execute_selection_set_to_map(&ctx, iter::once(&data_set), root_type, initial_data)?
232227
};
233228

234229
// Resolve introspection fields, if there are any
@@ -255,7 +250,7 @@ pub fn execute_root_selection_set(
255250
/// Executes a selection set, requiring the result to be of the given object type.
256251
///
257252
/// Allows passing in a parent value during recursive processing of objects and their fields.
258-
pub fn execute_selection_set<'a>(
253+
fn execute_selection_set<'a>(
259254
ctx: &'a ExecutionContext<impl Resolver>,
260255
selection_sets: impl Iterator<Item = &'a q::SelectionSet>,
261256
object_type: &s::ObjectType,

graphql/src/query/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::time::Instant;
66
use uuid::Uuid;
77

88
use crate::execution::*;
9+
use crate::schema::ast as sast;
910

1011
/// Utilities for working with GraphQL query ASTs.
1112
pub mod ast;
@@ -62,9 +63,15 @@ where
6263
}
6364
let selection_set = selection_set.unwrap_or(&query.selection_set);
6465

66+
// Obtain the root Query type and fail if there isn't one
67+
let query_type = match sast::get_root_query_type(&ctx.query.schema.document) {
68+
Some(t) => t,
69+
None => return Err(vec![QueryExecutionError::NoRootQueryObjectType]),
70+
};
71+
6572
// Execute top-level `query { ... }` and `{ ... }` expressions.
6673
let start = Instant::now();
67-
let result = execute_root_selection_set(&ctx, selection_set);
74+
let result = execute_root_selection_set(&ctx, selection_set, query_type);
6875
if *graph::log::LOG_GQL_TIMING {
6976
info!(
7077
query_logger,

graphql/src/subscription/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,20 +208,14 @@ async fn execute_subscription_event(
208208
// once, from flooding the blocking thread pool and the DB connection pool.
209209
let _permit = SUBSCRIPTION_QUERY_SEMAPHORE.acquire();
210210
let result = graph::spawn_blocking_allow_panic(async move {
211-
let initial_data = ctx.resolver.prefetch(&ctx, &ctx.query.selection_set)?;
212-
execute_selection_set(
213-
&ctx,
214-
iter::once(&ctx.query.selection_set),
215-
&subscription_type,
216-
initial_data,
217-
)
211+
execute_root_selection_set(&ctx, &ctx.query.selection_set, &subscription_type)
218212
})
219213
.await
220214
.map_err(|e| vec![QueryExecutionError::Panic(e.to_string())])
221215
.and_then(|x| x);
222216

223217
match result {
224-
Ok(value) => QueryResult::new(Some(value)),
218+
Ok(value) => QueryResult::new(Some(q::Value::Object(value))),
225219
Err(e) => QueryResult::from(e),
226220
}
227221
}

0 commit comments

Comments
 (0)