slightly modified based on Sunli's reply.
Signed-off-by: Peter Rong <PeterRong96@gmail.com>
This commit is contained in:
parent
7591ef81bc
commit
318be22c79
|
@ -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::*;
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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::*;
|
||||
|
|
|
@ -6,7 +6,7 @@ Resolve函数可以返回一个FieldResult类型,以下是FieldResult的定义
|
|||
type FieldResult<T> = std::result::Result<T, FieldError>;
|
||||
```
|
||||
|
||||
任何错误都能够被转换为`FieldError`,并且你还能扩展标准的错误输出。
|
||||
任何错误都能够被转换为`FieldError`,并且你还能扩展标准的错误信息。
|
||||
|
||||
下面是一个例子,解析一个输入的字符串到整数,当解析失败时返回错误,并且附加额外的错误信息。
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user