diff --git a/changelog/0.9.0.md b/changelog/0.9.0.md index bc9e2f7a..1b67aa9b 100644 --- a/changelog/0.9.0.md +++ b/changelog/0.9.0.md @@ -1,4 +1,5 @@ -# [0.9.0] 2017-12-03 + +## [0.9.0] 2017-12-03 ## Changes @@ -26,11 +27,16 @@ The iron and rocket integrations were removed from the main crate, and are now available via the [juniper_iron](https://crates.io/crates/juniper_iron) and [juniper_rocket](https://crates.io/crates/juniper_rocket) crates. -### FieldError - Custom data +### FieldError rewrite (custom data) The `FieldError` type now supports custom data with the `Value` type from serde_json. Use this to populate the `data` field in returned errors. +This also means that try! and `?` now work in resolvers, which is quite nice. + +Also, the `ResultExt` extension and the `jtry!` macro were removed, since they +are redundant now! + ### Dynamic Schemas Juniper has gained support for dynamic schemas, thanks to @srijs. diff --git a/juniper/src/executor_tests/executor.rs b/juniper/src/executor_tests/executor.rs index 3357b7b8..ceca4b31 100644 --- a/juniper/src/executor_tests/executor.rs +++ b/juniper/src/executor_tests/executor.rs @@ -216,7 +216,6 @@ mod dynamic_context_switching { use schema::model::RootNode; use parser::SourcePosition; use executor::{Context, ExecutionError, FieldError, FieldResult}; - use result_ext::ResultExt; struct Schema; @@ -239,19 +238,18 @@ mod dynamic_context_switching { } field item_res(&executor, key: i32) -> FieldResult<(&InnerContext, ItemRef)> { - executor.context().items.get(&key) + let res = executor.context().items.get(&key) .ok_or(format!("Could not find key {}", key)) - .map(|c| (c, ItemRef)) - .to_field_result() + .map(|c| (c, ItemRef))?; + Ok(res) } field item_res_opt(&executor, key: i32) -> FieldResult> { if key > 100 { - Err(format!("Key too large: {}", key)).to_field_result() - } else { - Ok(executor.context().items.get(&key) - .map(|c| (c, ItemRef))) + Err(format!("Key too large: {}", key))?; } + Ok(executor.context().items.get(&key) + .map(|c| (c, ItemRef))) } field item_always(&executor, key: i32) -> (&InnerContext, ItemRef) { diff --git a/juniper/src/lib.rs b/juniper/src/lib.rs index ede56a78..90595b3f 100644 --- a/juniper/src/lib.rs +++ b/juniper/src/lib.rs @@ -133,8 +133,6 @@ mod executor; pub mod integrations; pub mod graphiql; pub mod http; -#[macro_use] -mod result_ext; #[cfg(all(test, not(feature = "expose-test-schema")))] mod tests; @@ -159,7 +157,6 @@ pub use executor::{Context, ExecutionError, ExecutionResult, Executor, FieldErro pub use validation::RuleError; pub use types::scalars::{EmptyMutation, ID}; pub use schema::model::RootNode; -pub use result_ext::ResultExt; pub use schema::meta; diff --git a/juniper/src/result_ext.rs b/juniper/src/result_ext.rs deleted file mode 100644 index 864ee472..00000000 --- a/juniper/src/result_ext.rs +++ /dev/null @@ -1,65 +0,0 @@ -use std::fmt; -use std::result::Result; - -use FieldError; - -/** -Helper trait to produce `FieldResult`s - -`FieldResult` only have strings as errors as that's what's going out -in the GraphQL response. As such, all errors must be manually -converted to strings. Importing the `ResultExt` macro and using its -only method `to_field_result` can help with that: - -```rust -use std::str::FromStr; -use juniper::{FieldResult, ResultExt}; - -fn sample_fn(s: &str) -> FieldResult { - i32::from_str(s).to_field_result() -} - -# fn main() { assert_eq!(sample_fn("12"), Ok(12)); } -``` - -Alternatively, you can use the `jtry!` macro in all places you'd -normally use the regular `try!` macro: - -```rust -#[macro_use] extern crate juniper; - -use std::str::FromStr; - -use juniper::{FieldResult, ResultExt}; - -fn sample_fn(s: &str) -> FieldResult { - let value = jtry!(i32::from_str(s)); - - Ok(value) -} - -# fn main() { assert_eq!(sample_fn("12"), Ok(12)); } -``` - - */ -pub trait ResultExt { - /// Convert the error to a string by using it's `Display` implementation - fn to_field_result(self) -> Result; -} - -impl ResultExt for Result { - fn to_field_result(self) -> Result { - self.map_err(|e| FieldError::from(e)) - } -} - -/** -Helper macro to produce `FieldResult`s. - -See the documentation for the [`ResultExt`](trait.ResultExt.html) -trait. - */ -#[macro_export] -macro_rules! jtry { - ( $e:expr ) => { try!($crate::ResultExt::to_field_result($e)) } -}