From c40d80f26bb9cf59bf1e99099444999b6d5ef51d Mon Sep 17 00:00:00 2001 From: Cerber-Ursi <computers05@mail.ru> Date: Wed, 13 Apr 2022 20:15:50 +0700 Subject: [PATCH] Fix expansion of procedural macros inside `macro_rules!` (#1054, #1051) --- juniper/CHANGELOG.md | 3 ++ juniper_codegen/CHANGELOG.md | 6 ++++ juniper_codegen/src/common/parse/mod.rs | 1 + tests/integration/src/inside_macro.rs | 39 +++++++++++++++++++++++++ tests/integration/src/lib.rs | 2 ++ 5 files changed, 51 insertions(+) create mode 100644 tests/integration/src/inside_macro.rs diff --git a/juniper/CHANGELOG.md b/juniper/CHANGELOG.md index 60036119..149b7e15 100644 --- a/juniper/CHANGELOG.md +++ b/juniper/CHANGELOG.md @@ -65,6 +65,7 @@ All user visible changes to `juniper` crate will be documented in this file. Thi - Unsupported spreading GraphQL interface fragments on unions and other interfaces. ([#965], [#798]) - Unsupported expressions in `graphql_value!` macro. ([#996], [#503]) - Incorrect GraphQL list coercion rules: `null` cannot be coerced to an `[Int!]!` or `[Int]!`. ([#1004]) +- All procedural macros expansion inside `macro_rules!`. ([#1054], [#1051]) [#503]: /../../issues/503 [#750]: /../../issues/750 @@ -89,6 +90,8 @@ All user visible changes to `juniper` crate will be documented in this file. Thi [#1017]: /../../pull/1017 [#1025]: /../../pull/1025 [#1026]: /../../pull/1026 +[#1051]: /../../issues/1051 +[#1054]: /../../pull/1054 diff --git a/juniper_codegen/CHANGELOG.md b/juniper_codegen/CHANGELOG.md index 9afc8143..2ce86c70 100644 --- a/juniper_codegen/CHANGELOG.md +++ b/juniper_codegen/CHANGELOG.md @@ -37,6 +37,10 @@ All user visible changes to `juniper_codegen` crate will be documented in this f - `#[derive(GraphQLInterface)]` macro allowing using structs as GraphQL interfaces. ([#1026]) +### Fixed + +- All procedural macros expansion inside `macro_rules!`. ([#1054], [#1051]) + [#971]: /../../pull/971 [#985]: /../../pull/985 [#987]: /../../pull/987 @@ -47,6 +51,8 @@ All user visible changes to `juniper_codegen` crate will be documented in this f [#1017]: /../../pull/1017 [#1025]: /../../pull/1025 [#1026]: /../../pull/1026 +[#1051]: /../../issues/1051 +[#1054]: /../../pull/1054 diff --git a/juniper_codegen/src/common/parse/mod.rs b/juniper_codegen/src/common/parse/mod.rs index c9c79dc7..eaa946d9 100644 --- a/juniper_codegen/src/common/parse/mod.rs +++ b/juniper_codegen/src/common/parse/mod.rs @@ -127,6 +127,7 @@ impl TypeExt for syn::Type { fn unparenthesized(&self) -> &Self { match self { Self::Paren(ty) => ty.elem.unparenthesized(), + Self::Group(ty) => ty.elem.unparenthesized(), ty => ty, } } diff --git a/tests/integration/src/inside_macro.rs b/tests/integration/src/inside_macro.rs new file mode 100644 index 00000000..f94b6f99 --- /dev/null +++ b/tests/integration/src/inside_macro.rs @@ -0,0 +1,39 @@ +//! Checks that `#[graphql_object]` macro correctly expands inside a declarative +//! macro definition. +//! See [#1051](https://github.com/graphql-rust/juniper/pull/1051) and +//! [#1054](https://github.com/graphql-rust/juniper/pull/1054) for details. + +use juniper::{ + graphql_object, graphql_value, graphql_vars, EmptyMutation, EmptySubscription, RootNode, +}; + +macro_rules! impl_id { + ($typename:ident) => { + #[graphql_object] + impl $typename { + fn id(&self) -> i32 { + 42 + } + } + }; +} + +struct Unit; +impl_id!(Unit); + +#[tokio::test] +async fn works() { + let query = r#" + query Unit { + id + } + "#; + + let schema = RootNode::new(Unit, EmptyMutation::new(), EmptySubscription::new()); + let (res, errs) = juniper::execute(query, None, &schema, &graphql_vars! {}, &()) + .await + .unwrap(); + + assert_eq!(errs.len(), 0); + assert_eq!(res, graphql_value!({"id": 42})); +} diff --git a/tests/integration/src/lib.rs b/tests/integration/src/lib.rs index 2e59d8c3..9b283001 100644 --- a/tests/integration/src/lib.rs +++ b/tests/integration/src/lib.rs @@ -11,6 +11,8 @@ mod explicit_null; #[cfg(test)] mod infallible_as_field_error; #[cfg(test)] +mod inside_macro; +#[cfg(test)] mod issue_371; #[cfg(test)] mod issue_372;