grafbase_sdk/types/
directive.rs

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