2020-04-06 05:49:39 +00:00
|
|
|
use actix::clock::Duration;
|
2020-03-17 09:26:59 +00:00
|
|
|
use actix_web::{web, App, HttpServer};
|
2020-04-06 05:49:39 +00:00
|
|
|
use async_graphql::{EmptyMutation, Schema};
|
|
|
|
use futures::{Stream, StreamExt};
|
2020-03-17 09:26:59 +00:00
|
|
|
|
|
|
|
struct QueryRoot;
|
|
|
|
|
|
|
|
#[async_graphql::Object]
|
2020-04-06 05:49:39 +00:00
|
|
|
impl QueryRoot {
|
2020-03-17 09:26:59 +00:00
|
|
|
#[field]
|
2020-04-06 05:49:39 +00:00
|
|
|
async fn value(&self) -> i32 {
|
|
|
|
0
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SubscriptionRoot;
|
|
|
|
|
|
|
|
#[async_graphql::Subscription]
|
|
|
|
impl SubscriptionRoot {
|
|
|
|
#[field]
|
2020-04-06 05:49:39 +00:00
|
|
|
fn interval(&self, n: i32) -> impl Stream<Item = i32> {
|
|
|
|
let mut value = 0;
|
|
|
|
actix_rt::time::interval(Duration::from_secs(1)).map(move |_| {
|
|
|
|
value += n;
|
|
|
|
value
|
|
|
|
})
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
HttpServer::new(move || {
|
2020-04-06 05:49:39 +00:00
|
|
|
let schema = Schema::new(QueryRoot, EmptyMutation, SubscriptionRoot);
|
2020-03-17 09:26:59 +00:00
|
|
|
let handler = async_graphql_actix_web::HandlerBuilder::new(schema)
|
|
|
|
.enable_ui("http://localhost:8000", Some("ws://localhost:8000"))
|
|
|
|
.enable_subscription()
|
|
|
|
.build();
|
|
|
|
App::new().service(web::resource("/").to(handler))
|
|
|
|
})
|
|
|
|
.bind("127.0.0.1:8000")?
|
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|