2020-08-09 17:19:34 -05:00
|
|
|
use std::{env, pin::Pin, time::Duration};
|
|
|
|
|
|
|
|
use actix_cors::Cors;
|
2021-07-06 17:41:42 -05:00
|
|
|
use actix_web::{
|
|
|
|
http::header,
|
|
|
|
middleware,
|
|
|
|
web::{self, Data},
|
|
|
|
App, Error, HttpRequest, HttpResponse, HttpServer,
|
|
|
|
};
|
2020-08-09 17:19:34 -05:00
|
|
|
|
|
|
|
use juniper::{
|
2022-07-13 05:30:51 -05:00
|
|
|
graphql_subscription, graphql_value,
|
2022-01-26 12:58:53 -06:00
|
|
|
tests::fixtures::starwars::schema::{Database, Query},
|
2022-07-13 05:30:51 -05:00
|
|
|
EmptyMutation, FieldError, GraphQLObject, RootNode,
|
2020-08-09 17:19:34 -05:00
|
|
|
};
|
|
|
|
use juniper_actix::{graphql_handler, playground_handler, subscriptions::subscriptions_handler};
|
|
|
|
use juniper_graphql_ws::ConnectionConfig;
|
|
|
|
|
|
|
|
type Schema = RootNode<'static, Query, EmptyMutation<Database>, Subscription>;
|
|
|
|
|
|
|
|
fn schema() -> Schema {
|
|
|
|
Schema::new(Query, EmptyMutation::<Database>::new(), Subscription)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn playground() -> Result<HttpResponse, Error> {
|
|
|
|
playground_handler("/graphql", Some("/subscriptions")).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn graphql(
|
|
|
|
req: actix_web::HttpRequest,
|
|
|
|
payload: actix_web::web::Payload,
|
|
|
|
schema: web::Data<Schema>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let context = Database::new();
|
|
|
|
graphql_handler(&schema, &context, req, payload).await
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Subscription;
|
|
|
|
|
2022-07-13 05:30:51 -05:00
|
|
|
#[derive(GraphQLObject)]
|
2020-08-09 17:19:34 -05:00
|
|
|
struct RandomHuman {
|
|
|
|
id: String,
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
2020-08-19 02:08:53 -05:00
|
|
|
type RandomHumanStream =
|
|
|
|
Pin<Box<dyn futures::Stream<Item = Result<RandomHuman, FieldError>> + Send>>;
|
|
|
|
|
2020-11-06 20:15:18 -06:00
|
|
|
#[graphql_subscription(context = Database)]
|
2020-08-09 17:19:34 -05:00
|
|
|
impl Subscription {
|
|
|
|
#[graphql(
|
|
|
|
description = "A random humanoid creature in the Star Wars universe every 3 seconds. Second result will be an error."
|
|
|
|
)]
|
2020-08-19 02:08:53 -05:00
|
|
|
async fn random_human(context: &Database) -> RandomHumanStream {
|
2020-08-09 17:19:34 -05:00
|
|
|
let mut counter = 0;
|
|
|
|
|
|
|
|
let context = (*context).clone();
|
|
|
|
|
|
|
|
use rand::{rngs::StdRng, Rng, SeedableRng};
|
|
|
|
let mut rng = StdRng::from_entropy();
|
2021-06-29 01:22:45 -05:00
|
|
|
let mut interval = tokio::time::interval(Duration::from_secs(5));
|
|
|
|
let stream = async_stream::stream! {
|
2020-08-09 17:19:34 -05:00
|
|
|
counter += 1;
|
2021-06-29 01:22:45 -05:00
|
|
|
loop {
|
|
|
|
interval.tick().await;
|
|
|
|
if counter == 2 {
|
|
|
|
yield Err(FieldError::new(
|
|
|
|
"some field error from handler",
|
2021-08-12 18:12:01 -05:00
|
|
|
graphql_value!("some additional string"),
|
2021-06-29 01:22:45 -05:00
|
|
|
))
|
|
|
|
} else {
|
|
|
|
let random_id = rng.gen_range(1000..1005).to_string();
|
|
|
|
let human = context.get_human(&random_id).unwrap().clone();
|
|
|
|
|
|
|
|
yield Ok(RandomHuman {
|
2022-07-13 05:30:51 -05:00
|
|
|
id: human.id().into(),
|
|
|
|
name: human.name().unwrap().into(),
|
2021-06-29 01:22:45 -05:00
|
|
|
})
|
|
|
|
}
|
2020-08-09 17:19:34 -05:00
|
|
|
}
|
2021-06-29 01:22:45 -05:00
|
|
|
};
|
2020-08-09 17:19:34 -05:00
|
|
|
|
|
|
|
Box::pin(stream)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn subscriptions(
|
|
|
|
req: HttpRequest,
|
|
|
|
stream: web::Payload,
|
|
|
|
schema: web::Data<Schema>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let context = Database::new();
|
|
|
|
let schema = schema.into_inner();
|
|
|
|
let config = ConnectionConfig::new(context);
|
|
|
|
// set the keep alive interval to 15 secs so that it doesn't timeout in playground
|
|
|
|
// playground has a hard-coded timeout set to 20 secs
|
|
|
|
let config = config.with_keep_alive_interval(Duration::from_secs(15));
|
|
|
|
|
|
|
|
subscriptions_handler(req, stream, schema, config).await
|
|
|
|
}
|
|
|
|
|
2020-09-12 11:32:15 -05:00
|
|
|
#[actix_web::main]
|
2020-08-09 17:19:34 -05:00
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
env::set_var("RUST_LOG", "info");
|
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
2021-07-06 17:41:42 -05:00
|
|
|
.app_data(Data::new(schema()))
|
2020-08-09 17:19:34 -05:00
|
|
|
.wrap(
|
2020-10-20 04:57:14 -05:00
|
|
|
Cors::default()
|
2021-07-06 17:41:42 -05:00
|
|
|
.allow_any_origin()
|
2020-08-09 17:19:34 -05:00
|
|
|
.allowed_methods(vec!["POST", "GET"])
|
2021-06-29 01:22:45 -05:00
|
|
|
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
|
|
|
|
.allowed_header(header::CONTENT_TYPE)
|
2020-08-09 17:19:34 -05:00
|
|
|
.supports_credentials()
|
2020-10-20 04:57:14 -05:00
|
|
|
.max_age(3600),
|
2020-08-09 17:19:34 -05:00
|
|
|
)
|
2021-07-06 17:41:42 -05:00
|
|
|
.wrap(middleware::Compress::default())
|
|
|
|
.wrap(middleware::Logger::default())
|
2020-08-09 17:19:34 -05:00
|
|
|
.service(web::resource("/subscriptions").route(web::get().to(subscriptions)))
|
|
|
|
.service(
|
|
|
|
web::resource("/graphql")
|
|
|
|
.route(web::post().to(graphql))
|
|
|
|
.route(web::get().to(graphql)),
|
|
|
|
)
|
|
|
|
.service(web::resource("/playground").route(web::get().to(playground)))
|
2022-03-04 09:53:27 -06:00
|
|
|
.default_service(web::to(|| async {
|
2020-08-09 17:19:34 -05:00
|
|
|
HttpResponse::Found()
|
2021-06-29 01:22:45 -05:00
|
|
|
.append_header((header::LOCATION, "/playground"))
|
2020-08-09 17:19:34 -05:00
|
|
|
.finish()
|
|
|
|
}))
|
|
|
|
})
|
2022-07-13 05:30:51 -05:00
|
|
|
.bind("127.0.0.1:8080")?
|
2020-08-09 17:19:34 -05:00
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|