diff --git a/juniper/Cargo.toml b/juniper/Cargo.toml index 8075528a..cb92caa5 100644 --- a/juniper/Cargo.toml +++ b/juniper/Cargo.toml @@ -22,11 +22,13 @@ path = "benches/bench.rs" [features] nightly = [] expose-test-schema = [] +default = ["uuid"] [dependencies] serde = { version = "^1.0.8" } serde_derive = {version="^1.0.8" } serde_json = { version="^1.0.2", optional = true } +uuid = { version = "0.5.1", optional = true } [dev-dependencies] bencher = "^0.1.2" diff --git a/juniper/src/integrations/mod.rs b/juniper/src/integrations/mod.rs index 82b2d094..a4a7803c 100644 --- a/juniper/src/integrations/mod.rs +++ b/juniper/src/integrations/mod.rs @@ -1 +1,4 @@ pub mod serde; + +#[cfg(feature = "uuid")] +mod uuid; diff --git a/juniper/src/integrations/uuid.rs b/juniper/src/integrations/uuid.rs new file mode 100644 index 00000000..b0806b28 --- /dev/null +++ b/juniper/src/integrations/uuid.rs @@ -0,0 +1,32 @@ +use uuid::Uuid; + +use ::Value; + +graphql_scalar!(Uuid { + description: "Uuid" + + resolve(&self) -> Value { + Value::string(self.to_string()) + } + + from_input_value(v: &InputValue) -> Option<Uuid> { + v.as_string_value() + .and_then(|s| Uuid::parse_str(s).ok()) + } +}); + +#[cfg(test)] +mod test { + use uuid::Uuid; + + #[test] + fn uuid_from_input_value() { + let raw = "123e4567-e89b-12d3-a456-426655440000"; + let input = ::InputValue::String(raw.to_string()); + + let parsed: Uuid = ::FromInputValue::from(&input).unwrap(); + let id = Uuid::parse_str(raw).unwrap(); + + assert_eq!(parsed, id); + } +} \ No newline at end of file diff --git a/juniper/src/lib.rs b/juniper/src/lib.rs index 7b6c8c38..e43d88e2 100644 --- a/juniper/src/lib.rs +++ b/juniper/src/lib.rs @@ -121,6 +121,10 @@ extern crate serde_derive; #[cfg(any(test, feature = "expose-test-schema"))] extern crate serde_json; + +#[cfg(any(test, feature = "uuid"))] +extern crate uuid; + use std::borrow::Cow; #[macro_use]