From 7e21cfa94406083021d0913225dbb8b1d6593b8e Mon Sep 17 00:00:00 2001 From: Yin Jifeng Date: Sun, 18 Jul 2021 20:14:22 +0800 Subject: [PATCH] Allow field visible to support paths --- derive/src/args.rs | 4 ++-- derive/src/utils.rs | 3 +-- tests/introspection_visible.rs | 20 +++++++++++++------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/derive/src/args.rs b/derive/src/args.rs index 09bbc89d..ba31c4ec 100644 --- a/derive/src/args.rs +++ b/derive/src/args.rs @@ -50,7 +50,7 @@ impl FromMeta for DefaultValue { pub enum Visible { None, HiddenAlways, - FnName(String), + FnName(Path), } impl FromMeta for Visible { @@ -58,7 +58,7 @@ impl FromMeta for Visible { match value { Lit::Bool(LitBool { value: true, .. }) => Ok(Visible::None), Lit::Bool(LitBool { value: false, .. }) => Ok(Visible::HiddenAlways), - Lit::Str(str) => Ok(Visible::FnName(str.value())), + Lit::Str(str) => Ok(Visible::FnName(syn::parse_str::(&str.value())?)), _ => Err(darling::Error::unexpected_lit_type(value)), } } diff --git a/derive/src/utils.rs b/derive/src/utils.rs index a1fbbc2f..2b10acaa 100644 --- a/derive/src/utils.rs +++ b/derive/src/utils.rs @@ -403,8 +403,7 @@ pub fn visible_fn(visible: &Option) -> TokenStream { None | Some(Visible::None) => quote! { ::std::option::Option::None }, Some(Visible::HiddenAlways) => quote! { ::std::option::Option::Some(|_| false) }, Some(Visible::FnName(name)) => { - let ident = Ident::new(name, Span::call_site()); - quote! { ::std::option::Option::Some(#ident) } + quote! { ::std::option::Option::Some(#name) } } } } diff --git a/tests/introspection_visible.rs b/tests/introspection_visible.rs index 955e70d3..a8edad25 100644 --- a/tests/introspection_visible.rs +++ b/tests/introspection_visible.rs @@ -206,16 +206,22 @@ pub async fn test_enum_value_visible() { #[tokio::test] pub async fn test_visible_fn() { - struct IsAdmin(bool); + mod nested { + use async_graphql::Context; - #[derive(SimpleObject)] - #[graphql(visible = "is_admin")] - struct MyObj { - a: i32, + pub struct IsAdmin(pub bool); + + pub fn is_admin(ctx: &Context<'_>) -> bool { + ctx.data_unchecked::().0 + } } - fn is_admin(ctx: &Context<'_>) -> bool { - ctx.data_unchecked::().0 + use nested::IsAdmin; + + #[derive(SimpleObject)] + #[graphql(visible = "nested::is_admin")] + struct MyObj { + a: i32, } struct Query;