From 15deab33ee8079204b576867881f9711297790de Mon Sep 17 00:00:00 2001 From: Jarrett Tierney Date: Tue, 22 Mar 2022 20:43:54 -0700 Subject: [PATCH] Add feature(bson-uuid) which will enable Uuid's from the bson crate --- Cargo.toml | 2 +- src/types/external/bson.rs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 67987dd4..2ad924f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "async-graphql" -version = "3.0.36" +version = "3.0.37" authors = ["sunli ", "Koxiaet"] edition = "2021" description = "A GraphQL server library implemented in Rust" diff --git a/src/types/external/bson.rs b/src/types/external/bson.rs index 2686d049..b48f931d 100644 --- a/src/types/external/bson.rs +++ b/src/types/external/bson.rs @@ -2,6 +2,8 @@ use bson::{oid::ObjectId, Bson, Document}; #[cfg(feature = "chrono")] use bson::DateTime as UtcDateTime; +#[cfg(feature = "bson-uuid")] +use bson::Uuid; #[cfg(feature = "chrono")] use chrono::{DateTime, Utc}; @@ -21,6 +23,21 @@ impl ScalarType for ObjectId { } } +#[cfg(feature = "bson-uuid")] +#[Scalar(internal, name = "UUID")] +impl ScalarType for Uuid { + fn parse(value: Value) -> InputValueResult { + match value { + Value::String(s) => Ok(Uuid::parse_str(&s)?), + _ => Err(InputValueError::expected_type(value)), + } + } + + fn to_value(&self) -> Value { + Value::String(self.to_string()) + } +} + #[cfg(feature = "chrono")] #[Scalar(internal, name = "DateTime")] impl ScalarType for UtcDateTime {