Repository: graphql-rust/juniper (language: rust)

Issue:
Implement GraphQLType for uuid::Uuid
Since the UUID crate is so popular, it's a pain not to have it implement GraphQLType.

We should optionally implement it behind a feature flag, representing the UUID with a string.


Discussion / hints:

Fix patch:
diff --git a/juniper/Cargo.toml b/juniper/Cargo.toml
index 8075528a1..cb92caa51 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 82b2d0949..a4a7803cc 100644
--- a/juniper/src/integrations/mod.rs
+++ b/juniper/src/integrations/mod.rs
@@ -1,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 000000000..b0806b285
--- /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 7b6c8c38b..e43d88e2c 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]


Test patch: