Add the DateTime<FixedOffset> scalar

This commit is contained in:
Renat Sadykov 2020-08-29 23:18:57 +03:00
parent 53489c60bb
commit 83261cef2a
No known key found for this signature in database
GPG Key ID: E7F9B76CFC1969CD
2 changed files with 28 additions and 2 deletions

View File

@ -1,6 +1,23 @@
use crate::{InputValueError, InputValueResult, ScalarType, Value};
use async_graphql_derive::Scalar;
use chrono::{DateTime, Utc};
use chrono::{DateTime, FixedOffset, Utc};
/// Implement the DateTime<FixedOffset> scalar
///
/// The input/output is a string in RFC3339 format.
#[Scalar(internal, name = "DateTimeFixedOffset")]
impl ScalarType for DateTime<FixedOffset> {
fn parse(value: Value) -> InputValueResult<Self> {
match &value {
Value::String(s) => Ok(DateTime::parse_from_rfc3339(s)?),
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn to_value(&self) -> Value {
Value::String(self.to_rfc3339())
}
}
/// Implement the DateTime<Utc> scalar
///

View File

@ -25,7 +25,7 @@ mod tests {
use super::ID;
use crate::Type;
use bson::oid::ObjectId;
use chrono::{DateTime, NaiveDate, NaiveTime, Utc};
use chrono::{DateTime, FixedOffset, NaiveDate, NaiveTime, Utc};
use uuid::Uuid;
#[test]
@ -60,6 +60,15 @@ mod tests {
"DateTimeUtc!"
);
assert_eq!(
<DateTime::<FixedOffset> as Type>::type_name(),
"DateTimeFixedOffset"
);
assert_eq!(
<DateTime::<FixedOffset> as Type>::qualified_type_name(),
"DateTimeFixedOffset!"
);
assert_eq!(<Uuid as Type>::type_name(), "UUID");
assert_eq!(<Uuid as Type>::qualified_type_name(), "UUID!");