2020-05-26 15:27:50 +00:00
|
|
|
|
# 默认值
|
|
|
|
|
|
|
|
|
|
你可以为输入值类型定义默认值,下面展示了在不同类型上默认值的定义方法。
|
|
|
|
|
|
|
|
|
|
## 对象字段参数
|
|
|
|
|
|
|
|
|
|
```rust
|
2022-06-02 21:32:12 +00:00
|
|
|
|
# extern crate async_graphql;
|
2020-05-26 15:27:50 +00:00
|
|
|
|
use async_graphql::*;
|
|
|
|
|
|
|
|
|
|
struct Query;
|
|
|
|
|
|
|
|
|
|
fn my_default() -> i32 {
|
|
|
|
|
30
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
|
#[Object]
|
2020-05-26 15:27:50 +00:00
|
|
|
|
impl Query {
|
|
|
|
|
// value参数的默认值为0,它会调用i32::default()
|
2022-06-02 21:32:12 +00:00
|
|
|
|
async fn test1(&self, #[graphql(default)] value: i32) -> i32 { todo!() }
|
2021-01-19 09:53:29 +00:00
|
|
|
|
|
2020-05-26 15:27:50 +00:00
|
|
|
|
// value参数的默认值为10
|
2022-06-02 21:32:12 +00:00
|
|
|
|
async fn test2(&self, #[graphql(default = 10)] value: i32) -> i32 { todo!() }
|
2020-05-26 15:27:50 +00:00
|
|
|
|
|
|
|
|
|
// value参数的默认值使用my_default函数的返回结果,值为30
|
2022-06-02 21:32:12 +00:00
|
|
|
|
async fn test3(&self, #[graphql(default_with = "my_default()")] value: i32) -> i32 { todo!() }
|
2020-05-26 15:27:50 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 接口字段参数
|
|
|
|
|
|
|
|
|
|
```rust
|
2022-06-02 21:32:12 +00:00
|
|
|
|
# extern crate async_graphql;
|
|
|
|
|
# fn my_default() -> i32 { 5 }
|
|
|
|
|
# struct MyObj;
|
|
|
|
|
# #[Object]
|
|
|
|
|
# impl MyObj {
|
|
|
|
|
# async fn test1(&self, value: i32) -> i32 { todo!() }
|
|
|
|
|
# async fn test2(&self, value: i32) -> i32 { todo!() }
|
|
|
|
|
# async fn test3(&self, value: i32) -> i32 { todo!() }
|
|
|
|
|
# }
|
2020-05-26 15:27:50 +00:00
|
|
|
|
use async_graphql::*;
|
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
|
#[derive(Interface)]
|
2020-09-13 04:12:32 +00:00
|
|
|
|
#[graphql(
|
2022-06-02 21:32:12 +00:00
|
|
|
|
field(name = "test1", type = "i32", arg(name = "value", type = "i32", default)),
|
|
|
|
|
field(name = "test2", type = "i32", arg(name = "value", type = "i32", default = 10)),
|
|
|
|
|
field(name = "test3", type = "i32", arg(name = "value", type = "i32", default_with = "my_default()")),
|
2020-05-26 15:27:50 +00:00
|
|
|
|
)]
|
|
|
|
|
enum MyInterface {
|
|
|
|
|
MyObj(MyObj),
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 输入对象(InputObject)
|
|
|
|
|
|
|
|
|
|
```rust
|
2022-06-02 21:32:12 +00:00
|
|
|
|
# extern crate async_graphql;
|
|
|
|
|
# fn my_default() -> i32 { 5 }
|
2020-05-26 15:27:50 +00:00
|
|
|
|
use async_graphql::*;
|
|
|
|
|
|
2021-10-06 16:24:46 +00:00
|
|
|
|
#[derive(InputObject)]
|
2020-05-26 15:27:50 +00:00
|
|
|
|
struct MyInputObject {
|
2020-09-28 09:44:00 +00:00
|
|
|
|
#[graphql(default)]
|
2020-05-26 15:27:50 +00:00
|
|
|
|
value1: i32,
|
2021-01-19 09:53:29 +00:00
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
|
#[graphql(default = 10)]
|
2020-05-26 15:27:50 +00:00
|
|
|
|
value2: i32,
|
|
|
|
|
|
2021-01-19 09:53:29 +00:00
|
|
|
|
#[graphql(default_with = "my_default()")]
|
2020-05-26 15:27:50 +00:00
|
|
|
|
value3: i32,
|
|
|
|
|
}
|
2021-01-19 09:53:29 +00:00
|
|
|
|
```
|