diff --git a/juniper/Cargo.toml b/juniper/Cargo.toml index caa834a9..66a796a7 100644 --- a/juniper/Cargo.toml +++ b/juniper/Cargo.toml @@ -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] diff --git a/juniper/src/integrations/mod.rs b/juniper/src/integrations/mod.rs index a4a7803c..3a2e9b87 100644 --- a/juniper/src/integrations/mod.rs +++ b/juniper/src/integrations/mod.rs @@ -1,4 +1,7 @@ pub mod serde; +#[cfg(feature = "url")] +mod url; + #[cfg(feature = "uuid")] mod uuid; diff --git a/juniper/src/integrations/url.rs b/juniper/src/integrations/url.rs new file mode 100644 index 00000000..ed30defd --- /dev/null +++ b/juniper/src/integrations/url.rs @@ -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 { + 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); + } +} diff --git a/juniper/src/lib.rs b/juniper/src/lib.rs index 0e627b1c..9a98e345 100644 --- a/juniper/src/lib.rs +++ b/juniper/src/lib.rs @@ -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;