async-graphql/docs/zh-CN/src/query_and_mutation.md

44 lines
907 B
Markdown
Raw Normal View History

2020-04-15 03:15:30 +00:00
# 查询和变更
2020-04-16 09:06:46 +00:00
## 查询根对象
2020-05-13 04:49:43 +00:00
查询根对象是一个GraphQL对象定义类似其它对象。查询对象的所有字段Resolver函数是并发执行的。
2020-04-16 09:06:46 +00:00
```rust
use async_graphql::*;
struct Query;
#[Object]
2020-04-16 09:06:46 +00:00
impl Query {
Rework errors This completely overhauls the error system used in async-graphql. - `Error` has been renamed to `ServerError` and `FieldError` has been renamed to just `Error`. This is because `FieldError` is by far the most common error that users will have to use so it makes sense to use the most obvious error name. Also, the current name didn't make sense as it was used for things other than field errors, such as the data callback for websockets. - `ServerError` has been made completely opaque. Before it was an enum of all the possible errors, but now it just contains an error message, the locations, the path and extensions. It is a shame that we lose information, it makes more sense as _conceptually_ GraphQL does not provide that information. It also frees us to change the internals of async-graphql a lot more. - The path of errors is no longer an opaque JSON value but a regular type, `Vec<PathSegment>`. The type duplication of `PathSegment` and `QueryPathSegment` is unfortunate, I plan to work on this in the future. - Now that `ServerError` is opaque, `RuleError` has been removed from the public API, making it simpler. - Additionally `QueryError` has been completely removed. Instead the error messages are constructed ad-hoc; I took care to never repeat an error message. - Instead of constructing field-not-found errors inside the implementations of field resolvers they now return `Option`s, where a `None` value is representative of the field not being found. - As an unfortunate consequence of the last change, self-referential types based on the output of a subscription resolver can no longer be created. This does not mean anything for users, but causes lifetime issues in the implementation of merged objects. I fixed it with a bit of a hack, but this'll have to be looked into further. - `InputValueError` now has a generic parameter - it's kind of weird but it's necessary for ergonomics. It also improves error messages. - The `ErrorExtensions` trait has been removed. I didn't think the `extend` method was necessary since `From` impls exist. But the ergonomics are still there with a new trait `ExtendError`, which is implemented for both errors and results. - `Response` now supports serializing multiple errors. This allows for nice things like having multiple validation errors not be awkwardly shoved into a single error. - When an error occurs in execution, data is sent as `null`. This is slightly more compliant with the spec but the algorithm described in <https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has yet to be implemented.
2020-09-29 19:06:44 +00:00
async fn user(&self, username: String) -> Result<Option<User>> {
2020-04-16 09:06:46 +00:00
// 在数据库中查找用户
}
}
```
## 变更根对象
变更根对象也是一个GraphQL但变更根对象的执行是顺序的只有第一个变更执行完成之后才会执行下一个。
下面的变更根对象提供用户注册和登录操作:
```rust
use async_graphql::*;
struct Mutation;
#[Object]
2020-04-16 09:06:46 +00:00
impl Mutation {
async fn signup(&self, username: String, password: String) -> Result<bool> {
// 用户注册
}
async fn login(&self, username: String, password: String) -> Result<String> {
// 用户登录并生成token
}
}
```