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

23 lines
735 B
Markdown
Raw Normal View History

2020-04-15 03:15:30 +00:00
# 订阅
2020-04-16 09:06:46 +00:00
2020-09-15 03:56:05 +00:00
订阅根对象和其它根对象定义稍有不同它的Resolver函数总是返回一个 [Stream](https://docs.rs/futures-core/~0.3/futures_core/stream/trait.Stream.html) 或者`FieldResult<Stream>`,而字段参数通常作为数据的筛选条件。
2020-04-16 09:06:46 +00:00
下面的例子订阅一个整数流,它每秒产生一个整数,参数`step`指定了整数的步长默认为1。
```rust
use async_graphql::*;
2020-04-17 11:35:14 +00:00
struct Subscription;
#[Subscription]
2020-04-17 11:35:14 +00:00
impl Subscription {
async fn integers(&self, #[arg(default = "1")] step: i32) -> impl Stream<Item = i32> {
2020-04-16 09:06:46 +00:00
let mut value = 0;
tokio::time::interval(Duration::from_secs(1)).map(move |_| {
value += step;
value
})
}
}
```