bluejay_visibility/
interface_type_definition.rs

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