2020-04-27 09:58:10 +00:00
|
|
|
use crate::{Result, ScalarType, Value};
|
|
|
|
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-03-04 02:38:07 +00:00
|
|
|
fn parse(value: &Value) -> Option<Self> {
|
2020-03-01 10:54:34 +00:00
|
|
|
match value {
|
2020-03-03 11:15:18 +00:00
|
|
|
Value::String(s) => Some(Utc.datetime_from_str(&s, "%+").ok()?),
|
|
|
|
_ => None,
|
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
|
|
|
}
|
|
|
|
}
|