grafbase_hooks/hooks/authorization/edge_post_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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
/// Arguments passed to the `authorize_edge_post_execution` hook.
pub struct EdgePostExecutionArguments {
definition: crate::wit::EdgeDefinition,
edges: Vec<(String, Vec<String>)>,
metadata: String,
}
impl EdgePostExecutionArguments {
pub(crate) fn new(
definition: crate::wit::EdgeDefinition,
edges: Vec<(String, Vec<String>)>,
metadata: String,
) -> Self {
Self {
definition,
edges,
metadata,
}
}
/// The name of the parent type of the edge.
///
/// For the following GraphQL schema:
///
/// ```graphql
/// type Address {
/// street: String!
/// }
///
/// type User {
/// id: Int!
/// addresses: [Address!]! @authorized(fields: "id", node: "street")
/// }
///
/// type Query {
/// users: [User!]!
/// }
/// ```
///
/// And the query:
///
/// ```graphql
/// query {
/// users { addresses { street } }
/// }
/// ```
///
/// The parent type name is `User`.
pub fn parent_type_name(&self) -> &str {
&self.definition.parent_type_name
}
/// The name of the authorized edge.
///
/// For the following GraphQL schema:
///
/// ```graphql
/// type Address {
/// street: String!
/// }
///
/// type User {
/// id: Int!
/// addresses: [Address!]! @authorized(fields: "id", node: "street")
/// }
///
/// type Query {
/// users: [User!]!
/// }
/// ```
///
/// And the query:
///
/// ```graphql
/// query {
/// users { addresses { street } }
/// }
/// ```
///
/// The field name is `addresses`.
pub fn field_name(&self) -> &str {
&self.definition.field_name
}
/// The returned edges, serialized as a JSON objects. The first item of the tuple
/// is a serialization of the fields defined in the `fields` argument and the second
/// one is a serialization of the fields defined in the `node` argument.
///
/// This method will deserialize the parent nodes into either `serde_json::Value` or a custom struct.
///
/// For the following GraphQL schema:
///
/// ```graphql
/// type Address {
/// street: String!
/// }
///
/// type User {
/// id: Int!
/// addresses: [Address!]! @authorized(fields: "id", node: "street")
/// }
///
/// type Query {
/// users: [User!]!
/// }
/// ```
///
/// And the query:
///
/// ```graphql
/// query {
/// users { addresses { street } }
/// }
/// ```
///
/// The query returns two entities:
///
/// ```json
/// [
/// {
/// "id": 1,
/// "addresses": [{ "street": "Elm Street" }]
/// },
/// {
/// "id": 2,
/// "addresses": [{ "street": "Maple Street" }]
/// }
/// ]
/// ```
///
/// The arguments can be deserialized into a custom struct like:
///
/// ```rust
/// #[derive(serde::Deserialize)]
/// struct Parent {
/// id: u64,
/// }
///
/// #[derive(serde::Deserialize)]
/// struct Node {
/// street: String,
/// }
///
/// # fn foo(arguments: grafbase_hooks::EdgePostExecutionArguments) -> Result<(), serde_json::Error> {
/// let edges: Vec<(Parent, Vec<Node>)> = arguments.deserialize_edges()?;
/// # Ok(())
/// # }
/// ```
///
/// The directive defines the `fields` argument as `id` and `node` argument as
/// `street`, so the hook gets a vector of tuples where the first item is the
/// parent fields with the fields defined in `fields` and the second one is
/// the nodes with the fields defined in `node`.
pub fn deserialize_edges<T, K>(&self) -> Result<Vec<(T, Vec<K>)>, serde_json::Error>
where
T: serde::de::DeserializeOwned,
K: serde::de::DeserializeOwned,
{
self.edges
.iter()
.map(|(parent, nodes)| {
let parent: T = serde_json::from_str(parent)?;
let nodes: Vec<K> = nodes
.iter()
.map(|node| serde_json::from_str(node))
.collect::<Result<_, _>>()?;
Ok((parent, nodes))
})
.collect()
}
/// 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 Address {
/// street: String!
/// }
///
/// type User {
/// id: Int!
/// addresses: [Address!]! @authorized(
/// fields: "id",
/// node: "street",
/// metadata: { role: "admin" }
/// )
/// }
///
/// type Query {
/// users: [User!]!
/// }
/// ```
///
/// When executing a query like:
///
/// ```graphql
/// query {
/// users { addresses { street } }
/// }
/// ```
///
/// The metadata is `{"role": "admin"}`.
///
/// The metadata can be deserialized into a custom struct like:
///
/// ```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::EdgePostExecutionArguments) -> 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)
}
}