feat: add chrono::Duration custom scalar
This commit is contained in:
parent
b48d4c8c1f
commit
119c5d0f5d
|
@ -22,6 +22,7 @@ dataloader = ["futures-timer", "futures-channel", "lru"]
|
|||
tracing = ["tracinglib", "tracing-futures"]
|
||||
decimal = ["rust_decimal"]
|
||||
cbor = ["serde_cbor"]
|
||||
chrono-duration = ["chrono", "iso8601-duration"]
|
||||
|
||||
[dependencies]
|
||||
async-graphql-derive = { path = "derive", version = "=2.11.0" }
|
||||
|
@ -50,6 +51,7 @@ mime = "0.3.15"
|
|||
bson = { version = "2.0.0", optional = true, features = ["chrono-0_4"] }
|
||||
chrono = { version = "0.4.19", optional = true }
|
||||
chrono-tz = { version = "0.5.3", optional = true }
|
||||
iso8601-duration = { version = "0.1.0", optional = true }
|
||||
log = { version = "0.4.14", optional = true }
|
||||
secrecy = { version = "0.7.0", optional = true }
|
||||
tracinglib = { version = "0.1.25", optional = true, package = "tracing" }
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
use chrono::Duration;
|
||||
use iso8601_duration as iso8601;
|
||||
|
||||
use crate::{InputValueError, InputValueResult, Scalar, ScalarType, Value};
|
||||
|
||||
/// Implement the Duration scalar
|
||||
///
|
||||
/// The input/output is a string in ISO8601 format.
|
||||
#[Scalar(internal, name = "Duration")]
|
||||
impl ScalarType for Duration {
|
||||
fn parse(value: Value) -> InputValueResult<Self> {
|
||||
match &value {
|
||||
Value::String(s) => Ok(Duration::from_std(iso8601::Duration::parse(s)?.to_std())?),
|
||||
_ => Err(InputValueError::expected_type(value)),
|
||||
}
|
||||
}
|
||||
|
||||
fn to_value(&self) -> Value {
|
||||
Value::String(self.to_string())
|
||||
}
|
||||
}
|
|
@ -18,6 +18,8 @@ mod bson;
|
|||
mod chrono_tz;
|
||||
#[cfg(feature = "chrono")]
|
||||
mod datetime;
|
||||
#[cfg(feature = "chrono-duration")]
|
||||
mod duration;
|
||||
#[cfg(feature = "decimal")]
|
||||
mod decimal;
|
||||
#[cfg(feature = "chrono")]
|
||||
|
|
Loading…
Reference in New Issue