async-graphql/src/scalars/datetime.rs

21 lines
641 B
Rust
Raw Normal View History

use crate::{InputValueError, InputValueResult, 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.
#[Scalar(internal, name = "DateTime")]
impl ScalarType for DateTime<Utc> {
fn parse(value: Value) -> InputValueResult<Self> {
2020-03-01 10:54:34 +00:00
match value {
Value::String(s) => Ok(Utc.datetime_from_str(&s, "%+")?),
_ => 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
}
}