2016-09-11 11:41:29 -05:00
|
|
|
extern crate iron;
|
|
|
|
extern crate mount;
|
|
|
|
extern crate logger;
|
|
|
|
extern crate rustc_serialize;
|
|
|
|
#[macro_use] extern crate juniper;
|
|
|
|
|
2016-09-18 08:40:38 -05:00
|
|
|
use std::env;
|
|
|
|
|
2016-09-11 11:41:29 -05:00
|
|
|
use mount::Mount;
|
|
|
|
use logger::Logger;
|
|
|
|
use iron::prelude::*;
|
|
|
|
use juniper::FieldResult;
|
|
|
|
use juniper::iron_handlers::{GraphQLHandler, GraphiQLHandler};
|
|
|
|
|
|
|
|
fn context_factory(_: &mut Request) -> () {
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut mount = Mount::new();
|
|
|
|
|
|
|
|
let graphql_endpoint = GraphQLHandler::new(context_factory, Query { }, Mutation { });
|
|
|
|
let graphiql_endpoint = GraphiQLHandler::new("/graphql");
|
|
|
|
|
|
|
|
mount.mount("/graphiql", 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);
|
|
|
|
|
2016-09-18 08:40:38 -05:00
|
|
|
let host = env::var("LISTEN").unwrap_or("0.0.0.0:8080".to_owned());
|
2016-09-11 11:41:29 -05:00
|
|
|
println!("GraphQL server started on {}", host);
|
2016-09-18 08:40:38 -05:00
|
|
|
Iron::new(chain).http(host.as_str()).unwrap();
|
2016-09-11 11:41:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Query {}
|
|
|
|
struct Mutation {}
|
|
|
|
|
|
|
|
graphql_object!(Query: () as "Query" |&self| {
|
|
|
|
field dummy() -> FieldResult<&str> {
|
|
|
|
Ok("Dummy field")
|
|
|
|
}
|
|
|
|
|
|
|
|
field error() -> FieldResult<&str> {
|
|
|
|
Err("Can't do it".to_owned())
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
graphql_object!(<CtxT> Mutation: CtxT as "Mutation" |&self| {
|
|
|
|
field print(value: String) -> FieldResult<String> {
|
|
|
|
println!("Printing text according to mutation");
|
|
|
|
println!("{}", value);
|
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
});
|