2020-04-21 01:21:02 -05:00
|
|
|
#![deny(warnings)]
|
|
|
|
|
2020-06-30 10:13:15 -05:00
|
|
|
use std::env;
|
|
|
|
|
2020-04-21 01:21:02 -05:00
|
|
|
use actix_cors::Cors;
|
|
|
|
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
|
|
|
|
use juniper::{
|
2020-07-16 02:46:37 -05:00
|
|
|
tests::fixtures::starwars::{model::Database, schema::Query},
|
2020-04-21 01:21:02 -05:00
|
|
|
EmptyMutation, EmptySubscription, RootNode,
|
|
|
|
};
|
|
|
|
use juniper_actix::{
|
|
|
|
graphiql_handler as gqli_handler, graphql_handler, playground_handler as play_handler,
|
|
|
|
};
|
|
|
|
|
|
|
|
type Schema = RootNode<'static, Query, EmptyMutation<Database>, EmptySubscription<Database>>;
|
|
|
|
|
|
|
|
fn schema() -> Schema {
|
|
|
|
Schema::new(
|
|
|
|
Query,
|
|
|
|
EmptyMutation::<Database>::new(),
|
|
|
|
EmptySubscription::<Database>::new(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn graphiql_handler() -> Result<HttpResponse, Error> {
|
|
|
|
gqli_handler("/", None).await
|
|
|
|
}
|
|
|
|
async fn playground_handler() -> Result<HttpResponse, Error> {
|
|
|
|
play_handler("/", None).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
|
|
|
|
}
|
|
|
|
|
2020-09-12 11:32:15 -05:00
|
|
|
#[actix_web::main]
|
2020-04-21 01:21:02 -05:00
|
|
|
async fn main() -> std::io::Result<()> {
|
2020-06-30 10:13:15 -05:00
|
|
|
env::set_var("RUST_LOG", "info");
|
2020-04-21 01:21:02 -05:00
|
|
|
env_logger::init();
|
2020-06-30 10:13:15 -05:00
|
|
|
|
2020-04-21 01:21:02 -05:00
|
|
|
let server = HttpServer::new(move || {
|
|
|
|
App::new()
|
|
|
|
.data(schema())
|
|
|
|
.wrap(middleware::Compress::default())
|
|
|
|
.wrap(middleware::Logger::default())
|
|
|
|
.wrap(
|
|
|
|
Cors::new()
|
|
|
|
.allowed_methods(vec!["POST", "GET"])
|
|
|
|
.supports_credentials()
|
|
|
|
.max_age(3600)
|
|
|
|
.finish(),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/")
|
|
|
|
.route(web::post().to(graphql))
|
|
|
|
.route(web::get().to(graphql)),
|
|
|
|
)
|
|
|
|
.service(web::resource("/playground").route(web::get().to(playground_handler)))
|
|
|
|
.service(web::resource("/graphiql").route(web::get().to(graphiql_handler)))
|
|
|
|
});
|
|
|
|
server.bind("127.0.0.1:8080").unwrap().run().await
|
|
|
|
}
|