Add scalar type for NaiveDateTime. #237

This commit is contained in:
Sunli 2020-08-14 15:28:42 +08:00
parent 4e8bd65b34
commit 8c272127ea
3 changed files with 29 additions and 19 deletions

View File

@ -5,7 +5,6 @@ mod floats;
mod id;
mod integers;
mod json;
mod naive_date;
mod naive_time;
mod string;
mod uuid;

View File

@ -1,17 +0,0 @@
use crate::{InputValueError, InputValueResult, ScalarType, Value};
use async_graphql_derive::Scalar;
use chrono::NaiveDate;
#[Scalar(internal)]
impl ScalarType for NaiveDate {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::String(s) => Ok(NaiveDate::parse_from_str(&s, "%Y-%m-%d")?),
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn to_value(&self) -> Value {
Value::String(self.format("%Y-%m-%d").to_string())
}
}

View File

@ -1,6 +1,20 @@
use crate::{InputValueError, InputValueResult, ScalarType, Value};
use async_graphql_derive::Scalar;
use chrono::NaiveTime;
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
#[Scalar(internal)]
impl ScalarType for NaiveDate {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::String(s) => Ok(NaiveDate::parse_from_str(&s, "%Y-%m-%d")?),
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn to_value(&self) -> Value {
Value::String(self.format("%Y-%m-%d").to_string())
}
}
#[Scalar(internal)]
impl ScalarType for NaiveTime {
@ -15,3 +29,17 @@ impl ScalarType for NaiveTime {
Value::String(self.format("%H:%M:%S").to_string())
}
}
#[Scalar(internal)]
impl ScalarType for NaiveDateTime {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::String(s) => Ok(NaiveDateTime::parse_from_str(&s, "%Y-%m-%d %H:%M:%S")?),
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn to_value(&self) -> Value {
Value::String(self.format("%Y-%m-%d %H:%M:%S").to_string())
}
}