libgraphql_core/operation/
field_selection.rs

1use crate::types::Field;
2use crate::DirectiveAnnotation;
3use crate::loc;
4use crate::operation::SelectionSet;
5use crate::schema::Schema;
6use crate::Value;
7use indexmap::IndexMap;
8
9#[derive(Clone, Debug, PartialEq)]
10pub struct FieldSelection<'schema> {
11    pub(super) alias: Option<String>,
12    pub(super) arguments: IndexMap<String, Value>,
13    pub(super) def_location: loc::SourceLocation,
14    pub(super) directives: Vec<DirectiveAnnotation>,
15    pub(super) field: &'schema Field,
16    pub(super) schema: &'schema Schema,
17    pub(super) selection_set: Option<SelectionSet<'schema>>,
18}
19impl<'schema> FieldSelection<'schema> {
20    pub fn alias(&self) -> Option<&str> {
21        self.alias.as_deref()
22    }
23
24    pub fn arguments(&self) -> &IndexMap<String, Value> {
25        &self.arguments
26    }
27
28    pub fn def_location(&self) -> &loc::SourceLocation {
29        &self.def_location
30    }
31
32    pub fn directives(&self) -> &Vec<DirectiveAnnotation> {
33        &self.directives
34    }
35
36    pub fn field(&self) -> &'schema Field {
37        self.field
38    }
39
40    /**
41     * If an alias was specified for this selection, return the alias.
42     * Otherwise return the name of the field.
43     */
44    pub fn selected_name(&self) -> &str {
45        self.alias().unwrap_or_else(|| self.field().name())
46    }
47
48    pub fn selection_set(&self) -> Option<&SelectionSet<'schema>> {
49        self.selection_set.as_ref()
50    }
51}