2019-08-19 15:40:22 -05:00
|
|
|
//! This example demonstrates async/await usage with warp.
|
|
|
|
|
2020-06-14 08:26:18 -05:00
|
|
|
use juniper::{
|
|
|
|
graphql_object, EmptyMutation, EmptySubscription, FieldError, GraphQLEnum, RootNode,
|
|
|
|
};
|
2019-08-19 15:40:22 -05:00
|
|
|
use warp::{http::Response, Filter};
|
|
|
|
|
2020-06-14 08:26:18 -05:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
struct Context;
|
2019-08-19 15:40:22 -05:00
|
|
|
impl juniper::Context for Context {}
|
|
|
|
|
2020-06-14 08:26:18 -05:00
|
|
|
#[derive(Clone, Copy, Debug, GraphQLEnum)]
|
2019-08-19 16:16:50 -05:00
|
|
|
enum UserKind {
|
|
|
|
Admin,
|
|
|
|
User,
|
|
|
|
Guest,
|
|
|
|
}
|
|
|
|
|
2020-06-14 08:26:18 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2019-08-19 15:40:22 -05:00
|
|
|
struct User {
|
|
|
|
id: i32,
|
2019-08-19 16:16:50 -05:00
|
|
|
kind: UserKind,
|
2019-08-19 15:40:22 -05:00
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
2020-06-14 08:26:18 -05:00
|
|
|
#[graphql_object(Context = Context)]
|
2019-08-19 15:40:22 -05:00
|
|
|
impl User {
|
|
|
|
fn id(&self) -> i32 {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
|
2019-08-19 16:16:50 -05:00
|
|
|
fn kind(&self) -> UserKind {
|
|
|
|
self.kind
|
|
|
|
}
|
|
|
|
|
2019-08-19 15:40:22 -05:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn friends(&self) -> Vec<User> {
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 08:26:18 -05:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2019-11-04 07:55:36 -06:00
|
|
|
struct Query;
|
2019-08-19 15:40:22 -05:00
|
|
|
|
2020-06-14 08:26:18 -05:00
|
|
|
#[graphql_object(Context = Context)]
|
2019-08-19 15:40:22 -05:00
|
|
|
impl Query {
|
|
|
|
async fn users() -> Vec<User> {
|
2020-06-14 08:26:18 -05:00
|
|
|
vec![User {
|
|
|
|
id: 1,
|
|
|
|
kind: UserKind::Admin,
|
|
|
|
name: "user1".into(),
|
|
|
|
}]
|
2019-08-19 15:40:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetch a URL and return the response body text.
|
|
|
|
async fn request(url: String) -> Result<String, FieldError> {
|
2020-06-14 08:26:18 -05:00
|
|
|
Ok(reqwest::get(&url).await?.text().await?)
|
2019-08-19 15:40:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 22:31:36 -05:00
|
|
|
type Schema = RootNode<'static, Query, EmptyMutation<Context>, EmptySubscription<Context>>;
|
2019-08-19 15:40:22 -05:00
|
|
|
|
|
|
|
fn schema() -> Schema {
|
2020-06-14 08:26:18 -05:00
|
|
|
Schema::new(
|
|
|
|
Query,
|
|
|
|
EmptyMutation::<Context>::new(),
|
|
|
|
EmptySubscription::<Context>::new(),
|
|
|
|
)
|
2019-08-19 15:40:22 -05:00
|
|
|
}
|
|
|
|
|
2020-03-18 22:31:36 -05:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2020-06-14 08:26:18 -05:00
|
|
|
std::env::set_var("RUST_LOG", "warp_async");
|
2019-08-19 15:40:22 -05:00
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
let log = warp::log("warp_server");
|
|
|
|
|
|
|
|
let homepage = warp::path::end().map(|| {
|
|
|
|
Response::builder()
|
|
|
|
.header("content-type", "text/html")
|
|
|
|
.body(format!(
|
|
|
|
"<html><h1>juniper_warp</h1><div>visit <a href=\"/graphiql\">/graphiql</a></html>"
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
log::info!("Listening on 127.0.0.1:8080");
|
|
|
|
|
2020-06-14 08:26:18 -05:00
|
|
|
let state = warp::any().map(|| Context);
|
2020-03-18 22:31:36 -05:00
|
|
|
let graphql_filter = juniper_warp::make_graphql_filter(schema(), state.boxed());
|
2019-08-19 15:40:22 -05:00
|
|
|
|
|
|
|
warp::serve(
|
2020-03-18 22:31:36 -05:00
|
|
|
warp::get()
|
2019-08-19 15:40:22 -05:00
|
|
|
.and(warp::path("graphiql"))
|
2020-06-14 08:26:18 -05:00
|
|
|
.and(juniper_warp::graphiql_filter("/graphql", None))
|
2019-08-19 15:40:22 -05:00
|
|
|
.or(homepage)
|
|
|
|
.or(warp::path("graphql").and(graphql_filter))
|
|
|
|
.with(log),
|
|
|
|
)
|
2020-03-18 22:31:36 -05:00
|
|
|
.run(([127, 0, 0, 1], 8080))
|
|
|
|
.await
|
2019-08-19 15:40:22 -05:00
|
|
|
}
|