Skip to content

Commit 3c9d65b

Browse files
committed
graphql: Make SelectionSet.items private
1 parent bfdce9b commit 3c9d65b

File tree

5 files changed

+40
-27
lines changed

5 files changed

+40
-27
lines changed

graphql/src/execution/ast.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct FragmentDefinition {
1515
#[derive(Debug, Clone, PartialEq)]
1616
pub struct SelectionSet {
1717
span: (Pos, Pos),
18-
pub items: Vec<Selection>,
18+
items: Vec<Selection>,
1919
}
2020

2121
impl SelectionSet {
@@ -29,6 +29,28 @@ impl SelectionSet {
2929
items: Vec::new(),
3030
}
3131
}
32+
33+
pub fn is_empty(&self) -> bool {
34+
self.items.is_empty()
35+
}
36+
37+
pub fn included(&self) -> impl Iterator<Item = &Selection> {
38+
self.items.iter().filter(|selection| selection.selected())
39+
}
40+
41+
pub fn selections(&self) -> impl Iterator<Item = &Selection> {
42+
self.items.iter()
43+
}
44+
45+
pub fn push(&mut self, field: Field) {
46+
self.items.push(Selection::Field(field))
47+
}
48+
}
49+
50+
impl Extend<Selection> for SelectionSet {
51+
fn extend<T: IntoIterator<Item = Selection>>(&mut self, iter: T) {
52+
self.items.extend(iter)
53+
}
3254
}
3355

3456
#[derive(Debug, Clone, PartialEq)]
@@ -51,7 +73,7 @@ impl Selection {
5173
}
5274

5375
/// Returns true if a selection should be skipped (as per the `@skip` directive).
54-
pub fn skip(&self) -> bool {
76+
fn skip(&self) -> bool {
5577
match self.get_directive("skip") {
5678
Some(directive) => match directive.argument_value("if") {
5779
Some(val) => match val {
@@ -66,7 +88,7 @@ impl Selection {
6688
}
6789

6890
/// Returns true if a selection should be included (as per the `@include` directive).
69-
pub fn include(&self) -> bool {
91+
fn include(&self) -> bool {
7092
match self.get_directive("include") {
7193
Some(directive) => match directive.argument_value("if") {
7294
Some(val) => match val {
@@ -79,6 +101,10 @@ impl Selection {
79101
None => true,
80102
}
81103
}
104+
105+
fn selected(&self) -> bool {
106+
!self.skip() && self.include()
107+
}
82108
}
83109

84110
#[derive(Debug, Clone, PartialEq)]

graphql/src/execution/execution.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -293,25 +293,25 @@ pub fn execute_root_selection_set_uncached(
293293
// non-existent fields; those will cause an error later when we execute
294294
// the data_set SelectionSet
295295
if is_introspection_field(&name) {
296-
intro_set.items.extend(selections)
296+
intro_set.extend(selections)
297297
} else if &name == META_FIELD_NAME {
298298
meta_items.extend(selections)
299299
} else {
300-
data_set.items.extend(selections)
300+
data_set.extend(selections)
301301
}
302302
}
303303

304304
// If we are getting regular data, prefetch it from the database
305-
let mut values = if data_set.items.is_empty() && meta_items.is_empty() {
305+
let mut values = if data_set.is_empty() && meta_items.is_empty() {
306306
Object::default()
307307
} else {
308308
let initial_data = ctx.resolver.prefetch(&ctx, &data_set)?;
309-
data_set.items.extend(meta_items);
309+
data_set.extend(meta_items);
310310
execute_selection_set_to_map(&ctx, iter::once(&data_set), root_type, initial_data)?
311311
};
312312

313313
// Resolve introspection fields, if there are any
314-
if !intro_set.items.is_empty() {
314+
if !intro_set.is_empty() {
315315
let ictx = ctx.as_introspection_context();
316316

317317
values.extend(execute_selection_set_to_map(
@@ -588,13 +588,7 @@ pub fn collect_fields_inner<'a>(
588588
) {
589589
for selection_set in selection_sets {
590590
// Only consider selections that are not skipped and should be included
591-
let selections = selection_set
592-
.items
593-
.iter()
594-
.filter(|selection| !selection.skip())
595-
.filter(|selection| selection.include());
596-
597-
for selection in selections {
591+
for selection in selection_set.included() {
598592
match selection {
599593
a::Selection::Field(ref field) => {
600594
let response_key = field.response_key();

graphql/src/execution/query.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct SelectedFields<'a>(&'a a::SelectionSet);
4949
impl<'a> std::fmt::Display for SelectedFields<'a> {
5050
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
5151
let mut first = true;
52-
for item in &self.0.items {
52+
for item in self.0.selections() {
5353
match item {
5454
a::Selection::Field(field) => {
5555
if !first {
@@ -206,7 +206,7 @@ impl Query {
206206
let mut bcs = HashMap::new();
207207
let mut errors = Vec::new();
208208

209-
for field in self.selection_set.items.iter().filter_map(|sel| match sel {
209+
for field in self.selection_set.selections().filter_map(|sel| match sel {
210210
Field(f) => Some(f),
211211
_ => None,
212212
}) {
@@ -247,7 +247,7 @@ impl Query {
247247
field_error_policy,
248248
)
249249
});
250-
selection_set.items.push(Field(field.clone()));
250+
selection_set.push(field.clone());
251251
if field_error_policy == ErrorPolicy::Deny {
252252
*error_policy = ErrorPolicy::Deny;
253253
}

graphql/src/store/prefetch.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -890,14 +890,7 @@ fn collect_fields_inner<'a>(
890890
}
891891
}
892892

893-
// Only consider selections that are not skipped and should be included
894-
let selections = selection_set
895-
.items
896-
.iter()
897-
.filter(|selection| !selection.skip())
898-
.filter(|selection| selection.include());
899-
900-
for selection in selections {
893+
for selection in selection_set.included() {
901894
match selection {
902895
a::Selection::Field(ref field) => {
903896
let response_key = field.response_key();

graphql/src/store/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ pub fn collect_entities_from_query_field(
327327

328328
// If the query field has a non-empty selection set, this means we
329329
// need to recursively process it
330-
for selection in field.selection_set.items.iter() {
330+
for selection in field.selection_set.selections() {
331331
if let a::Selection::Field(sub_field) = selection {
332332
queue.push_back((object_type, sub_field))
333333
}

0 commit comments

Comments
 (0)