Add `skip_input` attribute to `InputObject` macro, `skip_output` attribute to `SimpleObject` macro. #797

This commit is contained in:
Sunli 2022-01-26 16:57:47 +08:00
parent e1db5954ef
commit 334b6efe99
7 changed files with 91 additions and 2 deletions

View File

@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
# [3.0.26] 2022-1-26
- Add `skip_input` attribute to `InputObject` macro, `skip_output` attribute to `SimpleObject` macro.
# [3.0.25] 2022-1-24
- Fixed some integrations overwritten HTTP headers. [#793](https://github.com/async-graphql/async-graphql/issues/793)

View File

@ -130,6 +130,11 @@ pub struct SimpleObjectField {
#[darling(default)]
pub skip: bool,
#[darling(default)]
pub skip_output: bool,
// for InputObject
#[darling(default)]
pub skip_input: bool,
#[darling(default)]
pub name: Option<String>,
#[darling(default)]
pub deprecation: Deprecation,
@ -361,6 +366,11 @@ pub struct InputObjectField {
#[darling(default)]
pub skip: bool,
#[darling(default)]
pub skip_input: bool,
// for SimpleObject
#[darling(default)]
pub skip_output: bool,
#[darling(default)]
pub visible: Option<Visible>,
#[darling(default)]
pub secret: bool,

View File

@ -65,7 +65,7 @@ pub fn generate(object_args: &args::InputObject) -> GeneratorResult<TokenStream>
.rename(ident.unraw().to_string(), RenameTarget::Field)
});
if field.skip {
if field.skip || field.skip_input {
get_fields.push(quote! {
let #ident: #ty = ::std::default::Default::default();
});

View File

@ -93,7 +93,7 @@ pub fn generate(object_args: &args::SimpleObject) -> GeneratorResult<TokenStream
}
for SimpleObjectFieldGenerator { field, derived } in &processed_fields {
if field.skip {
if field.skip || field.skip_output {
continue;
}

View File

@ -22,6 +22,7 @@ Define a GraphQL input object
| validator | Input value validator *[See also the Book](https://async-graphql.github.io/async-graphql/en/input_value_validators.html)* | object | Y |
| flatten | Similar to serde (flatten) | boolean | Y |
| skip | Skip this field, use `Default::default` to get a default value for this field. | bool | Y |
| skip_input | Skip this field, similar to `skip`, but avoids conflicts when this macro is used with `SimpleObject`. | 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 |
| secret | Mark this field as a secret, it will not output the actual value in the log. | bool | Y |

View File

@ -22,6 +22,7 @@ Similar to `Object`, but defined on a structure that automatically generates get
| Attribute | description | Type | Optional |
|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------|----------|
| skip | Skip this field | bool | Y |
| skip_output | Skip this field, similar to `skip`, but avoids conflicts when this macro is used with `InputObject`. | bool | Y |
| name | Field name | string | Y |
| deprecation | Field deprecated | bool | Y |
| deprecation | Field deprecation reason | string | Y |

View File

@ -406,3 +406,76 @@ pub async fn test_both_input_output() {
assert_eq!(<MyObject as InputType>::type_name(), "MyObjectInput");
assert_eq!(<MyObject as OutputType>::type_name(), "MyObject");
}
#[tokio::test]
pub async fn test_skip_input() {
#[derive(SimpleObject, InputObject)]
#[graphql(input_name = "MyObjectInput")]
#[allow(dead_code)]
struct MyObject {
a: i32,
#[graphql(skip_input)]
b: i32,
}
struct Query;
#[Object]
impl Query {
async fn obj(&self, input: MyObject) -> MyObject {
input
}
}
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
assert_eq!(
schema
.execute("{ obj(input: { a: 1 }) { a b } }")
.await
.into_result()
.unwrap()
.data,
value!({
"obj": {
"a": 1,
"b": 0,
}
})
);
}
#[tokio::test]
pub async fn test_skip_output() {
#[derive(SimpleObject, InputObject)]
#[graphql(input_name = "MyObjectInput")]
#[allow(dead_code)]
struct MyObject {
a: i32,
#[graphql(skip_output)]
b: i32,
}
struct Query;
#[Object]
impl Query {
async fn obj(&self, input: MyObject) -> MyObject {
input
}
}
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
assert_eq!(
schema
.execute("{ obj(input: { a: 1, b: 2 }) { a } }")
.await
.into_result()
.unwrap()
.data,
value!({
"obj": {
"a": 1,
}
})
);
}