Add some docs

This commit is contained in:
sunli 2020-04-16 17:06:46 +08:00
parent 11000b42d3
commit 51ea8696ac
5 changed files with 92 additions and 6 deletions

View File

@ -429,7 +429,6 @@ impl EnumItem {
#[derive(Debug)]
pub struct InputField {
pub internal: bool,
pub name: Option<String>,
pub desc: Option<String>,
pub default: Option<Value>,
@ -438,7 +437,6 @@ pub struct InputField {
impl InputField {
pub fn parse(crate_name: &TokenStream, attrs: &[Attribute]) -> Result<Self> {
let mut internal = false;
let mut name = None;
let mut desc = None;
let mut default = None;
@ -449,9 +447,6 @@ impl InputField {
if let Meta::List(args) = &attr.parse_meta()? {
for meta in &args.nested {
match meta {
NestedMeta::Meta(Meta::Path(p)) if p.is_ident("internal") => {
internal = true;
}
NestedMeta::Meta(Meta::NameValue(nv)) => {
if nv.path.is_ident("name") {
if let syn::Lit::Str(lit) = &nv.lit {
@ -506,7 +501,6 @@ impl InputField {
}
Ok(Self {
internal,
name,
desc,
default,

View File

@ -1 +1,27 @@
# 输入对象(InputObject)
你可以定义一个对象作为参数类型GraphQL称之为`Input Object`,输入对象的定义方式和[简单对象](simple_object.md)很像,不同的是,简单对象只能用于输出,而输入对象只能用于输入。
输入对象不需要为每个字段指定`#[field]`标记,它每个字段都是`Input Value`。但你也可以通过可选的`#[field]`标记来给字段添加描述,重命名。
```rust
use async_graphql::*;
#[InputObject]
struct Coordinate {
latitude: f64,
#[field(desc = "...")]
longitude: f64
}
struct Mutation;
#[Object]
impl Mutation {
async fn users_at_location(&self, coordinate: Coordinate, radius: f64) -> Vec<User> {
// 将坐标写入数据库
// ...
}
}
```

View File

@ -1 +1,6 @@
# 定义模式(Schema)
在定义了基本的类型之后,需要定义一个模式把他们组合起来,模式由三种类型组成,查询对象,变更对象和订阅对象,其中变更对象和订阅对象是可选的。
当模式创建时,`Async-graphql`会遍历所有对象图,并注册所有类型。这意味着,如果定义了 GraphQL 对象但从未引用,那么此对象就不会暴露在模式中。

View File

@ -1 +1,43 @@
# 查询和变更
## 查询根对象
查询根对象是一个GraphQL对象定义类似其它对象。查询对象的所有字段Resolve函数是并发执行的。
```rust
use async_graphql::*;
struct Query;
#[Object]
impl Query {
async fn user(&self, username: String) -> FieldResult<Option<User>> {
// 在数据库中查找用户
}
}
```
## 变更根对象
变更根对象也是一个GraphQL但变更根对象的执行是顺序的只有第一个变更执行完成之后才会执行下一个。
下面的变更根对象提供用户注册和登录操作:
```rust
use async_graphql::*;
struct Mutation;
#[Object]
impl Mutation {
async fn signup(&self, username: String, password: String) -> Result<bool> {
// 用户注册
}
async fn login(&self, username: String, password: String) -> Result<String> {
// 用户登录并生成token
}
}
```

View File

@ -1 +1,20 @@
# 订阅
订阅根对象和其它根对象定义稍有不同它的Resolve函数总是返回一个`Stream`,而字段参数通常作为数据的筛选条件。
下面的例子订阅一个整数流,它每秒产生一个整数,参数`step`指定了整数的步长默认为1。
```rust
use async_graphql::*;
#[Subscription]
struct Subscription {
fn async integers(&self, #[arg(default = "1")] step: i32) -> impl Stream<Item = i32> {
let mut value = 0;
tokio::time::interval(Duration::from_secs(1)).map(move |_| {
value += step;
value
})
}
}
```