juniper/integrations/
url.rs

1//! GraphQL support for [`url`] crate types.
2//!
3//! # Supported types
4//!
5//! | Rust type | GraphQL scalar |
6//! |-----------|----------------|
7//! | [`Url`]   | [`URL`][s1]    |
8//!
9//! [`Url`]: url::Url
10//! [s1]: https://graphql-scalars.dev/docs/scalars/url
11
12use crate::graphql_scalar;
13
14/// [Standard URL][0] format as specified in [RFC 3986].
15///
16/// [`URL` scalar][1] compliant.
17///
18/// See also [`url::Url`][2] for details.
19///
20/// [0]: http://url.spec.whatwg.org
21/// [1]: https://graphql-scalars.dev/docs/scalars/url
22/// [2]: https://docs.rs/url/*/url/struct.Url.html
23/// [RFC 3986]: https://datatracker.ietf.org/doc/html/rfc3986
24#[graphql_scalar]
25#[graphql(
26    name = "URL",
27    with = url_scalar,
28    to_output_with = Url::as_str,
29    parse_token(String),
30    specified_by_url = "https://graphql-scalars.dev/docs/scalars/url",
31)]
32type Url = url::Url;
33
34mod url_scalar {
35    use super::Url;
36
37    pub(super) fn from_input(s: &str) -> Result<Url, Box<str>> {
38        Url::parse(s).map_err(|e| format!("Failed to parse `URL`: {e}").into())
39    }
40}
41
42#[cfg(test)]
43mod test {
44    use url::Url;
45
46    use crate::{InputValue, graphql_input_value};
47
48    #[test]
49    fn url_from_input() {
50        let raw = "https://example.net/";
51        let input: InputValue = graphql_input_value!((raw));
52
53        let parsed: Url = crate::FromInputValue::from_input_value(&input).unwrap();
54        let url = Url::parse(raw).unwrap();
55
56        assert_eq!(parsed, url);
57    }
58}