async-graphql/docs/en/src/query_and_mutation.md

45 lines
1.0 KiB
Markdown
Raw Normal View History

2020-04-15 03:15:30 +00:00
# Query and Mutation
2020-05-09 20:56:15 +00:00
## Query root object
2020-05-13 04:49:43 +00:00
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.
2020-05-09 20:56:15 +00:00
```rust
use async_graphql::*;
struct Query;
#[Object]
impl Query {
async fn user(&self, username: String) -> FieldResult<Option<User>> {
// Look up users from the database
}
}
```
## 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
use async_graphql::*;
struct Mutation;
#[Object]
impl Mutation {
async fn signup(&self, username: String, password: String) -> Result<bool> {
// User signup
}
async fn login(&self, username: String, password: String) -> Result<String> {
// User login (generate token)
}
}
```