Add decimal scalar

This commit is contained in:
Bram Hoendervangers 2021-07-01 22:52:37 +02:00
parent 199f599cd5
commit 6fb324182b
4 changed files with 24 additions and 0 deletions

View File

@ -20,6 +20,7 @@ unblock = ["blocking"]
string_number = ["num-traits"]
dataloader = ["futures-timer", "futures-channel", "lru"]
tracing = ["tracinglib", "tracing-futures"]
decimal = ["rust_decimal"]
[dependencies]
async-graphql-derive = { path = "derive", version = "=2.9.5" }
@ -53,6 +54,7 @@ tracing-futures = { version = "0.2.5", optional = true, features = ["std-future"
opentelemetry = { version = "0.13.0", optional = true }
url = { version = "2.2.1", optional = true }
uuid = { version = "0.8.2", optional = true, features = ["v4", "serde"] }
rust_decimal = { version = "1.14.3", optional = true }
# Non-feature optional dependencies
blocking = { version = "1.0.2", optional = true }

View File

@ -71,6 +71,7 @@
//! - `uuid`: Integrate with the [`uuid` crate](https://crates.io/crates/uuid).
//! - `string_number`: Enable the [StringNumber](types/struct.StringNumber.html).
//! - `dataloader`: Support [DataLoader](dataloader/struct.DataLoader.html).
//! - `decimal`: Integrate with the [`rust_decimal` crate](https://crates.io/crates/rust_decimal).
//!
//! ## Integrations
//!

19
src/types/external/decimal.rs vendored Normal file
View File

@ -0,0 +1,19 @@
use std::str::FromStr;
use rust_decimal::Decimal;
use crate::{InputValueError, InputValueResult, Scalar, ScalarType, Value};
#[Scalar(internal, name = "Decimal")]
impl ScalarType for Decimal {
fn parse(value: Value) -> InputValueResult<Self> {
match &value {
Value::String(s) => Ok(Decimal::from_str(s)?),
_ => Err(InputValueError::expected_type(value)),
}
}
fn to_value(&self) -> Value {
Value::String(self.to_string())
}
}

View File

@ -17,6 +17,8 @@ mod bson;
mod chrono_tz;
#[cfg(feature = "chrono")]
mod datetime;
#[cfg(feature = "decimal")]
mod decimal;
#[cfg(feature = "chrono")]
mod naive_time;
#[cfg(feature = "secrecy")]