From fe86dbb7826c4f329496bf43431a9ca44a67135e Mon Sep 17 00:00:00 2001 From: Graeme Coupar Date: Sat, 12 Dec 2020 02:30:41 +0000 Subject: [PATCH] Mark `Arc` with `IsInputType` & `IsOutputType`. (#822) * Mark `Arc` with `IsInputType` & `IsOutputType`. We've just updated a service we work with to the latest release of juniper (it's been on a fairly old master commit for some time). In this service we have some fields that are contained within `Arc`s, which I added support for in #479. Since then it seems the new marker traits of `IsInputType` & `IsOutputType` have been added, but they do not support `Arc`, leading to: ``` error[E0277]: the trait bound `Arc: IsOutputType<__S>` is not satisfied --> src/graphql.rs:36:1 | 36 | #[juniper::graphql_object(Context=Context)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `IsOutputType<__S>` is not implemented for `Arc` | = note: required by `juniper::marker::IsOutputType::mark` = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info) ``` This commit adds support for those, mirroring the support for `Box`. I'm not sure if there's a good place to add a test for this functionality so that regressions can be avoided in the future? * Add Arc tests & FromInputValue for Arc --- integration_tests/juniper_tests/src/arc_fields.rs | 15 +++++++++++++++ integration_tests/juniper_tests/src/lib.rs | 2 ++ juniper/src/types/marker.rs | 13 +++++++++++++ juniper/src/types/pointers.rs | 13 +++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 integration_tests/juniper_tests/src/arc_fields.rs diff --git a/integration_tests/juniper_tests/src/arc_fields.rs b/integration_tests/juniper_tests/src/arc_fields.rs new file mode 100644 index 00000000..5f7799f9 --- /dev/null +++ b/integration_tests/juniper_tests/src/arc_fields.rs @@ -0,0 +1,15 @@ +use std::sync::Arc; + +struct Query; + +#[juniper::graphql_object] +impl Query { + fn ping() -> Arc { + Arc::new(false) + } +} + +#[derive(juniper::GraphQLInputObject)] +struct Ping { + expect_result: Arc, +} diff --git a/integration_tests/juniper_tests/src/lib.rs b/integration_tests/juniper_tests/src/lib.rs index 19c07da4..d4f0e94b 100644 --- a/integration_tests/juniper_tests/src/lib.rs +++ b/integration_tests/juniper_tests/src/lib.rs @@ -1,6 +1,8 @@ #[cfg(test)] mod api; #[cfg(test)] +mod arc_fields; +#[cfg(test)] mod codegen; #[cfg(test)] mod custom_scalar; diff --git a/juniper/src/types/marker.rs b/juniper/src/types/marker.rs index 4d95b761..f7207fd8 100644 --- a/juniper/src/types/marker.rs +++ b/juniper/src/types/marker.rs @@ -168,5 +168,18 @@ where { } +impl IsInputType for std::sync::Arc +where + T: IsInputType + ?Sized, + S: ScalarValue, +{ +} +impl IsOutputType for std::sync::Arc +where + T: IsOutputType + ?Sized, + S: ScalarValue, +{ +} + impl<'a, S> IsInputType for str where S: ScalarValue {} impl<'a, S> IsOutputType for str where S: ScalarValue {} diff --git a/juniper/src/types/pointers.rs b/juniper/src/types/pointers.rs index 2ed2d50a..0c3d0f7e 100644 --- a/juniper/src/types/pointers.rs +++ b/juniper/src/types/pointers.rs @@ -283,6 +283,19 @@ where } } +impl FromInputValue for Arc +where + S: ScalarValue, + T: FromInputValue, +{ + fn from_input_value(v: &InputValue) -> Option> { + match >::from_input_value(v) { + Some(v) => Some(Arc::new(v)), + None => None, + } + } +} + impl ToInputValue for Arc where S: fmt::Debug,