translate section 4 (schema)

This commit is contained in:
Ethan Fast 2020-05-09 13:56:15 -07:00
parent 0d540465a5
commit d0b4e4db87
4 changed files with 72 additions and 3 deletions

View File

@ -1 +1,6 @@
# Schema
After defining the basic types, you need to define a schema to combine them. The schema consists of three types: a query object, mutation object, and subscription object, where the mutation object and subscription object are optional.
When the schema is created, `Async-Graphql` will traverse all object graphs and register all types. This means that if a GraphQL object is defined but never referenced, then this object will not be exposed in the schema.

View File

@ -1,12 +1,12 @@
# Introduction
`Async-graphql` is a GraphQL server-side library implemented in the Rust language. It is fully compatible with the GraphQL specification and most of its extensions, type-safe, and high-performance.
`Async-graphql` is a GraphQL server-side library implemented in Rust. It is fully compatible with the GraphQL specification and most of its extensions, and offers type safety and high performance.
You can define the Schema in the Rust language, the procedural macros automatically generate the framework code for the GraphQL query, and the lack of extending Rust's syntax means that Rustfmt can be used normally, which I value very much, which is one of the reasons why I developed `Async-graphql`.
You can define a Schema in Rust and procedural macros will automatically generate code for a GraphQL query. This library does not extend Rust's syntax, which means that Rustfmt can be used normally. I value this highly and it is one of the reasons why I developed `Async-graphql`.
## Why do this?
I like GraphQL and Rust. I've been using `Juniper` before, which solved my problem of implementing the GraphQL server with Rust, but it has some regrets, the most important of which is that it didn't support async/await at the time, so I decided to make one for myself.
I like GraphQL and Rust. I've been using `Juniper`, which solves the problem of implementing a GraphQL server with Rust. But Juniper had several problems, the most important of which is that it didn't support async/await at the time. So I decided to make this library for myself.
## Progress

View File

@ -1 +1,44 @@
# Query and Mutation
## Query root object
The query root object is a GraphQL object with a definition similar to other objects. Resolve functions for all fields of the query object are executed concurrently.
```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)
}
}
```

View File

@ -1 +1,22 @@
# Subscription
The definition of the subscription root object is slightly different from other root objects. Its Resolve function always returns a Stream, and the field parameters are usually used as data filtering conditions.
The following example subscribes to an integer stream, which generates one integer per second. The parameter step specifies the integer step size with a default of 1
```rust
use async_graphql::*;
struct Subscription;
#[Subscription]
impl Subscription {
async fn integers(&self, #[arg(default = "1")] step: i32) -> impl Stream<Item = i32> {
let mut value = 0;
tokio::time::interval(Duration::from_secs(1)).map(move |_| {
value += step;
value
})
}
}
```