Add some docs

This commit is contained in:
sunli 2020-04-16 11:06:09 +08:00
parent a358088d74
commit d7ff639fa3
5 changed files with 58 additions and 5 deletions

View File

@ -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::<String>
}
}
```

View File

@ -24,8 +24,8 @@ impl MyObject {
async fn value_from_db(
&self,
ctx: &Context<'_'>,
#[arg(desc = "Id of object")] id: i64)
-> FieldResult<String> {
#[arg(desc = "Id of object")] id: i64
) -> FieldResult<String> {
let conn = ctx.data::<DbPool>().take();
Ok(conn.query_something(id)?.name)
}

View File

@ -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,
}
```

View File

@ -1 +1,19 @@
# 错误处理
任何错误都能够被转换为`FieldError`,并且你还能扩展标准的错误输出。
下面是一个例子,解析一个输入的字符串到整数,当解析失败时返回错误,并且附加额外的错误信息。
```rust
struct Query;
#[Object]
impl Query {
#[field]
async fn parse_with_extensions(&self, input: String) -> FieldResult<i32> {
Ok("234a"
.parse()
.map_err(|err| err.extend_with(|_| json!({"code": 400})))?)
}
}
```

View File

@ -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;