Merge pull request #578 from flisky/master

Allow field visible to support paths
This commit is contained in:
Sunli 2021-07-18 20:58:55 +08:00 committed by GitHub
commit 863380ee1f
3 changed files with 16 additions and 11 deletions

View File

@ -50,7 +50,7 @@ impl FromMeta for DefaultValue {
pub enum Visible { pub enum Visible {
None, None,
HiddenAlways, HiddenAlways,
FnName(String), FnName(Path),
} }
impl FromMeta for Visible { impl FromMeta for Visible {
@ -58,7 +58,7 @@ impl FromMeta for Visible {
match value { match value {
Lit::Bool(LitBool { value: true, .. }) => Ok(Visible::None), Lit::Bool(LitBool { value: true, .. }) => Ok(Visible::None),
Lit::Bool(LitBool { value: false, .. }) => Ok(Visible::HiddenAlways), 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::<Path>(&str.value())?)),
_ => Err(darling::Error::unexpected_lit_type(value)), _ => Err(darling::Error::unexpected_lit_type(value)),
} }
} }

View File

@ -403,8 +403,7 @@ pub fn visible_fn(visible: &Option<Visible>) -> TokenStream {
None | Some(Visible::None) => quote! { ::std::option::Option::None }, None | Some(Visible::None) => quote! { ::std::option::Option::None },
Some(Visible::HiddenAlways) => quote! { ::std::option::Option::Some(|_| false) }, Some(Visible::HiddenAlways) => quote! { ::std::option::Option::Some(|_| false) },
Some(Visible::FnName(name)) => { Some(Visible::FnName(name)) => {
let ident = Ident::new(name, Span::call_site()); quote! { ::std::option::Option::Some(#name) }
quote! { ::std::option::Option::Some(#ident) }
} }
} }
} }

View File

@ -206,16 +206,22 @@ pub async fn test_enum_value_visible() {
#[tokio::test] #[tokio::test]
pub async fn test_visible_fn() { pub async fn test_visible_fn() {
struct IsAdmin(bool); mod nested {
use async_graphql::Context;
#[derive(SimpleObject)] pub struct IsAdmin(pub bool);
#[graphql(visible = "is_admin")]
struct MyObj { pub fn is_admin(ctx: &Context<'_>) -> bool {
a: i32, ctx.data_unchecked::<IsAdmin>().0
}
} }
fn is_admin(ctx: &Context<'_>) -> bool { use nested::IsAdmin;
ctx.data_unchecked::<IsAdmin>().0
#[derive(SimpleObject)]
#[graphql(visible = "nested::is_admin")]
struct MyObj {
a: i32,
} }
struct Query; struct Query;