Merge pull request #689 from meh/duration-scalar
feat: add chrono::Duration custom scalar
This commit is contained in:
commit
ecb4b4dac5
@ -22,6 +22,7 @@ dataloader = ["futures-timer", "futures-channel", "lru"]
|
|||||||
tracing = ["tracinglib", "tracing-futures"]
|
tracing = ["tracinglib", "tracing-futures"]
|
||||||
decimal = ["rust_decimal"]
|
decimal = ["rust_decimal"]
|
||||||
cbor = ["serde_cbor"]
|
cbor = ["serde_cbor"]
|
||||||
|
chrono-duration = ["chrono", "iso8601-duration"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
async-graphql-derive = { path = "derive", version = "=2.11.0" }
|
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"] }
|
bson = { version = "2.0.0", optional = true, features = ["chrono-0_4"] }
|
||||||
chrono = { version = "0.4.19", optional = true }
|
chrono = { version = "0.4.19", optional = true }
|
||||||
chrono-tz = { version = "0.5.3", 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 }
|
log = { version = "0.4.14", optional = true }
|
||||||
secrecy = { version = "0.7.0", optional = true }
|
secrecy = { version = "0.7.0", optional = true }
|
||||||
tracinglib = { version = "0.1.25", optional = true, package = "tracing" }
|
tracinglib = { version = "0.1.25", optional = true, package = "tracing" }
|
||||||
|
21
src/types/external/duration.rs
vendored
Normal file
21
src/types/external/duration.rs
vendored
Normal file
@ -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", specified_by_url = "https://en.wikipedia.org/wiki/ISO_8601#Durations")]
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
}
|
2
src/types/external/mod.rs
vendored
2
src/types/external/mod.rs
vendored
@ -18,6 +18,8 @@ mod bson;
|
|||||||
mod chrono_tz;
|
mod chrono_tz;
|
||||||
#[cfg(feature = "chrono")]
|
#[cfg(feature = "chrono")]
|
||||||
mod datetime;
|
mod datetime;
|
||||||
|
#[cfg(feature = "chrono-duration")]
|
||||||
|
mod duration;
|
||||||
#[cfg(feature = "decimal")]
|
#[cfg(feature = "decimal")]
|
||||||
mod decimal;
|
mod decimal;
|
||||||
#[cfg(feature = "chrono")]
|
#[cfg(feature = "chrono")]
|
||||||
|
Loading…
Reference in New Issue
Block a user