3b7ed74d11
- 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
50 lines
1.2 KiB
Markdown
50 lines
1.2 KiB
Markdown
# Query and Mutation
|
|
|
|
## Query root object
|
|
|
|
The query root object is a GraphQL object with a definition similar to other objects. Resolver functions for all fields of the query object are executed concurrently.
|
|
|
|
```rust
|
|
# 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>> {
|
|
// Look up users from the database
|
|
# todo!()
|
|
}
|
|
}
|
|
|
|
```
|
|
|
|
## Mutation root object
|
|
|
|
The mutation root object is also a GraphQL object, but it executes sequentially. One mutation following from another will only be executed only after the first mutation is completed.
|
|
|
|
The following mutation root object provides an example of user registration and login:
|
|
|
|
```rust
|
|
# extern crate async_graphql;
|
|
use async_graphql::*;
|
|
|
|
struct Mutation;
|
|
|
|
#[Object]
|
|
impl Mutation {
|
|
async fn signup(&self, username: String, password: String) -> Result<bool> {
|
|
// User signup
|
|
# todo!()
|
|
}
|
|
|
|
async fn login(&self, username: String, password: String) -> Result<String> {
|
|
// User login (generate token)
|
|
# todo!()
|
|
}
|
|
}
|
|
```
|