2020-05-10 10:27:46 +00:00
|
|
|
use crate::{InputValueError, InputValueResult, Result, ScalarType, Value};
|
2020-04-27 09:58:10 +00:00
|
|
|
use async_graphql_derive::Scalar;
|
2020-03-01 10:54:34 +00:00
|
|
|
use chrono::{DateTime, TimeZone, Utc};
|
|
|
|
|
2020-03-20 03:56:08 +00:00
|
|
|
/// Implement the DateTime<Utc> scalar
|
|
|
|
///
|
|
|
|
/// The input/output is a string in RFC3339 format.
|
2020-04-27 09:58:10 +00:00
|
|
|
#[Scalar(internal)]
|
|
|
|
impl ScalarType for DateTime<Utc> {
|
2020-03-01 10:54:34 +00:00
|
|
|
fn type_name() -> &'static str {
|
|
|
|
"DateTime"
|
|
|
|
}
|
|
|
|
|
2020-05-11 13:47:24 +00:00
|
|
|
fn parse(value: Value) -> InputValueResult<Self> {
|
2020-03-01 10:54:34 +00:00
|
|
|
match value {
|
2020-05-10 10:27:46 +00:00
|
|
|
Value::String(s) => Ok(Utc.datetime_from_str(&s, "%+")?),
|
2020-05-11 13:47:24 +00:00
|
|
|
_ => Err(InputValueError::ExpectedType(value)),
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 03:39:28 +00:00
|
|
|
fn to_json(&self) -> Result<serde_json::Value> {
|
|
|
|
Ok(self.to_rfc3339().into())
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
}
|