gitea_pages/backend/src/api/scalars/uuid.rs

40 lines
920 B
Rust

use std::ops::Deref;
type Value = uuidv7::Uuid;
pub struct Uuid(pub Value);
#[juniper::graphql_scalar(name = "UUID", description = "UUID encoded as a string")]
impl<S> GraphQLScalar for Uuid
where
S: juniper::ScalarValue,
{
fn resolve(&self) -> juniper::Value {
juniper::Value::scalar(self.0.to_string())
}
fn from_input_value(value: &juniper::InputValue) -> Option<Uuid> {
value
.as_string_value()
.and_then(|s| {
use uuid_simd::UuidExt;
use uuidv7::Uuid;
Uuid::parse(s.as_bytes()).ok()
})
.map(Uuid)
}
fn from_str<'a>(value: juniper::ScalarToken<'a>) -> juniper::ParseScalarResult<'a, S> {
<String as juniper::ParseScalarValue<S>>::from_str(value)
}
}
impl Deref for Uuid {
type Target = Value;
fn deref(&self) -> &Self::Target {
&self.0
}
}