async-graphql/src/types/external/datetime.rs

54 lines
1.6 KiB
Rust
Raw Normal View History

use crate::{GQLScalar, InputValueError, InputValueResult, ScalarType, Value};
use chrono::{DateTime, FixedOffset, Local, Utc};
2020-08-29 20:18:57 +00:00
/// Implement the DateTime<FixedOffset> scalar
///
/// The input/output is a string in RFC3339 format.
#[GQLScalar(internal, name = "DateTime")]
2020-08-29 20:18:57 +00:00
impl ScalarType for DateTime<FixedOffset> {
fn parse(value: Value) -> InputValueResult<Self> {
match &value {
Value::String(s) => Ok(s.parse::<DateTime<FixedOffset>>()?),
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn to_value(&self) -> Value {
Value::String(self.to_rfc3339())
}
}
/// Implement the DateTime<Local> scalar
///
/// The input/output is a string in RFC3339 format.
#[GQLScalar(internal, name = "DateTime")]
impl ScalarType for DateTime<Local> {
fn parse(value: Value) -> InputValueResult<Self> {
match &value {
Value::String(s) => Ok(s.parse::<DateTime<Local>>()?),
2020-08-29 20:18:57 +00:00
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn to_value(&self) -> Value {
Value::String(self.to_rfc3339())
}
}
2020-03-01 10:54:34 +00:00
2020-03-20 03:56:08 +00:00
/// Implement the DateTime<Utc> scalar
///
/// The input/output is a string in RFC3339 format.
#[GQLScalar(internal, name = "DateTime")]
impl ScalarType for DateTime<Utc> {
fn parse(value: Value) -> InputValueResult<Self> {
match &value {
2020-06-16 15:21:05 +00:00
Value::String(s) => Ok(s.parse::<DateTime<Utc>>()?),
_ => Err(InputValueError::ExpectedType(value)),
2020-03-01 10:54:34 +00:00
}
}
fn to_value(&self) -> Value {
Value::String(self.to_rfc3339())
2020-03-01 10:54:34 +00:00
}
}