2017-12-02 08:55:29 -06:00
|
|
|
extern crate iron;
|
|
|
|
extern crate juniper;
|
|
|
|
extern crate juniper_iron;
|
2018-01-13 05:25:55 -06:00
|
|
|
extern crate logger;
|
|
|
|
extern crate mount;
|
2017-12-02 08:55:29 -06:00
|
|
|
|
|
|
|
use std::env;
|
|
|
|
|
|
|
|
use iron::prelude::*;
|
2019-08-21 08:23:53 -05:00
|
|
|
use juniper::{
|
2020-07-16 02:46:37 -05:00
|
|
|
tests::fixtures::starwars::{model::Database, schema::Query},
|
2020-03-18 22:31:36 -05:00
|
|
|
EmptyMutation, EmptySubscription,
|
2019-08-21 08:23:53 -05:00
|
|
|
};
|
2017-12-02 08:55:29 -06:00
|
|
|
use juniper_iron::{GraphQLHandler, GraphiQLHandler};
|
2018-07-19 08:22:21 -05:00
|
|
|
use logger::Logger;
|
|
|
|
use mount::Mount;
|
2017-12-02 08:55:29 -06:00
|
|
|
|
2018-07-13 03:30:14 -05:00
|
|
|
fn context_factory(_: &mut Request) -> IronResult<Database> {
|
|
|
|
Ok(Database::new())
|
2017-12-02 08:55:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut mount = Mount::new();
|
|
|
|
|
2020-03-18 22:31:36 -05:00
|
|
|
let graphql_endpoint = GraphQLHandler::new(
|
|
|
|
context_factory,
|
|
|
|
Query,
|
|
|
|
EmptyMutation::<Database>::new(),
|
|
|
|
EmptySubscription::<Database>::new(),
|
|
|
|
);
|
2020-04-12 20:03:09 -05:00
|
|
|
let graphiql_endpoint = GraphiQLHandler::new("/graphql", None);
|
2017-12-02 08:55:29 -06:00
|
|
|
|
|
|
|
mount.mount("/", graphiql_endpoint);
|
|
|
|
mount.mount("/graphql", graphql_endpoint);
|
|
|
|
|
|
|
|
let (logger_before, logger_after) = Logger::new(None);
|
|
|
|
|
|
|
|
let mut chain = Chain::new(mount);
|
|
|
|
chain.link_before(logger_before);
|
|
|
|
chain.link_after(logger_after);
|
|
|
|
|
2018-01-27 05:45:28 -06:00
|
|
|
let host = env::var("LISTEN").unwrap_or_else(|_| "0.0.0.0:8080".to_owned());
|
2017-12-02 08:55:29 -06:00
|
|
|
println!("GraphQL server started on {}", host);
|
|
|
|
Iron::new(chain).http(host.as_str()).unwrap();
|
|
|
|
}
|