grafbase_hooks/hooks/authorization/edge_pre_execution_arguments.rs
1/// Arguments passed to the `authorize_edge_pre_execution` hook.
2pub struct EdgePreExecutionArguments {
3 definition: crate::wit::EdgeDefinition,
4 arguments: String,
5 metadata: String,
6}
7
8impl EdgePreExecutionArguments {
9 pub(crate) fn new(definition: crate::wit::EdgeDefinition, arguments: String, metadata: String) -> Self {
10 Self {
11 definition,
12 arguments,
13 metadata,
14 }
15 }
16
17 /// The name of the parent type of the edge.
18 ///
19 /// For the following GraphQL schema:
20 ///
21 /// ```graphql
22 /// type Query {
23 /// user(id: ID!): User @authorized(arguments: "id")
24 /// }
25 /// ```
26 ///
27 /// The parent type name is `Query`.
28 pub fn parent_type_name(&self) -> &str {
29 &self.definition.parent_type_name
30 }
31
32 /// The name of the authorized edge.
33 ///
34 /// For the following GraphQL schema:
35 ///
36 /// ```graphql
37 /// type Query {
38 /// user(id: ID!): User @authorized(arguments: "id")
39 /// }
40 /// ```
41 ///
42 /// The field name is `user`.
43 pub fn field_name(&self) -> &str {
44 &self.definition.field_name
45 }
46
47 /// The arguments passed to the `@authorized` directive. The arguments are
48 /// serialized as a JSON object. This method will deserialize the arguments
49 /// into either `serde_json::Value` or a custom struct.
50 ///
51 /// For the following GraphQL schema:
52 ///
53 /// ```graphql
54 /// type Query {
55 /// user(id: ID!): User @authorized(arguments: "id")
56 /// }
57 /// ```
58 ///
59 /// When executing a query like:
60 ///
61 /// ```graphql
62 /// query {
63 /// user(id: "123") { id }
64 /// }
65 /// ```
66 ///
67 /// The arguments are `{"id": "123"}`.
68 ///
69 /// The arguments can be deserialized into a custom struct like:
70 ///
71 /// ```rust
72 /// #[derive(serde::Deserialize)]
73 /// struct Arguments {
74 /// id: String,
75 /// }
76 ///
77 /// # fn foo(arguments: grafbase_hooks::EdgePreExecutionArguments) -> Result<(), serde_json::Error> {
78 /// let arguments: Arguments = arguments.arguments()?;
79 /// # Ok(())
80 /// # }
81 /// ```
82 pub fn arguments<'a, T>(&'a self) -> Result<T, serde_json::Error>
83 where
84 T: serde::Deserialize<'a>,
85 {
86 serde_json::from_str(&self.arguments)
87 }
88
89 /// The metadata passed to the `@authorized` directive. The metadata is
90 /// serialized as a JSON object. This method will deserialize the metadata
91 /// into either `serde_json::Value` or a custom struct.
92 ///
93 /// For the following GraphQL schema:
94 ///
95 /// ```graphql
96 /// type Query {
97 /// user(id: ID!): User @authorized(arguments: "id", metadata: { role: "admin" })
98 /// }
99 /// ```
100 ///
101 /// When executing a query like:
102 ///
103 /// ```graphql
104 /// query {
105 /// user(id: "123") { id }
106 /// }
107 /// ```
108 ///
109 /// The metadata is `{"role": "admin"}`.
110 ///
111 /// The metadata can be deserialized into a custom struct like:
112 ///
113 /// ```rust
114 /// #[derive(serde::Deserialize)]
115 /// #[serde(untagged, rename = "snake_case")]
116 /// enum Role {
117 /// Admin,
118 /// User,
119 /// }
120 ///
121 /// #[derive(serde::Deserialize)]
122 /// struct Metadata {
123 /// role: Role,
124 /// }
125 ///
126 /// # fn foo(arguments: grafbase_hooks::EdgePreExecutionArguments) -> Result<(), serde_json::Error> {
127 /// let arguments: Metadata = arguments.metadata()?;
128 /// # Ok(())
129 /// # }
130 /// ```
131 pub fn metadata<'a, T>(&'a self) -> Result<T, serde_json::Error>
132 where
133 T: serde::Deserialize<'a>,
134 {
135 serde_json::from_str(&self.metadata)
136 }
137}