From b22535a77d93fdd08646f6afc8dd48cf6c719bbe Mon Sep 17 00:00:00 2001 From: Caio Date: Sun, 10 May 2020 21:30:51 -0300 Subject: [PATCH 1/2] impl GraphQLScalar for NaiveTime --- juniper/src/integrations/chrono.rs | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/juniper/src/integrations/chrono.rs b/juniper/src/integrations/chrono.rs index 586967549..6582bc60c 100644 --- a/juniper/src/integrations/chrono.rs +++ b/juniper/src/integrations/chrono.rs @@ -99,6 +99,29 @@ where } } +#[crate::graphql_scalar_internal(description = "NaiveTime")] +impl GraphQLScalar for NaiveTime +where + S: ScalarValue, +{ + fn resolve(&self) -> Value { + Value::scalar(self.format("%H:%M:%S").to_string()) + } + + fn from_input_value(v: &InputValue) -> Option { + v.as_string_value() + .and_then(|s| NaiveTime::parse_from_str(s, "%H:%M:%S").ok()) + } + + fn from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> { + if let ScalarToken::String(value) = value { + Ok(S::from(value.to_owned())) + } else { + Err(ParseError::UnexpectedToken(Token::Scalar(value))) + } + } +} + // JSON numbers (i.e. IEEE doubles) are not precise enough for nanosecond // datetimes. Values will be truncated to microsecond resolution. #[crate::graphql_scalar_internal(description = "NaiveDateTime")] @@ -194,6 +217,19 @@ mod test { assert_eq!(parsed.day(), d); } + #[test] + fn naivetime_from_input_value() { + let input: crate::InputValue; + input = InputValue::scalar("21:12:19".to_string()); + let [h, m, s] = [21, 12, 19]; + let parsed: NaiveTime = crate::FromInputValue::from_input_value(&input).unwrap(); + let expected = NaiveTime::from_hms(h, m, s); + assert_eq!(parsed, expected); + assert_eq!(parsed.hour(), h); + assert_eq!(parsed.minute(), m); + assert_eq!(parsed.second(), s); + } + #[test] fn naivedatetime_from_input_value() { let raw = 1_000_000_000_f64; @@ -230,6 +266,9 @@ mod integration_test { fn exampleNaiveDateTime() -> NaiveDateTime { NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11) } + fn exampleNaiveTime() -> NaiveTime { + NaiveTime::from_hms(16, 7, 8) + } fn exampleDateTimeFixedOffset() -> DateTime { DateTime::parse_from_rfc3339("1996-12-19T16:39:57-08:00").unwrap() } @@ -242,6 +281,7 @@ mod integration_test { { exampleNaiveDate, exampleNaiveDateTime, + exampleNaiveTime, exampleDateTimeFixedOffset, exampleDateTimeUtc, } @@ -265,6 +305,7 @@ mod integration_test { vec![ ("exampleNaiveDate", Value::scalar("2015-03-14")), ("exampleNaiveDateTime", Value::scalar(1_467_969_011.0)), + ("exampleNaiveTime", Value::scalar("16:07:08")), ( "exampleDateTimeFixedOffset", Value::scalar("1996-12-19T16:39:57-08:00"), From a7ba3357563dc09a65042abe33060cc74431a6f2 Mon Sep 17 00:00:00 2001 From: Caio Date: Wed, 20 May 2020 09:02:28 -0300 Subject: [PATCH 2/2] Add feature --- juniper/Cargo.toml | 1 + juniper/src/integrations/chrono.rs | 34 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/juniper/Cargo.toml b/juniper/Cargo.toml index 202b03c0a..69e970a61 100644 --- a/juniper/Cargo.toml +++ b/juniper/Cargo.toml @@ -31,6 +31,7 @@ default = [ "url", "uuid", ] +scalar-naivetime = [] [dependencies] juniper_codegen = { version = "0.14.2", path = "../juniper_codegen" } diff --git a/juniper/src/integrations/chrono.rs b/juniper/src/integrations/chrono.rs index 6582bc60c..0d5a16962 100644 --- a/juniper/src/integrations/chrono.rs +++ b/juniper/src/integrations/chrono.rs @@ -11,6 +11,8 @@ | | | precise enough for nanoseconds. | | | | Values will be truncated to microsecond | | | | resolution. | +| `NaiveTime` | H:M:S | Optional. Use the `scalar-naivetime` | +| | | feature. | */ #![allow(clippy::needless_lifetimes)] @@ -99,6 +101,7 @@ where } } +#[cfg(feature = "scalar-naivetime")] #[crate::graphql_scalar_internal(description = "NaiveTime")] impl GraphQLScalar for NaiveTime where @@ -218,6 +221,7 @@ mod test { } #[test] + #[cfg(feature = "scalar-naivetime")] fn naivetime_from_input_value() { let input: crate::InputValue; input = InputValue::scalar("21:12:19".to_string()); @@ -259,6 +263,7 @@ mod integration_test { struct Root; #[crate::graphql_object_internal] + #[cfg(feature = "scalar-naivetime")] impl Root { fn exampleNaiveDate() -> NaiveDate { NaiveDate::from_ymd(2015, 3, 14) @@ -277,6 +282,24 @@ mod integration_test { } } + #[crate::graphql_object_internal] + #[cfg(not(feature = "scalar-naivetime"))] + impl Root { + fn exampleNaiveDate() -> NaiveDate { + NaiveDate::from_ymd(2015, 3, 14) + } + fn exampleNaiveDateTime() -> NaiveDateTime { + NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11) + } + fn exampleDateTimeFixedOffset() -> DateTime { + DateTime::parse_from_rfc3339("1996-12-19T16:39:57-08:00").unwrap() + } + fn exampleDateTimeUtc() -> DateTime { + Utc.timestamp(61, 0) + } + } + + #[cfg(feature = "scalar-naivetime")] let doc = r#" { exampleNaiveDate, @@ -287,6 +310,16 @@ mod integration_test { } "#; + #[cfg(not(feature = "scalar-naivetime"))] + let doc = r#" + { + exampleNaiveDate, + exampleNaiveDateTime, + exampleDateTimeFixedOffset, + exampleDateTimeUtc, + } + "#; + let schema = RootNode::new( Root, EmptyMutation::<()>::new(), @@ -305,6 +338,7 @@ mod integration_test { vec![ ("exampleNaiveDate", Value::scalar("2015-03-14")), ("exampleNaiveDateTime", Value::scalar(1_467_969_011.0)), + #[cfg(feature = "scalar-naivetime")] ("exampleNaiveTime", Value::scalar("16:07:08")), ( "exampleDateTimeFixedOffset",