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 {
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::<Path>(&str.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 },
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) }
}
}
}

View File

@ -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::<IsAdmin>().0
}
}
fn is_admin(ctx: &Context<'_>) -> bool {
ctx.data_unchecked::<IsAdmin>().0
use nested::IsAdmin;
#[derive(SimpleObject)]
#[graphql(visible = "nested::is_admin")]
struct MyObj {
a: i32,
}
struct Query;