grafbase_hooks/hooks/authorization/edge_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 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
/// Arguments passed to the `authorize_edge_pre_execution` hook.
pub struct EdgePreExecutionArguments {
definition: crate::wit::EdgeDefinition,
arguments: String,
metadata: String,
}
impl EdgePreExecutionArguments {
pub(crate) fn new(definition: crate::wit::EdgeDefinition, arguments: String, metadata: String) -> Self {
Self {
definition,
arguments,
metadata,
}
}
/// The name of the parent type of the edge.
///
/// For the following GraphQL schema:
///
/// ```graphql
/// type Query {
/// user(id: ID!): User @authorized(arguments: "id")
/// }
/// ```
///
/// The parent type name is `Query`.
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 Query {
/// user(id: ID!): User @authorized(arguments: "id")
/// }
/// ```
///
/// The field name is `user`.
pub fn field_name(&self) -> &str {
&self.definition.field_name
}
/// The arguments passed to the `@authorized` directive. The arguments are
/// serialized as a JSON object. This method will deserialize the arguments
/// into either `serde_json::Value` or a custom struct.
///
/// For the following GraphQL schema:
///
/// ```graphql
/// type Query {
/// user(id: ID!): User @authorized(arguments: "id")
/// }
/// ```
///
/// When executing a query like:
///
/// ```graphql
/// query {
/// user(id: "123") { id }
/// }
/// ```
///
/// The arguments are `{"id": "123"}`.
///
/// The arguments can be deserialized into a custom struct like:
///
/// ```rust
/// #[derive(serde::Deserialize)]
/// struct Arguments {
/// id: String,
/// }
///
/// # fn foo(arguments: grafbase_hooks::EdgePreExecutionArguments) -> Result<(), serde_json::Error> {
/// let arguments: Arguments = arguments.deserialize_arguments()?;
/// # Ok(())
/// # }
/// ```
pub fn deserialize_arguments<T: serde::de::DeserializeOwned>(&self) -> Result<T, serde_json::Error> {
serde_json::from_str(&self.arguments)
}
/// 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 Query {
/// user(id: ID!): User @authorized(arguments: "id", metadata: { role: "admin" })
/// }
/// ```
///
/// 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 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::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)
}
}