Skip to content

Commit

Permalink
Support chrono-tz::Tz GraphQL scalar (#519)
Browse files Browse the repository at this point in the history
Co-authored-by: Nirman Gupta <[email protected]>
Co-authored-by: Kai Ren <[email protected]>
  • Loading branch information
3 people authored Oct 19, 2020
1 parent 31d339b commit 5832b36
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ your Schemas automatically.
- [uuid][uuid]
- [url][url]
- [chrono][chrono]
- [chrono-tz][chrono-tz]
- [bson][bson]

### Web Frameworks
Expand Down Expand Up @@ -112,5 +113,6 @@ Juniper has not reached 1.0 yet, thus some API instability should be expected.
[uuid]: https://crates.io/crates/uuid
[url]: https://crates.io/crates/url
[chrono]: https://crates.io/crates/chrono
[chrono-tz]: https://crates.io/crates/chrono-tz
[bson]: https://crates.io/crates/bson
[juniper-from-schema]: https://github.com/davidpdrsn/juniper-from-schema
2 changes: 2 additions & 0 deletions juniper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
- `ParseError`
- `RuleError`

- Support `chrono-tz::Tz` scalar behind a `chrono-tz` feature flag. ([#519](https://github.com/graphql-rust/juniper/pull/519))

## Fixes

- Massively improved the `#[graphql_union]` proc macro. ([#666](https://github.com/graphql-rust/juniper/pull/666)):
Expand Down
1 change: 1 addition & 0 deletions juniper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ anyhow = { default-features = false, version = "1.0.32", optional = true }
async-trait = "0.1.39"
bson = { version = "1.0", optional = true }
chrono = { default-features = false, version = "0.4", optional = true }
chrono-tz = { version = "0.5", default-features = false, optional = true }
fnv = "1.0.3"
futures = { default-features = false, features = ["alloc"], version = "0.3.1" }
futures-enum = "0.1.12"
Expand Down
80 changes: 80 additions & 0 deletions juniper/src/integrations/chrono_tz.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//! [`Tz`] (timezone) scalar implementation, represented by its [IANA database][1] name.
//!
//! [`Tz`]: chrono_tz::Tz
//! [1]: http://www.iana.org/time-zones
use chrono_tz::Tz;

use crate::{
graphql_scalar,
parser::{ParseError, ScalarToken, Token},
value::ParseScalarResult,
Value,
};

#[graphql_scalar(name = "Tz", description = "Timezone")]
impl<S> GraphQLScalar for Tz
where
S: ScalarValue,
{
fn resolve(&self) -> Value {
Value::scalar(self.name().to_owned())
}

fn from_input_value(v: &InputValue) -> Option<Tz> {
v.as_string_value().and_then(|s| s.parse::<Tz>().ok())
}

fn from_str<'a>(val: ScalarToken<'a>) -> ParseScalarResult<'a, S> {
if let ScalarToken::String(s) = val {
Ok(S::from(s.to_owned()))
} else {
Err(ParseError::UnexpectedToken(Token::Scalar(val)))
}
}
}

#[cfg(test)]
mod test {
mod from_input_value {
use chrono_tz::Tz;

use crate::{DefaultScalarValue, FromInputValue, InputValue};

fn tz_input_test(raw: &'static str, expected: Option<Tz>) {
let input = <InputValue<DefaultScalarValue>>::scalar(raw.to_string());
let parsed: Option<Tz> = FromInputValue::from_input_value(&input);

assert_eq!(parsed, expected);
}

#[test]
fn europe_zone() {
tz_input_test("Europe/London", Some(chrono_tz::Europe::London));
}

#[test]
fn etc_minus() {
tz_input_test("Etc/GMT-3", Some(chrono_tz::Etc::GMTMinus3));
}

mod invalid {
use super::tz_input_test;

#[test]
fn forward_slash() {
tz_input_test("Abc/Xyz", None);
}

#[test]
fn number() {
tz_input_test("8086", None);
}

#[test]
fn no_forward_slash() {
tz_input_test("AbcXyz", None);
}
}
}
}
4 changes: 4 additions & 0 deletions juniper/src/integrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ pub mod serde;
/// GraphQL support for [chrono](https://github.com/chronotope/chrono) types.
pub mod chrono;

#[cfg(feature = "chrono-tz")]
/// GraphQL support for [chrono-tz](https://github.com/chronotope/chrono-tz) types.
pub mod chrono_tz;

#[cfg(feature = "url")]
/// GraphQL support for [url](https://github.com/servo/rust-url) types.
pub mod url;
Expand Down
3 changes: 1 addition & 2 deletions juniper_codegen/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ pub mod duplicate;
pub mod parse_impl;
pub mod span_container;

use std::collections::HashMap;
use std::str::FromStr;
use std::{collections::HashMap, str::FromStr};

use proc_macro2::{Span, TokenStream};
use proc_macro_error::abort;
Expand Down

0 comments on commit 5832b36

Please sign in to comment.