async-graphql/docs/en/src/subscription.md

23 lines
847 B
Markdown
Raw Normal View History

2020-04-15 03:15:30 +00:00
# Subscription
2020-05-09 20:56:15 +00:00
2020-09-15 03:56:05 +00:00
The definition of the subscription root object is slightly different from other root objects. Its resolver function always returns a [Stream](https://docs.rs/futures-core/~0.3/futures_core/stream/trait.Stream.html) or `FieldResult<Stream>`, and the field parameters are usually used as data filtering conditions.
2020-05-09 20:56:15 +00:00
2020-09-01 05:47:22 +00:00
The following example subscribes to an integer stream, which generates one integer per second. The parameter `step` specifies the integer step size with a default of 1.
2020-05-09 20:56:15 +00:00
```rust
use async_graphql::*;
struct Subscription;
2020-09-13 04:12:32 +00:00
#[GQLSubscription]
2020-05-09 20:56:15 +00:00
impl Subscription {
async fn 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
})
}
}
```