async-graphql/src/types/external/datetime.rs
2020-09-13 10:38:19 +01:00

54 lines
1.6 KiB
Rust

use crate::{GQLScalar, InputValueError, InputValueResult, ScalarType, Value};
use chrono::{DateTime, FixedOffset, Local, Utc};
/// Implement the DateTime<FixedOffset> scalar
///
/// The input/output is a string in RFC3339 format.
#[GQLScalar(internal, name = "DateTime")]
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>>()?),
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn to_value(&self) -> Value {
Value::String(self.to_rfc3339())
}
}
/// 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 {
Value::String(s) => Ok(s.parse::<DateTime<Utc>>()?),
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn to_value(&self) -> Value {
Value::String(self.to_rfc3339())
}
}