grafbase_sdk/types/
directive.rs

1use serde::Deserialize;
2
3use crate::{SdkError, cbor, wit};
4
5use super::FieldDefinitionDirectiveSite;
6
7/// The directive and its arguments which define the extension in the GraphQL SDK.
8pub struct SchemaDirective<'a> {
9    pub(crate) subgraph_name: &'a str,
10    pub(crate) directive: &'a wit::Directive,
11}
12
13impl<'a> SchemaDirective<'a> {
14    /// The name of the directive.
15    #[inline]
16    pub fn name(&self) -> &'a str {
17        &self.directive.name
18    }
19
20    /// The name of the subgraph this directive is part of.
21    #[inline]
22    pub fn subgraph_name(&self) -> &'a str {
23        self.subgraph_name
24    }
25
26    /// The directive arguments. The output is a Serde structure, that must map to
27    /// the arguments of the directive.
28    ///
29    /// Error is returned if the directive argument does not match the output structure.
30    #[inline]
31    pub fn arguments<T>(&'a self) -> Result<T, SdkError>
32    where
33        T: Deserialize<'a>,
34    {
35        cbor::from_slice(&self.directive.arguments).map_err(Into::into)
36    }
37}
38
39/// A field definition directive with its site information
40pub struct FieldDefinitionDirective<'a>(&'a wit::FieldDefinitionDirective);
41
42impl<'a> FieldDefinitionDirective<'a> {
43    /// The name of the directive
44    #[inline]
45    pub fn name(&self) -> &'a str {
46        &self.0.name
47    }
48
49    /// Arguments of the directive with any query data injected. Any argument that depends on
50    /// response data will not be present here and be provided separately.
51    pub fn arguments<T>(&self) -> Result<T, SdkError>
52    where
53        T: Deserialize<'a>,
54    {
55        minicbor_serde::from_slice(&self.0.arguments).map_err(Into::into)
56    }
57
58    /// Deserialize the arguments of the directive using a `DeserializeSeed`.
59    pub fn arguments_seed<T>(&self, seed: T) -> Result<T::Value, SdkError>
60    where
61        T: serde::de::DeserializeSeed<'a>,
62    {
63        let mut deserializer = minicbor_serde::Deserializer::new(&self.0.arguments);
64        seed.deserialize(&mut deserializer).map_err(From::from)
65    }
66
67    ///Serialized arguments as sent by the host. There is no guarantee on the bytes.
68    pub fn arguments_bytes(&self) -> &[u8] {
69        &self.0.arguments
70    }
71
72    /// The site information for this directive
73    #[inline]
74    pub fn site(&self) -> FieldDefinitionDirectiveSite<'a> {
75        (&self.0.site).into()
76    }
77}
78
79impl<'a> From<&'a wit::FieldDefinitionDirective> for FieldDefinitionDirective<'a> {
80    fn from(value: &'a wit::FieldDefinitionDirective) -> Self {
81        Self(value)
82    }
83}