Merge pull request #179 from zenlist/datetime_parse

Update date time parsing to support more formats
This commit is contained in:
Sunli 2020-06-16 10:11:20 +08:00 committed by GitHub
commit 27075f271a

View File

@ -1,6 +1,6 @@
use crate::{InputValueError, InputValueResult, ScalarType, Value};
use async_graphql_derive::Scalar;
use chrono::{DateTime, TimeZone, Utc};
use chrono::{DateTime, Utc};
/// Implement the DateTime<Utc> scalar
///
@ -8,8 +8,10 @@ use chrono::{DateTime, TimeZone, Utc};
#[Scalar(internal, name = "DateTimeUtc")]
impl ScalarType for DateTime<Utc> {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::String(s) => Ok(Utc.datetime_from_str(&s, "%+")?),
match &value {
Value::String(s) => s
.parse::<DateTime<Utc>>()
.map_err(|_| InputValueError::ExpectedType(value)),
_ => Err(InputValueError::ExpectedType(value)),
}
}