Add the skip attribute to the InputObject fields.

This commit is contained in:
Sunli 2020-12-15 20:29:59 +08:00
parent 004ff93557
commit fe24247c9d
4 changed files with 44 additions and 2 deletions

View File

@ -238,6 +238,8 @@ pub struct InputObjectField {
#[darling(default)]
pub flatten: bool,
#[darling(default)]
pub skip: bool,
#[darling(default)]
pub visible: Option<Visible>,
}

View File

@ -61,6 +61,14 @@ pub fn generate(object_args: &args::InputObject) -> GeneratorResult<TokenStream>
.rename(ident.unraw().to_string(), RenameTarget::Field)
});
if field.skip {
get_fields.push(quote! {
let #ident: #ty = ::std::default::Default::default();
});
fields.push(ident);
continue;
}
if field.flatten {
flatten_fields.push((ident, ty));

View File

@ -495,8 +495,9 @@ pub use async_graphql_derive::Enum;
/// | default_with | Expression to generate default value | code string | Y |
/// | validator | Input value validator | [`InputValueValidator`](validators/trait.InputValueValidator.html) | Y |
/// | flatten | Similar to serde (flatten) | boolean | Y |
/// | visible | If `false`, it will not be displayed in introspection. *[See also the Book](https://async-graphql.github.io/async-graphql/en/visibility.html).* | bool | Y |
/// | visible | Call the specified function. If the return value is `false`, it will not be displayed in introspection. | string | Y |
/// | skip | Skip this field, use `Default::default` to get a default value for this field. | bool | Y |
/// | visible | If `false`, it will not be displayed in introspection. *[See also the Book](https://async-graphql.github.io/async-graphql/en/visibility.html).* | bool | Y |
/// | visible | Call the specified function. If the return value is `false`, it will not be displayed in introspection. | string | Y |
///
/// # Examples
///

View File

@ -287,3 +287,34 @@ pub async fn test_inputobject_flatten_multiple() {
})
);
}
#[async_std::test]
pub async fn test_input_object_skip_field() {
#[derive(InputObject)]
struct MyInput2 {
a: i32,
#[graphql(skip)]
b: i32,
}
struct Root;
#[Object]
impl Root {
async fn a(&self, input: MyInput2) -> i32 {
assert_eq!(input.b, i32::default());
input.a
}
}
let schema = Schema::new(Root, EmptyMutation, EmptySubscription);
let query = r#"{
a(input:{a: 777})
}"#;
assert_eq!(
schema.execute(query).await.into_result().unwrap().data,
value!({
"a": 777
})
);
}