From fc51ff9e22a809e257ae92f12272f1dbcb31f594 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 22 Aug 2024 22:25:57 +0200 Subject: Add position type and query on it --- src/dates.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/dates.rs (limited to 'src/dates.rs') diff --git a/src/dates.rs b/src/dates.rs new file mode 100644 index 0000000..87e6f5e --- /dev/null +++ b/src/dates.rs @@ -0,0 +1,35 @@ +use async_graphql::{InputValueError, InputValueResult, Scalar, ScalarType, Value}; +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct GraphQLDate(pub DateTime); + +impl From> for GraphQLDate { + fn from(dt: DateTime) -> Self { + GraphQLDate(dt) + } +} + +impl From for DateTime { + fn from(my_dt: GraphQLDate) -> Self { + my_dt.0 + } +} + +#[Scalar] +impl ScalarType for GraphQLDate { + fn parse(value: Value) -> InputValueResult { + if let Value::String(s) = &value { + DateTime::parse_from_rfc3339(s) + .map(|dt| GraphQLDate(dt.with_timezone(&Utc))) + .map_err(|e| InputValueError::custom(format!("Invalid DateTime: {}", e))) + } else { + Err(InputValueError::expected_type(value)) + } + } + + fn to_value(&self) -> Value { + Value::String(self.0.to_rfc3339()) + } +} -- cgit v1.2.3-18-g5258