Add the DateTime<FixedOffset> scalar
This commit is contained in:
parent
53489c60bb
commit
83261cef2a
@ -1,6 +1,23 @@
|
|||||||
use crate::{InputValueError, InputValueResult, ScalarType, Value};
|
use crate::{InputValueError, InputValueResult, ScalarType, Value};
|
||||||
use async_graphql_derive::Scalar;
|
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
|
/// Implement the DateTime<Utc> scalar
|
||||||
///
|
///
|
||||||
|
@ -25,7 +25,7 @@ mod tests {
|
|||||||
use super::ID;
|
use super::ID;
|
||||||
use crate::Type;
|
use crate::Type;
|
||||||
use bson::oid::ObjectId;
|
use bson::oid::ObjectId;
|
||||||
use chrono::{DateTime, NaiveDate, NaiveTime, Utc};
|
use chrono::{DateTime, FixedOffset, NaiveDate, NaiveTime, Utc};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -60,6 +60,15 @@ mod tests {
|
|||||||
"DateTimeUtc!"
|
"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>::type_name(), "UUID");
|
||||||
assert_eq!(<Uuid as Type>::qualified_type_name(), "UUID!");
|
assert_eq!(<Uuid as Type>::qualified_type_name(), "UUID!");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user