Make the actix example self contained (#839)
A smaller example is esier to understand. Fixes #804 Co-authored-by: Christian Legnitto <LegNeato@users.noreply.github.com>
This commit is contained in:
parent
4682fe2f4e
commit
250e261204
1 changed files with 82 additions and 18 deletions
|
@ -1,16 +1,78 @@
|
|||
#![deny(warnings)]
|
||||
|
||||
use std::env;
|
||||
use std::{collections::HashMap, env};
|
||||
|
||||
use actix_cors::Cors;
|
||||
use actix_web::{http::header, middleware, web, App, Error, HttpResponse, HttpServer};
|
||||
use juniper::{
|
||||
tests::fixtures::starwars::schema::{Database, Query},
|
||||
EmptyMutation, EmptySubscription, RootNode,
|
||||
};
|
||||
use juniper_actix::{
|
||||
graphiql_handler as gqli_handler, graphql_handler, playground_handler as play_handler,
|
||||
};
|
||||
use juniper::{graphql_object, EmptyMutation, EmptySubscription, GraphQLObject, RootNode};
|
||||
use juniper_actix::{graphiql_handler, graphql_handler, playground_handler};
|
||||
#[derive(Clone, GraphQLObject)]
|
||||
///a user
|
||||
pub struct User {
|
||||
///the id
|
||||
id: i32,
|
||||
///the name
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct Database {
|
||||
///this could be a database connection
|
||||
users: HashMap<i32, User>,
|
||||
}
|
||||
impl Database {
|
||||
pub fn new() -> Database {
|
||||
let mut users = HashMap::new();
|
||||
users.insert(
|
||||
1,
|
||||
User {
|
||||
id: 1,
|
||||
name: "Aron".to_string(),
|
||||
},
|
||||
);
|
||||
users.insert(
|
||||
2,
|
||||
User {
|
||||
id: 2,
|
||||
name: "Bea".to_string(),
|
||||
},
|
||||
);
|
||||
users.insert(
|
||||
3,
|
||||
User {
|
||||
id: 3,
|
||||
name: "Carl".to_string(),
|
||||
},
|
||||
);
|
||||
users.insert(
|
||||
4,
|
||||
User {
|
||||
id: 4,
|
||||
name: "Dora".to_string(),
|
||||
},
|
||||
);
|
||||
Database { users }
|
||||
}
|
||||
pub fn get_user(&self, id: &i32) -> Option<&User> {
|
||||
self.users.get(id)
|
||||
}
|
||||
}
|
||||
|
||||
// To make our Database usable by Juniper, we have to implement a marker trait.
|
||||
impl juniper::Context for Database {}
|
||||
|
||||
// Queries represent the callable funcitons
|
||||
struct Query;
|
||||
#[graphql_object(context = Database)]
|
||||
impl Query {
|
||||
fn apiVersion() -> String {
|
||||
"1.0".to_string()
|
||||
}
|
||||
#[graphql(arguments(id(description = "id of the user")))]
|
||||
fn user(database: &Database, id: i32) -> Option<&User> {
|
||||
database.get_user(&id)
|
||||
}
|
||||
}
|
||||
|
||||
type Schema = RootNode<'static, Query, EmptyMutation<Database>, EmptySubscription<Database>>;
|
||||
|
||||
|
@ -22,13 +84,13 @@ fn schema() -> Schema {
|
|||
)
|
||||
}
|
||||
|
||||
async fn graphiql_handler() -> Result<HttpResponse, Error> {
|
||||
gqli_handler("/", None).await
|
||||
async fn graphiql_route() -> Result<HttpResponse, Error> {
|
||||
graphiql_handler("/graphgl", None).await
|
||||
}
|
||||
async fn playground_handler() -> Result<HttpResponse, Error> {
|
||||
play_handler("/", None).await
|
||||
async fn playground_route() -> Result<HttpResponse, Error> {
|
||||
playground_handler("/graphgl", None).await
|
||||
}
|
||||
async fn graphql(
|
||||
async fn graphql_route(
|
||||
req: actix_web::HttpRequest,
|
||||
payload: actix_web::web::Payload,
|
||||
schema: web::Data<Schema>,
|
||||
|
@ -57,12 +119,14 @@ async fn main() -> std::io::Result<()> {
|
|||
.max_age(3600),
|
||||
)
|
||||
.service(
|
||||
web::resource("/")
|
||||
.route(web::post().to(graphql))
|
||||
.route(web::get().to(graphql)),
|
||||
web::resource("/graphgl")
|
||||
.route(web::post().to(graphql_route))
|
||||
.route(web::get().to(graphql_route)),
|
||||
)
|
||||
.service(web::resource("/playground").route(web::get().to(playground_handler)))
|
||||
.service(web::resource("/graphiql").route(web::get().to(graphiql_handler)))
|
||||
.service(web::resource("/playground").route(web::get().to(playground_route)))
|
||||
.service(web::resource("/graphiql").route(web::get().to(graphiql_route)))
|
||||
});
|
||||
server.bind("127.0.0.1:8080").unwrap().run().await
|
||||
}
|
||||
// now go to http://127.0.0.1:8080/playground or graphiql and execute
|
||||
//{ apiVersion, user(id: 2){id, name}}
|
||||
|
|
Loading…
Reference in a new issue