async-graphql/docs/zh-CN/src/query_and_mutation.md
Edward Rudd 3b7ed74d11 correct doc examples so they compile
- examples to fix still
  - error_extensions.md ResultExt example does not compile!
     - trait ErrorExtensions is not implemented for ParseIntError
  - dataloader
     - requires sqlx to work. So we either "stub" it OR we rewrite them simpler to use a  simple "faux" db library
2022-06-02 17:32:12 -04:00

1.0 KiB
Raw Blame History

查询和变更

查询根对象

查询根对象是一个GraphQL对象定义类似其它对象。查询对象的所有字段Resolver函数是并发执行的。

# extern crate async_graphql;
use async_graphql::*;
# #[derive(SimpleObject)]
# struct User { a: i32 }

struct Query;

#[Object]
impl Query {
    async fn user(&self, username: String) -> Result<Option<User>> {
        // 在数据库中查找用户
#        todo!()
    }
}

变更根对象

变更根对象也是一个GraphQL但变更根对象的执行是顺序的只有第一个变更执行完成之后才会执行下一个。

下面的变更根对象提供用户注册和登录操作:

# extern crate async_graphql;
use async_graphql::*;

struct Mutation;

#[Object]
impl Mutation {
    async fn signup(&self, username: String, password: String) -> Result<bool> {
        // 用户注册
#        todo!()
    }

    async fn login(&self, username: String, password: String) -> Result<String> {
        // 用户登录并生成token
#        todo!()
    }
}