Skip to content

Commit 34a8d33

Browse files
committed
graphql: Restrict construction of selection sets
1 parent f7faf0e commit 34a8d33

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

graphql/src/execution/ast.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,23 @@ pub struct FragmentDefinition {
1414

1515
#[derive(Debug, Clone, PartialEq)]
1616
pub struct SelectionSet {
17-
pub span: (Pos, Pos),
17+
span: (Pos, Pos),
1818
pub items: Vec<Selection>,
1919
}
2020

21+
impl SelectionSet {
22+
pub fn new(span: (Pos, Pos), items: Vec<Selection>) -> Self {
23+
SelectionSet { span, items }
24+
}
25+
26+
pub fn empty_from(other: &SelectionSet) -> Self {
27+
SelectionSet {
28+
span: other.span.clone(),
29+
items: Vec::new(),
30+
}
31+
}
32+
}
33+
2134
#[derive(Debug, Clone, PartialEq)]
2235
pub enum Selection {
2336
Field(Field),

graphql/src/execution/execution.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,8 @@ pub fn execute_root_selection_set_uncached(
282282
) -> Result<Object, Vec<QueryExecutionError>> {
283283
// Split the top-level fields into introspection fields and
284284
// regular data fields
285-
let mut data_set = a::SelectionSet {
286-
span: selection_set.span,
287-
items: Vec::new(),
288-
};
289-
let mut intro_set = a::SelectionSet {
290-
span: selection_set.span,
291-
items: Vec::new(),
292-
};
285+
let mut data_set = a::SelectionSet::empty_from(selection_set);
286+
let mut intro_set = a::SelectionSet::empty_from(selection_set);
293287
let mut meta_items = Vec::new();
294288

295289
for (_, fields) in collect_fields(ctx, root_type, iter::once(selection_set)) {

graphql/src/execution/query.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,7 @@ impl Query {
243243

244244
let (selection_set, error_policy) = bcs.entry(bc).or_insert_with(|| {
245245
(
246-
a::SelectionSet {
247-
span: self.selection_set.span,
248-
items: vec![],
249-
},
246+
a::SelectionSet::empty_from(&self.selection_set),
250247
field_error_policy,
251248
)
252249
});
@@ -748,7 +745,7 @@ impl RawQuery {
748745
}
749746
})
750747
.collect::<Result<Vec<_>, _>>()?;
751-
Ok(a::SelectionSet { span, items })
748+
Ok(a::SelectionSet::new(span, items))
752749
}
753750

754751
fn expand_directives(

0 commit comments

Comments
 (0)