From d0b4e4db87d1b25fc09fb9c591818730ad03de84 Mon Sep 17 00:00:00 2001 From: Ethan Fast Date: Sat, 9 May 2020 13:56:15 -0700 Subject: [PATCH] translate section 4 (schema) --- docs/en/src/define_schema.md | 5 ++++ docs/en/src/introduction.md | 6 ++--- docs/en/src/query_and_mutation.md | 43 +++++++++++++++++++++++++++++++ docs/en/src/subscription.md | 21 +++++++++++++++ 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/docs/en/src/define_schema.md b/docs/en/src/define_schema.md index 07d8c082..a38c4a94 100644 --- a/docs/en/src/define_schema.md +++ b/docs/en/src/define_schema.md @@ -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. + diff --git a/docs/en/src/introduction.md b/docs/en/src/introduction.md index 20b0e348..9c003f78 100644 --- a/docs/en/src/introduction.md +++ b/docs/en/src/introduction.md @@ -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 diff --git a/docs/en/src/query_and_mutation.md b/docs/en/src/query_and_mutation.md index 60aa8069..251313fc 100644 --- a/docs/en/src/query_and_mutation.md +++ b/docs/en/src/query_and_mutation.md @@ -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> { + // 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 { + // User signup + } + + async fn login(&self, username: String, password: String) -> Result { + // User login (generate token) + } +} +``` + diff --git a/docs/en/src/subscription.md b/docs/en/src/subscription.md index 13dc747f..b7adb20a 100644 --- a/docs/en/src/subscription.md +++ b/docs/en/src/subscription.md @@ -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 { + let mut value = 0; + tokio::time::interval(Duration::from_secs(1)).map(move |_| { + value += step; + value + }) + } +} +```