Merge pull request #67 from DataCorrupted/master

slightly modified based on Sunli's reply.
This commit is contained in:
Sunli 2020-05-10 13:53:26 +08:00 committed by GitHub
commit 93dedf1198
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 5 additions and 5 deletions

View File

@ -6,8 +6,9 @@ Different from `SimpleObject`, `Object` must have Resolve defined for each field
Resolve is used to get the value of the field. You can query a database and return the result. **The return type of the function is the type of the field.** You can also return a `async_graphql::FieldResult` so to return an error if it occrs and error message will be send to query result.
<!--TODO: 全局类型的数据?-->
When querying a database, you may need a global data base connection pool, you can use `Schema::data` to attach a global data when creating Schema, the following `value_from_db` function showed how to retrive a database connection from `Context`.
When querying a database, you may need a global data base connection pool.
When creating `Schema`, you can use `SchemaBuilder::data` to setup `Schema` data, and `Context::data` to setup `Context`data.
The following `value_from_db` function showed how to retrive a database connection from `Context`.
```rust
use async_graphql::*;

View File

@ -6,7 +6,6 @@ Resolve can return a `FieldResult`, following is the definition:
type FieldResult<T> = std::result::Result<T, FieldError>;
```
<!--TODO: 扩展标准的错误输出? -->
Any `Error` can be converted to `FieldError` and you can extend error message.
Following example shows how to parse an input string to integer. When parsing failed, it would return error and attach error message.

View File

@ -6,7 +6,7 @@
Resolve函数用于计算字段的值你可以执行一个数据库查询并返回查询结果。**函数的返回值是字段的类型**,你也可以返回一个`async_graphql::FieldResult`类型,这样能够返回一个错误,这个错误信息将输出到查询结果中。
在查询数据库时你可能需要一个数据库连接池对象这个对象是个全局的你可以在创建Schema的时候用`Schema::data`函数附加一个全局类型的数据,下面的`value_from_db`字段展示了如何从`Context`中获取一个数据库连接。
在查询数据库时你可能需要一个数据库连接池对象这个对象是个全局的你可以在创建Schema的时候用`SchemaBuilder::data`函数设置`Schema`数据, 用`Context::data`函数设置`Context`数据。下面的`value_from_db`字段展示了如何从`Context`中获取一个数据库连接。
```rust
use async_graphql::*;

View File

@ -6,7 +6,7 @@ Resolve函数可以返回一个FieldResult类型以下是FieldResult的定义
type FieldResult<T> = std::result::Result<T, FieldError>;
```
任何错误都能够被转换为`FieldError`,并且你还能扩展标准的错误输出
任何错误都能够被转换为`FieldError`,并且你还能扩展标准的错误信息
下面是一个例子,解析一个输入的字符串到整数,当解析失败时返回错误,并且附加额外的错误信息。