grafbase_sdk/types/
directive_site.rs

1use serde::Deserialize;
2
3use crate::{wit, SdkError};
4
5/// The site where a directive is applied in the GraphQL schema.
6pub enum DirectiveSite<'a> {
7    /// Directive applied to an object type
8    Object(ObjectDirectiveSite<'a>),
9    /// Directive applied to a field definition
10    FieldDefinition(FieldDefinitionDirectiveSite<'a>),
11    /// Directive applied to an interface
12    Interface(InterfaceDirectiveSite<'a>),
13    /// Directive applied to a union
14    Union(UnionDirectiveSite<'a>),
15}
16
17impl<'a> From<&'a wit::DirectiveSite> for DirectiveSite<'a> {
18    fn from(value: &'a wit::DirectiveSite) -> Self {
19        match value {
20            wit::DirectiveSite::Object(site) => DirectiveSite::Object(site.into()),
21            wit::DirectiveSite::FieldDefinition(site) => DirectiveSite::FieldDefinition(site.into()),
22            wit::DirectiveSite::Interface(site) => DirectiveSite::Interface(site.into()),
23            wit::DirectiveSite::Union(site) => DirectiveSite::Union(site.into()),
24        }
25    }
26}
27
28impl<'a> DirectiveSite<'a> {
29    /// Arguments of the directive with any query data injected. Any argument that depends on
30    /// response data will not be present here and be provided separately.
31    pub fn arguments<T>(&self) -> Result<T, SdkError>
32    where
33        T: Deserialize<'a>,
34    {
35        minicbor_serde::from_slice(match self {
36            DirectiveSite::Object(site) => &site.0.arguments,
37            DirectiveSite::FieldDefinition(site) => &site.0.arguments,
38            DirectiveSite::Interface(site) => &site.0.arguments,
39            DirectiveSite::Union(site) => &site.0.arguments,
40        })
41        .map_err(Into::into)
42    }
43}
44
45/// A directive site for object types
46pub struct ObjectDirectiveSite<'a>(&'a wit::ObjectDirectiveSite);
47
48impl<'a> ObjectDirectiveSite<'a> {
49    /// The name of the object type
50    #[inline]
51    pub fn object_name(&self) -> &str {
52        &self.0.object_name
53    }
54
55    /// Arguments of the directive with any query data injected. Any argument that depends on
56    /// response data will not be present here and be provided separately.
57    pub fn arguments<T>(&self) -> Result<T, SdkError>
58    where
59        T: Deserialize<'a>,
60    {
61        minicbor_serde::from_slice(&self.0.arguments).map_err(Into::into)
62    }
63}
64
65impl<'a> From<&'a wit::ObjectDirectiveSite> for ObjectDirectiveSite<'a> {
66    fn from(value: &'a wit::ObjectDirectiveSite) -> Self {
67        Self(value)
68    }
69}
70
71/// A directive site for field definitions
72pub struct FieldDefinitionDirectiveSite<'a>(&'a wit::FieldDefinitionDirectiveSite);
73
74impl<'a> FieldDefinitionDirectiveSite<'a> {
75    /// The name of the parent type containing this field
76    #[inline]
77    pub fn parent_type_name(&self) -> &str {
78        &self.0.parent_type_name
79    }
80
81    /// The name of the field
82    #[inline]
83    pub fn field_name(&self) -> &str {
84        &self.0.field_name
85    }
86
87    /// Arguments of the directive with any query data injected. Any argument that depends on
88    /// response data will not be present here and be provided separately.
89    pub fn arguments<T>(&self) -> Result<T, SdkError>
90    where
91        T: Deserialize<'a>,
92    {
93        minicbor_serde::from_slice(&self.0.arguments).map_err(Into::into)
94    }
95}
96
97impl<'a> From<&'a wit::FieldDefinitionDirectiveSite> for FieldDefinitionDirectiveSite<'a> {
98    fn from(value: &'a wit::FieldDefinitionDirectiveSite) -> Self {
99        Self(value)
100    }
101}
102
103/// A directive site for union types
104pub struct UnionDirectiveSite<'a>(&'a wit::UnionDirectiveSite);
105
106impl<'a> UnionDirectiveSite<'a> {
107    /// The name of the union type
108    #[inline]
109    pub fn union_name(&self) -> &str {
110        &self.0.union_name
111    }
112
113    /// Arguments of the directive with any query data injected. Any argument that depends on
114    /// response data will not be present here and be provided separately.
115    pub fn arguments<T>(&self) -> Result<T, SdkError>
116    where
117        T: Deserialize<'a>,
118    {
119        minicbor_serde::from_slice(&self.0.arguments).map_err(Into::into)
120    }
121}
122
123impl<'a> From<&'a wit::UnionDirectiveSite> for UnionDirectiveSite<'a> {
124    fn from(value: &'a wit::UnionDirectiveSite) -> Self {
125        Self(value)
126    }
127}
128
129/// A directive site for interface types
130pub struct InterfaceDirectiveSite<'a>(&'a wit::InterfaceDirectiveSite);
131
132impl<'a> InterfaceDirectiveSite<'a> {
133    /// The name of the interface type
134    #[inline]
135    pub fn interface_name(&self) -> &str {
136        &self.0.interface_name
137    }
138
139    /// Arguments of the directive with any query data injected. Any argument that depends on
140    /// response data will not be present here and be provided separately.
141    pub fn arguments<T>(&self) -> Result<T, SdkError>
142    where
143        T: Deserialize<'a>,
144    {
145        minicbor_serde::from_slice(&self.0.arguments).map_err(Into::into)
146    }
147}
148
149impl<'a> From<&'a wit::InterfaceDirectiveSite> for InterfaceDirectiveSite<'a> {
150    fn from(value: &'a wit::InterfaceDirectiveSite) -> Self {
151        Self(value)
152    }
153}