libgraphql_core/operation/
fragment.rs1use crate::DirectiveAnnotation;
2use crate::loc;
3use crate::named_ref::DerefByName;
4use crate::named_ref::DerefByNameError;
5use crate::named_ref::NamedRef;
6use crate::operation::FragmentRegistry;
7use crate::operation::SelectionSet;
8use crate::schema::Schema;
9use crate::types::NamedGraphQLTypeRef;
10
11#[derive(Clone, Debug, PartialEq)]
13pub struct Fragment<'schema> {
14 pub(super) def_location: loc::SourceLocation,
15 pub(super) directives: Vec<DirectiveAnnotation>,
16 pub(super) name: String,
17 pub(super) schema: &'schema Schema,
18 pub(super) selection_set: SelectionSet<'schema>,
19 pub(super) type_condition_ref: NamedGraphQLTypeRef,
20}
21
22impl<'schema> Fragment<'schema> {
23 pub fn selection_set(&self) -> &SelectionSet<'schema> {
24 &self.selection_set
25 }
26}
27
28impl<'schema> DerefByName for Fragment<'schema> {
29 type Source = FragmentRegistry<'schema>;
30 type RefLocation = loc::SourceLocation;
31
32 fn deref_name<'a>(
33 source: &'a Self::Source,
34 name: &str,
35 ) -> std::result::Result<&'a Fragment<'schema>, DerefByNameError> {
36 source.fragments.get(name).ok_or_else(
37 || DerefByNameError::DanglingReference(name.to_string()),
38 )
39 }
40}
41
42pub type FragmentRef<'schema> = NamedRef<
43 FragmentRegistry<'schema>,
44 loc::SourceLocation,
45 Fragment<'schema>,
46>;