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)]
|
#![deny(warnings)]
|
||||||
|
|
||||||
use std::env;
|
use std::{collections::HashMap, env};
|
||||||
|
|
||||||
use actix_cors::Cors;
|
use actix_cors::Cors;
|
||||||
use actix_web::{http::header, middleware, web, App, Error, HttpResponse, HttpServer};
|
use actix_web::{http::header, middleware, web, App, Error, HttpResponse, HttpServer};
|
||||||
use juniper::{
|
use juniper::{graphql_object, EmptyMutation, EmptySubscription, GraphQLObject, RootNode};
|
||||||
tests::fixtures::starwars::schema::{Database, Query},
|
use juniper_actix::{graphiql_handler, graphql_handler, playground_handler};
|
||||||
EmptyMutation, EmptySubscription, RootNode,
|
#[derive(Clone, GraphQLObject)]
|
||||||
};
|
///a user
|
||||||
use juniper_actix::{
|
pub struct User {
|
||||||
graphiql_handler as gqli_handler, graphql_handler, playground_handler as play_handler,
|
///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>>;
|
type Schema = RootNode<'static, Query, EmptyMutation<Database>, EmptySubscription<Database>>;
|
||||||
|
|
||||||
|
@ -22,13 +84,13 @@ fn schema() -> Schema {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn graphiql_handler() -> Result<HttpResponse, Error> {
|
async fn graphiql_route() -> Result<HttpResponse, Error> {
|
||||||
gqli_handler("/", None).await
|
graphiql_handler("/graphgl", None).await
|
||||||
}
|
}
|
||||||
async fn playground_handler() -> Result<HttpResponse, Error> {
|
async fn playground_route() -> Result<HttpResponse, Error> {
|
||||||
play_handler("/", None).await
|
playground_handler("/graphgl", None).await
|
||||||
}
|
}
|
||||||
async fn graphql(
|
async fn graphql_route(
|
||||||
req: actix_web::HttpRequest,
|
req: actix_web::HttpRequest,
|
||||||
payload: actix_web::web::Payload,
|
payload: actix_web::web::Payload,
|
||||||
schema: web::Data<Schema>,
|
schema: web::Data<Schema>,
|
||||||
|
@ -57,12 +119,14 @@ async fn main() -> std::io::Result<()> {
|
||||||
.max_age(3600),
|
.max_age(3600),
|
||||||
)
|
)
|
||||||
.service(
|
.service(
|
||||||
web::resource("/")
|
web::resource("/graphgl")
|
||||||
.route(web::post().to(graphql))
|
.route(web::post().to(graphql_route))
|
||||||
.route(web::get().to(graphql)),
|
.route(web::get().to(graphql_route)),
|
||||||
)
|
)
|
||||||
.service(web::resource("/playground").route(web::get().to(playground_handler)))
|
.service(web::resource("/playground").route(web::get().to(playground_route)))
|
||||||
.service(web::resource("/graphiql").route(web::get().to(graphiql_handler)))
|
.service(web::resource("/graphiql").route(web::get().to(graphiql_route)))
|
||||||
});
|
});
|
||||||
server.bind("127.0.0.1:8080").unwrap().run().await
|
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…
Add table
Reference in a new issue