Open UkonnRa opened 4 years ago
Say when writing a custom error:
#[derive(thiserror::Error, Debug, Deserialize, Serialize)] #[serde(tag = "type", content = "data")] enum SomeError { #[error("SomeError Item: arg1: {arg1}, arg2: {arg2}")] Item { arg1: String, arg2: i32 }, }
I will return to the frontend as:
{ ..., "extensions": { // this field is serialized from serde "data": { "arg1": "<some-arg1>", "arg2": "<some-arg2>" } } }
So I write:
impl IntoFieldError for SomeError { fn into_field_error(self) -> juniper::FieldError { let data = serde_json::to_value(&self) .ok() .and_then(|j| j.get("data").cloned()) .unwrap_or_default(); juniper::FieldError::new( self.to_string(), juniper::Value::object( vec![ ("type", juniper::Value::scalar("SomeError")), ("data", juniper::Value::object(data.into())), ] .into_iter() .collect(), ), ) }
But of course, it won't compile, for:
the trait bound `juniper::Object<_>: std::convert::From<serde_json::value::Value>` is not satisfied
serde_json::Value
juniper::object::Object
HashMap<String, String>
JSON
I agree; I would love to see this feature behind a disabled by default feature flag.
See #325 and #280.
@mwilliammyers What is the status of the PR? I would love this feature.
Say when writing a custom error:
I will return to the frontend as:
So I write:
But of course, it won't compile, for:
serde_json::Value
andjuniper::object::Object
should be convertable;HashMap<String, String>
andjuniper::object::Object
should be convertable;JSON
(mapped to serde_json::Value) scalar is needed