diff --git a/docs/zh-CN/src/context b/docs/zh-CN/src/context index fa1ae017..7b4e2e82 100644 --- a/docs/zh-CN/src/context +++ b/docs/zh-CN/src/context @@ -1 +1,19 @@ # 查询上下文(Context) + +查询上下文(Context)的主要作用是获取附加到Schema的全局数据,**需要注意的是,如果你的Resolve函数返回的数据借用了Context内保存的数据,需要明确指定生命周期参数**。 + +下面是一个返回值借用Context内数据的例子: + +```rust +struct Query; + +#[Object] +impl Query { + async fn borrow_from_context_data<'ctx'>( + &self, + ctx: &'ctx Context<'_> + ) -> &'ctx String { + ctx.data:: + } +} +``` \ No newline at end of file diff --git a/docs/zh-CN/src/define_complex_object b/docs/zh-CN/src/define_complex_object index 5eb1805d..f2259b8f 100644 --- a/docs/zh-CN/src/define_complex_object +++ b/docs/zh-CN/src/define_complex_object @@ -24,8 +24,8 @@ impl MyObject { async fn value_from_db( &self, ctx: &Context<'_'>, - #[arg(desc = "Id of object")] id: i64) - -> FieldResult { + #[arg(desc = "Id of object")] id: i64 + ) -> FieldResult { let conn = ctx.data::().take(); Ok(conn.query_something(id)?.name) } diff --git a/docs/zh-CN/src/define_enum b/docs/zh-CN/src/define_enum index 9fe9f988..07cb13f6 100644 --- a/docs/zh-CN/src/define_enum +++ b/docs/zh-CN/src/define_enum @@ -1 +1,20 @@ # 枚举(Enum) + +定义一个枚举相当简单,直接给出一个例子。 + +**Async-graphql会自动把枚举项的名称转换为GraphQL标准的大写加下划线形式,你也可以用`name`属性自已定义名称。** + +```rust +#[Enum(desc = "One of the films in the Star Wars Trilogy")] +pub enum Episode { + #[item(desc = "Released in 1977.")] + NewHope, + + #[item(desc = "Released in 1980.")] + Empire, + + // rename to `AAA` + #[item(name="AAA", desc = "Released in 1983.")] + Jedi, +} +``` \ No newline at end of file diff --git a/docs/zh-CN/src/error_handling b/docs/zh-CN/src/error_handling index a27f18e2..5ab1868c 100644 --- a/docs/zh-CN/src/error_handling +++ b/docs/zh-CN/src/error_handling @@ -1 +1,19 @@ # 错误处理 + +任何错误都能够被转换为`FieldError`,并且你还能扩展标准的错误输出。 + +下面是一个例子,解析一个输入的字符串到整数,当解析失败时返回错误,并且附加额外的错误信息。 + +```rust +struct Query; + +#[Object] +impl Query { + #[field] + async fn parse_with_extensions(&self, input: String) -> FieldResult { + Ok("234a" + .parse() + .map_err(|err| err.extend_with(|_| json!({"code": 400})))?) + } +} +``` \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 32c96439..7a7e7a27 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -106,7 +106,7 @@ pub use serde_json; pub mod http; pub use base::{Scalar, Type}; -pub use context::{Context, Environment, QueryPathNode, QueryPathSegment, Variables}; +pub use context::{Context, ContextBase, Environment, QueryPathNode, QueryPathSegment, Variables}; pub use error::{ Error, ErrorExtensions, FieldError, FieldResult, ParseRequestError, QueryError, ResultExt, }; @@ -137,8 +137,6 @@ pub mod registry; #[doc(hidden)] pub use base::{BoxFieldFuture, InputObjectType, InputValueType, ObjectType, OutputValueType}; #[doc(hidden)] -pub use context::ContextBase; -#[doc(hidden)] pub use resolver::{collect_fields, do_resolve}; #[doc(hidden)] pub use subscription::SubscriptionType;