Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support serde_json::Value as juniper::object::Object #504

Open
UkonnRa opened this issue Jan 26, 2020 · 2 comments
Open

Support serde_json::Value as juniper::object::Object #504

UkonnRa opened this issue Jan 26, 2020 · 2 comments
Labels
enhancement Improvement of existing features or bugfix

Comments

@UkonnRa
Copy link

UkonnRa commented Jan 26, 2020

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
  1. I think that serde_json::Value and juniper::object::Object should be convertable;
  2. Maybe HashMap<String, String> and juniper::object::Object should be convertable;
  3. JSON (mapped to serde_json::Value) scalar is needed
@UkonnRa UkonnRa added the enhancement Improvement of existing features or bugfix label Jan 26, 2020
@mwilliammyers
Copy link

mwilliammyers commented Jan 27, 2020

I agree; I would love to see this feature behind a disabled by default feature flag.

See #325 and #280.

@littledivy
Copy link

@mwilliammyers What is the status of the PR? I would love this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement of existing features or bugfix
Projects
None yet
Development

No branches or pull requests

3 participants