libgraphql_core/operation/
selection_set.rs

1use crate::operation::FieldSelection;
2use crate::operation::FragmentRegistry;
3use crate::operation::SelectionSetBuilder;
4use crate::operation::Selection;
5use crate::schema::Schema;
6
7#[derive(Clone, Debug, PartialEq)]
8pub struct SelectionSet<'schema> {
9    pub(super) selections: Vec<Selection<'schema>>,
10    pub(super) schema: &'schema Schema,
11}
12impl<'schema> SelectionSet<'schema> {
13    pub fn builder<'fragreg>(
14        schema: &'schema Schema,
15        fragment_registry: &'fragreg FragmentRegistry<'schema>,
16    ) -> SelectionSetBuilder<'schema, 'fragreg> {
17        SelectionSetBuilder::new(schema, fragment_registry)
18    }
19
20    pub fn selected_fields(
21        &'schema self,
22        fragment_registry: &'schema FragmentRegistry<'schema>,
23    ) -> Box<dyn Iterator<Item = &'schema FieldSelection<'schema>> + 'schema> {
24        Box::new(
25            self.selections()
26                .iter()
27                .flat_map(|selection: &Selection<'schema>| match selection {
28                    Selection::Field(field_selection) =>
29                        Box::new(vec![field_selection].into_iter()),
30
31                    Selection::FragmentSpread(frag_spread) => {
32                        frag_spread.fragment(fragment_registry)
33                            .selection_set()
34                            .selected_fields(fragment_registry)
35                    },
36
37                    Selection::InlineFragment(inline_frag) =>
38                        inline_frag.selection_set()
39                            .selected_fields(fragment_registry),
40                })
41        )
42    }
43
44    pub fn selections(&self) -> &Vec<Selection<'schema>> {
45        &self.selections
46    }
47}
48