grafbase_hooks/hooks/authorization/
node_pre_execution_arguments.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/// Arguments passed to the `authorize_node_pre_execution` hook.
pub struct NodePreExecutionArguments {
    definition: crate::wit::NodeDefinition,
    metadata: String,
}

impl NodePreExecutionArguments {
    pub(crate) fn new(definition: crate::wit::NodeDefinition, metadata: String) -> Self {
        Self { definition, metadata }
    }

    /// The name of the node type.
    ///
    /// For the following GraphQL schema:
    ///
    /// ```graphql
    /// type User @authorized {
    ///     id: Int!
    ///     name: String!
    /// }
    ///
    /// type Query {
    ///    user(id: ID!): User
    /// }
    /// ```
    ///
    /// The node type name is `User`.
    pub fn type_name(&self) -> &str {
        &self.definition.type_name
    }

    /// The metadata passed to the `@authorized` directive. The metadata is
    /// serialized as a JSON object. This method will deserialize the metadata
    /// into either `serde_json::Value` or a custom struct.
    ///
    /// For the following GraphQL schema:
    ///
    /// ```graphql
    /// type User @authorized(metadata: { role: "admin" }) {
    ///     id: Int!
    ///     name: String!
    /// }
    ///
    /// type Query {
    ///    user(id: ID!): User
    /// }
    /// ```
    ///
    /// When executing a query like:
    ///
    /// ```graphql
    /// query {
    ///   user(id: "123") { id }
    /// }
    /// ```
    ///
    /// The metadata is `{"role": "admin"}`.
    ///
    /// The metadata can be deserialized into a custom struct:
    ///
    /// ```rust
    /// #[derive(serde::Deserialize)]
    /// #[serde(untagged, rename = "snake_case")]
    /// enum Role {
    ///    Admin,
    ///    User,
    /// }
    ///
    /// #[derive(serde::Deserialize)]
    /// struct Metadata {
    ///    role: Role,
    /// }
    ///
    /// # fn foo(arguments: grafbase_hooks::EdgePreExecutionArguments) -> Result<(), serde_json::Error> {
    /// let arguments: Metadata = arguments.deserialize_metadata()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn deserialize_metadata<T: serde::de::DeserializeOwned>(&self) -> Result<T, serde_json::Error> {
        serde_json::from_str(&self.metadata)
    }
}