async_graphql/types/external/
datetime.rs

1use chrono::{DateTime, FixedOffset, Local, Utc};
2
3use crate::{InputValueError, InputValueResult, Scalar, ScalarType, Value};
4
5/// Implement the DateTime<FixedOffset> scalar
6///
7/// The input/output is a string in RFC3339 format.
8#[Scalar(
9    internal,
10    name = "DateTime",
11    specified_by_url = "https://datatracker.ietf.org/doc/html/rfc3339"
12)]
13impl ScalarType for DateTime<FixedOffset> {
14    fn parse(value: Value) -> InputValueResult<Self> {
15        match &value {
16            Value::String(s) => Ok(s.parse::<DateTime<FixedOffset>>()?),
17            _ => Err(InputValueError::expected_type(value)),
18        }
19    }
20
21    fn to_value(&self) -> Value {
22        Value::String(self.to_rfc3339())
23    }
24}
25
26/// Implement the DateTime<Local> scalar
27///
28/// The input/output is a string in RFC3339 format.
29#[Scalar(
30    internal,
31    name = "DateTime",
32    specified_by_url = "https://datatracker.ietf.org/doc/html/rfc3339"
33)]
34impl ScalarType for DateTime<Local> {
35    fn parse(value: Value) -> InputValueResult<Self> {
36        match &value {
37            Value::String(s) => Ok(s.parse::<DateTime<Local>>()?),
38            _ => Err(InputValueError::expected_type(value)),
39        }
40    }
41
42    fn to_value(&self) -> Value {
43        Value::String(self.to_rfc3339())
44    }
45}
46
47/// Implement the DateTime<Utc> scalar
48///
49/// The input/output is a string in RFC3339 format.
50#[Scalar(
51    internal,
52    name = "DateTime",
53    specified_by_url = "https://datatracker.ietf.org/doc/html/rfc3339"
54)]
55impl ScalarType for DateTime<Utc> {
56    fn parse(value: Value) -> InputValueResult<Self> {
57        match &value {
58            Value::String(s) => Ok(s.parse::<DateTime<Utc>>()?),
59            _ => Err(InputValueError::expected_type(value)),
60        }
61    }
62
63    fn to_value(&self) -> Value {
64        Value::String(self.to_rfc3339())
65    }
66}