url feature: Implement url::Url as a scalar type

This commit is contained in:
Christian Legnitto 2017-09-10 12:56:39 -07:00 committed by Magnus Hallin
parent f7625056ac
commit c1cb120176
4 changed files with 40 additions and 1 deletions

View file

@ -22,13 +22,14 @@ path = "benches/bench.rs"
[features]
nightly = []
expose-test-schema = []
default = ["uuid"]
default = ["url", "uuid"]
[dependencies]
ordermap = { version = "^0.2.11", features = ["serde-1"] }
serde = { version = "^1.0.8" }
serde_derive = {version="^1.0.8" }
serde_json = { version="^1.0.2", optional = true }
url = { version = "^1.5.1", optional = true }
uuid = { version = "0.5.1", optional = true }
[dev-dependencies]

View file

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

View file

@ -0,0 +1,32 @@
use url::Url;
use ::Value;
graphql_scalar!(Url {
description: "Url"
resolve(&self) -> Value {
Value::string(self.as_str())
}
from_input_value(v: &InputValue) -> Option<Url> {
v.as_string_value()
.and_then(|s| Url::parse(s).ok())
}
});
#[cfg(test)]
mod test {
use url::Url;
#[test]
fn url_from_input_value() {
let raw = "https://example.net/";
let input = ::InputValue::String(raw.to_string());
let parsed: Url = ::FromInputValue::from_input_value(&input).unwrap();
let url = Url::parse(raw).unwrap();
assert_eq!(parsed, url);
}
}

View file

@ -124,6 +124,9 @@ extern crate serde_json;
extern crate ordermap;
#[cfg(any(test, feature = "url"))]
extern crate url;
#[cfg(any(test, feature = "uuid"))]
extern crate uuid;