Add big decimal support to juniper

This commit is contained in:
Anna Baldwin 2018-04-06 15:50:26 -06:00
parent 63d8a3d1a0
commit 177bf86a52
4 changed files with 40 additions and 0 deletions

View file

@ -26,6 +26,7 @@ path = "benches/bench.rs"
nightly = []
expose-test-schema = []
default = [
"bigdecimal",
"chrono",
"url",
"uuid"
@ -39,6 +40,7 @@ ordermap = { version = "0.2.11", features = ["serde-1"] }
serde = { version = "1.0.8" }
serde_derive = {version="1.0.2" }
bigdecimal = { version = "0.0.10", optional = true }
chrono = { version = "0.4.0", optional = true }
serde_json = { version="1.0.2", optional = true }
url = { version = "1.5.1", optional = true }

View file

@ -0,0 +1,32 @@
use bigdecimal::BigDecimal;
use Value;
graphql_scalar!(BigDecimal {
description: "BigDecimal"
resolve(&self) -> Value {
Value::string(self.to_string())
}
from_input_value(v: &InputValue) -> Option<BigDecimal> {
v.as_string_value()
.and_then(|s| s.parse::<BigDecimal>().ok())
}
});
#[cfg(test)]
mod test {
use bigdecimal::BigDecimal;
#[test]
fn bigdecimal_from_input_value() {
let raw = "-10.1910";
let input = ::InputValue::String(raw.to_string());
let parsed: BigDecimal = ::FromInputValue::from_input_value(&input).unwrap();
let bigdecimal = raw.parse::<BigDecimal>().unwrap();
assert_eq!(parsed, bigdecimal);
}
}

View file

@ -2,6 +2,10 @@
#[doc(hidden)]
pub mod serde;
#[cfg(feature = "bigdecimal")]
/// GraphQL support for [bigdecimal](https://github.com/akubera/bigdecimal-rs) types.
pub mod bigdecimal;
#[cfg(feature = "chrono")]
/// GraphQL support for [chrono](https://github.com/chronotope/chrono) types.
pub mod chrono;

View file

@ -100,6 +100,8 @@ extern crate serde_json;
extern crate fnv;
extern crate ordermap;
extern crate bigdecimal;
#[cfg(any(test, feature = "chrono"))]
extern crate chrono;