uuid feature: Implement uuid::Uuid as a scalar type

Fixes #86
This commit is contained in:
theduke 2017-08-28 08:55:23 +02:00
parent 1b18bc9869
commit 01b2dea848
4 changed files with 41 additions and 0 deletions

View file

@ -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"

View file

@ -1 +1,4 @@
pub mod serde;
#[cfg(feature = "uuid")]
mod uuid;

View file

@ -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);
}
}

View file

@ -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]