bluejay_visibility/
field_definition.rs

1use crate::{ArgumentsDefinition, Cache, Directives, OutputType, Warden};
2use bluejay_core::definition::{self, HasDirectives, SchemaDefinition};
3
4use once_cell::unsync::OnceCell;
5
6pub struct FieldDefinition<'a, S: SchemaDefinition, W: Warden<SchemaDefinition = S>> {
7    inner: &'a S::FieldDefinition,
8    cache: &'a Cache<'a, S, W>,
9    r#type: OutputType<'a, S, W>,
10    arguments_definition: OnceCell<Option<ArgumentsDefinition<'a, S, W>>>,
11    directives: Option<Directives<'a, S, W>>,
12}
13
14impl<'a, S: SchemaDefinition, W: Warden<SchemaDefinition = S>> FieldDefinition<'a, S, W> {
15    pub(crate) fn new(inner: &'a S::FieldDefinition, cache: &'a Cache<'a, S, W>) -> Option<Self> {
16        cache
17            .warden()
18            .is_field_definition_visible(inner)
19            .then(|| {
20                OutputType::new(definition::FieldDefinition::r#type(inner), cache).map(|r#type| {
21                    Self {
22                        inner,
23                        cache,
24                        r#type,
25                        arguments_definition: OnceCell::new(),
26                        directives: inner.directives().map(|d| Directives::new(d, cache)),
27                    }
28                })
29            })
30            .flatten()
31    }
32
33    pub fn inner(&self) -> &'a S::FieldDefinition {
34        self.inner
35    }
36}
37
38impl<'a, S: SchemaDefinition + 'a, W: Warden<SchemaDefinition = S>> definition::FieldDefinition
39    for FieldDefinition<'a, S, W>
40{
41    type OutputType = OutputType<'a, S, W>;
42    type ArgumentsDefinition = ArgumentsDefinition<'a, S, W>;
43
44    fn description(&self) -> Option<&str> {
45        self.inner.description()
46    }
47
48    fn name(&self) -> &str {
49        self.inner.name()
50    }
51
52    fn is_builtin(&self) -> bool {
53        self.inner.is_builtin()
54    }
55
56    fn r#type(&self) -> &Self::OutputType {
57        &self.r#type
58    }
59
60    fn arguments_definition(&self) -> Option<&Self::ArgumentsDefinition> {
61        self.arguments_definition
62            .get_or_init(|| {
63                self.inner
64                    .arguments_definition()
65                    .map(|ad| ArgumentsDefinition::new(ad, self.cache))
66            })
67            .as_ref()
68    }
69}
70
71impl<'a, S: SchemaDefinition + 'a, W: Warden<SchemaDefinition = S>> HasDirectives
72    for FieldDefinition<'a, S, W>
73{
74    type Directives = Directives<'a, S, W>;
75
76    fn directives(&self) -> Option<&Self::Directives> {
77        self.directives.as_ref()
78    }
79}