Skip to content

Commit 2cb96d0

Browse files
authored
impl GraphQLScalar for NaiveTime (#657)
* impl GraphQLScalar for NaiveTime * Add feature
1 parent 0bb1c5b commit 2cb96d0

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

juniper/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ default = [
3131
"url",
3232
"uuid",
3333
]
34+
scalar-naivetime = []
3435

3536
[dependencies]
3637
juniper_codegen = { version = "0.14.2", path = "../juniper_codegen" }

juniper/src/integrations/chrono.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
| | | precise enough for nanoseconds. |
1212
| | | Values will be truncated to microsecond |
1313
| | | resolution. |
14+
| `NaiveTime` | H:M:S | Optional. Use the `scalar-naivetime` |
15+
| | | feature. |
1416
1517
*/
1618
#![allow(clippy::needless_lifetimes)]
@@ -99,6 +101,30 @@ where
99101
}
100102
}
101103

104+
#[cfg(feature = "scalar-naivetime")]
105+
#[crate::graphql_scalar_internal(description = "NaiveTime")]
106+
impl<S> GraphQLScalar for NaiveTime
107+
where
108+
S: ScalarValue,
109+
{
110+
fn resolve(&self) -> Value {
111+
Value::scalar(self.format("%H:%M:%S").to_string())
112+
}
113+
114+
fn from_input_value(v: &InputValue) -> Option<NaiveTime> {
115+
v.as_string_value()
116+
.and_then(|s| NaiveTime::parse_from_str(s, "%H:%M:%S").ok())
117+
}
118+
119+
fn from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> {
120+
if let ScalarToken::String(value) = value {
121+
Ok(S::from(value.to_owned()))
122+
} else {
123+
Err(ParseError::UnexpectedToken(Token::Scalar(value)))
124+
}
125+
}
126+
}
127+
102128
// JSON numbers (i.e. IEEE doubles) are not precise enough for nanosecond
103129
// datetimes. Values will be truncated to microsecond resolution.
104130
#[crate::graphql_scalar_internal(description = "NaiveDateTime")]
@@ -194,6 +220,20 @@ mod test {
194220
assert_eq!(parsed.day(), d);
195221
}
196222

223+
#[test]
224+
#[cfg(feature = "scalar-naivetime")]
225+
fn naivetime_from_input_value() {
226+
let input: crate::InputValue<DefaultScalarValue>;
227+
input = InputValue::scalar("21:12:19".to_string());
228+
let [h, m, s] = [21, 12, 19];
229+
let parsed: NaiveTime = crate::FromInputValue::from_input_value(&input).unwrap();
230+
let expected = NaiveTime::from_hms(h, m, s);
231+
assert_eq!(parsed, expected);
232+
assert_eq!(parsed.hour(), h);
233+
assert_eq!(parsed.minute(), m);
234+
assert_eq!(parsed.second(), s);
235+
}
236+
197237
#[test]
198238
fn naivedatetime_from_input_value() {
199239
let raw = 1_000_000_000_f64;
@@ -223,6 +263,27 @@ mod integration_test {
223263
struct Root;
224264

225265
#[crate::graphql_object_internal]
266+
#[cfg(feature = "scalar-naivetime")]
267+
impl Root {
268+
fn exampleNaiveDate() -> NaiveDate {
269+
NaiveDate::from_ymd(2015, 3, 14)
270+
}
271+
fn exampleNaiveDateTime() -> NaiveDateTime {
272+
NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11)
273+
}
274+
fn exampleNaiveTime() -> NaiveTime {
275+
NaiveTime::from_hms(16, 7, 8)
276+
}
277+
fn exampleDateTimeFixedOffset() -> DateTime<FixedOffset> {
278+
DateTime::parse_from_rfc3339("1996-12-19T16:39:57-08:00").unwrap()
279+
}
280+
fn exampleDateTimeUtc() -> DateTime<Utc> {
281+
Utc.timestamp(61, 0)
282+
}
283+
}
284+
285+
#[crate::graphql_object_internal]
286+
#[cfg(not(feature = "scalar-naivetime"))]
226287
impl Root {
227288
fn exampleNaiveDate() -> NaiveDate {
228289
NaiveDate::from_ymd(2015, 3, 14)
@@ -238,6 +299,18 @@ mod integration_test {
238299
}
239300
}
240301

302+
#[cfg(feature = "scalar-naivetime")]
303+
let doc = r#"
304+
{
305+
exampleNaiveDate,
306+
exampleNaiveDateTime,
307+
exampleNaiveTime,
308+
exampleDateTimeFixedOffset,
309+
exampleDateTimeUtc,
310+
}
311+
"#;
312+
313+
#[cfg(not(feature = "scalar-naivetime"))]
241314
let doc = r#"
242315
{
243316
exampleNaiveDate,
@@ -265,6 +338,8 @@ mod integration_test {
265338
vec![
266339
("exampleNaiveDate", Value::scalar("2015-03-14")),
267340
("exampleNaiveDateTime", Value::scalar(1_467_969_011.0)),
341+
#[cfg(feature = "scalar-naivetime")]
342+
("exampleNaiveTime", Value::scalar("16:07:08")),
268343
(
269344
"exampleDateTimeFixedOffset",
270345
Value::scalar("1996-12-19T16:39:57-08:00"),

0 commit comments

Comments
 (0)