Update docs
This commit is contained in:
parent
a92a619345
commit
c79e9f1b58
|
@ -11,6 +11,7 @@
|
|||
- [Interface](define_interface.md)
|
||||
- [Union](define_union.md)
|
||||
- [InputObject](define_input_object.md)
|
||||
- [Default value](default_value.md)
|
||||
- [Schema](define_schema.md)
|
||||
- [Query and Mutation](query_and_mutation.md)
|
||||
- [Subscription](subscription.md)
|
||||
|
|
|
@ -24,8 +24,8 @@ impl ScalarType for StringNumber {
|
|||
}
|
||||
}
|
||||
|
||||
fn to_json(&self) -> Result<serde_json::Value> {
|
||||
Ok(serde_json::to_value(self.0).unwrap())
|
||||
fn to_value(&self) -> Value {
|
||||
Value::String(self.0.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
60
docs/en/src/default_value.md
Normal file
60
docs/en/src/default_value.md
Normal file
|
@ -0,0 +1,60 @@
|
|||
# Default value
|
||||
|
||||
You can define default values for input value types.
|
||||
The following shows how to define default values for different types.
|
||||
|
||||
## Object field
|
||||
|
||||
```rust
|
||||
use async_graphql::*;
|
||||
|
||||
struct Query;
|
||||
|
||||
fn my_default() -> i32 {
|
||||
30
|
||||
}
|
||||
|
||||
#[Object]
|
||||
impl Query {
|
||||
// The default value of the value parameter is 0, it will call i32::default()
|
||||
fn test1(&self, #[arg(default)] value: i32) {}
|
||||
|
||||
// The default value of the value parameter is 10
|
||||
fn test2(&self, #[arg(default = 10)] value: i32) {}
|
||||
|
||||
// The default value of the value parameter uses the return result of the my_default function, the value is 30.
|
||||
fn test3(&self, #[arg(default_with = "my_default()")] value: i32) {}
|
||||
}
|
||||
```
|
||||
|
||||
## Interface field
|
||||
|
||||
```rust
|
||||
use async_graphql::*;
|
||||
|
||||
#[Interface(
|
||||
field(name = "test1", arg(name = "value", default)),
|
||||
field(name = "test2", arg(name = "value", default = 10)),
|
||||
field(name = "test3", arg(name = "value", default = "my_default()")),
|
||||
)]
|
||||
enum MyInterface {
|
||||
MyObj(MyObj),
|
||||
}
|
||||
```
|
||||
|
||||
## Input object field
|
||||
|
||||
```rust
|
||||
use async_graphql::*;
|
||||
|
||||
struct MyInputObject {
|
||||
#[field(default)]
|
||||
value1: i32,
|
||||
|
||||
#[field(default = 10)]
|
||||
value2: i32,
|
||||
|
||||
#[field(default = "my_default()")]
|
||||
value3: i32,
|
||||
}
|
||||
```
|
|
@ -11,6 +11,7 @@
|
|||
- [接口(Interface)](define_interface.md)
|
||||
- [联合(Union)](define_union.md)
|
||||
- [输入对象(InputObject)](define_input_object.md)
|
||||
- [默认值](default_value.md)
|
||||
- [定义模式(Schema)](define_schema.md)
|
||||
- [查询和变更](query_and_mutation.md)
|
||||
- [订阅](subscription.md)
|
||||
|
|
|
@ -24,8 +24,8 @@ impl ScalarType for StringNumber {
|
|||
}
|
||||
}
|
||||
|
||||
fn to_json(&self) -> Result<serde_json::Value> {
|
||||
Ok(serde_json::to_value(self.0).unwrap())
|
||||
fn to_value(&self) -> Value {
|
||||
Value::String(self.0.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
59
docs/zh-CN/src/default_value.md
Normal file
59
docs/zh-CN/src/default_value.md
Normal file
|
@ -0,0 +1,59 @@
|
|||
# 默认值
|
||||
|
||||
你可以为输入值类型定义默认值,下面展示了在不同类型上默认值的定义方法。
|
||||
|
||||
## 对象字段参数
|
||||
|
||||
```rust
|
||||
use async_graphql::*;
|
||||
|
||||
struct Query;
|
||||
|
||||
fn my_default() -> i32 {
|
||||
30
|
||||
}
|
||||
|
||||
#[Object]
|
||||
impl Query {
|
||||
// value参数的默认值为0,它会调用i32::default()
|
||||
fn test1(&self, #[arg(default)] value: i32) {}
|
||||
|
||||
// value参数的默认值为10
|
||||
fn test2(&self, #[arg(default = 10)] value: i32) {}
|
||||
|
||||
// value参数的默认值使用my_default函数的返回结果,值为30
|
||||
fn test3(&self, #[arg(default_with = "my_default()")] value: i32) {}
|
||||
}
|
||||
```
|
||||
|
||||
## 接口字段参数
|
||||
|
||||
```rust
|
||||
use async_graphql::*;
|
||||
|
||||
#[Interface(
|
||||
field(name = "test1", arg(name = "value", default)),
|
||||
field(name = "test2", arg(name = "value", default = 10)),
|
||||
field(name = "test3", arg(name = "value", default = "my_default()")),
|
||||
)]
|
||||
enum MyInterface {
|
||||
MyObj(MyObj),
|
||||
}
|
||||
```
|
||||
|
||||
## 输入对象(InputObject)
|
||||
|
||||
```rust
|
||||
use async_graphql::*;
|
||||
|
||||
struct MyInputObject {
|
||||
#[field(default)]
|
||||
value1: i32,
|
||||
|
||||
#[field(default = 10)]
|
||||
value2: i32,
|
||||
|
||||
#[field(default = "my_default()")]
|
||||
value3: i32,
|
||||
}
|
||||
```
|
|
@ -12,7 +12,7 @@ struct Coordinate {
|
|||
latitude: f64,
|
||||
|
||||
#[field(desc = "...")]
|
||||
longitude: f64
|
||||
longitude: f64,
|
||||
}
|
||||
|
||||
struct Mutation;
|
||||
|
|
Loading…
Reference in New Issue
Block a user