async-graphql/docs/zh-CN/src/query_and_mutation.md

44 lines
918 B
Markdown
Raw Normal View History

2020-04-15 03:15:30 +00:00
# 查询和变更
2020-04-16 09:06:46 +00:00
## 查询根对象
2020-05-13 04:49:43 +00:00
查询根对象是一个GraphQL对象定义类似其它对象。查询对象的所有字段Resolver函数是并发执行的。
2020-04-16 09:06:46 +00:00
```rust
use async_graphql::*;
struct Query;
2020-09-13 04:12:32 +00:00
#[GQLObject]
2020-04-16 09:06:46 +00:00
impl Query {
async fn user(&self, username: String) -> FieldResult<Option<User>> {
// 在数据库中查找用户
}
}
```
## 变更根对象
变更根对象也是一个GraphQL但变更根对象的执行是顺序的只有第一个变更执行完成之后才会执行下一个。
下面的变更根对象提供用户注册和登录操作:
```rust
use async_graphql::*;
struct Mutation;
2020-09-13 04:12:32 +00:00
#[GQLObject]
2020-04-16 09:06:46 +00:00
impl Mutation {
async fn signup(&self, username: String, password: String) -> Result<bool> {
// 用户注册
}
async fn login(&self, username: String, password: String) -> Result<String> {
// 用户登录并生成token
}
}
```