parent
b7211a77e4
commit
015ecaff85
116
src/main.rs
116
src/main.rs
|
@ -1,109 +1,13 @@
|
|||
extern crate rocket;
|
||||
|
||||
use std::sync::{ atomic::AtomicUsize, atomic::Ordering, Mutex };
|
||||
use std::{ collections::HashMap };
|
||||
use juniper::{ EmptySubscription, RootNode, graphql_object, Context };
|
||||
use rocket::{ response::content, Rocket, State };
|
||||
use juniper::{ EmptySubscription };
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Person {
|
||||
id: String,
|
||||
name: String,
|
||||
}
|
||||
mod model;
|
||||
mod schema;
|
||||
|
||||
impl Person {
|
||||
pub fn new(id: String, name: String) -> Person {
|
||||
Person { id, name }
|
||||
}
|
||||
|
||||
fn name(&self) -> Option<&str> {
|
||||
Some(self.name.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
#[graphql_object(context = Database)]
|
||||
impl Person {
|
||||
fn id(&self) -> &str {
|
||||
&self.id
|
||||
}
|
||||
|
||||
fn name(&self) -> Option<&str> {
|
||||
Some(self.name.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Database {
|
||||
people: Mutex<HashMap<String, Person>>,
|
||||
next_id: AtomicUsize,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn new() -> Database {
|
||||
let database = Database {
|
||||
people: Mutex::new(HashMap::new()),
|
||||
next_id: AtomicUsize::new(1),
|
||||
};
|
||||
database
|
||||
}
|
||||
|
||||
pub fn get_person(&self, id: &str) -> Option<Person> {
|
||||
let people = self.people.lock().expect("Couldn't lock people");
|
||||
match people.get(id) {
|
||||
Some(person) => Some(person.to_owned()),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_person(&self, person: Person) {
|
||||
self.people.lock().expect("Couldn't lock people").insert(person.id.to_owned(), person);
|
||||
}
|
||||
|
||||
pub fn new_id(&self) -> usize {
|
||||
self.next_id.fetch_add(1, Ordering::Relaxed)
|
||||
}
|
||||
|
||||
pub fn new_person(&self, name: String) -> Option<Person> {
|
||||
let person = Person {
|
||||
id: self.new_id().to_string(),
|
||||
name,
|
||||
};
|
||||
self.people.lock().expect("lock people hashmap").insert(person.id.to_owned(), person.to_owned());
|
||||
Some(person)
|
||||
}
|
||||
}
|
||||
|
||||
impl Context for Database {}
|
||||
|
||||
pub struct Query;
|
||||
#[graphql_object(context = Database)]
|
||||
impl Query {
|
||||
#[graphql(arguments(id(description = "id of the human")))]
|
||||
fn human(database: &Database, id: String) -> Option<Person> {
|
||||
database.get_person(&id)
|
||||
}
|
||||
}
|
||||
|
||||
struct Mutations;
|
||||
|
||||
#[graphql_object(context = Database)]
|
||||
impl Mutations {
|
||||
fn addHuman(database: &Database, id: String, name: String) -> Person {
|
||||
let new_guy = Person::new(id, name);
|
||||
database.add_person(new_guy.to_owned());
|
||||
new_guy.to_owned()
|
||||
}
|
||||
|
||||
fn newId(database: &Database) -> i32 {
|
||||
database.new_id() as i32
|
||||
}
|
||||
|
||||
fn newPerson(database: &Database, name: String) -> Option<Person> {
|
||||
database.new_person(name)
|
||||
}
|
||||
}
|
||||
|
||||
type Schema = RootNode<'static, Query, Mutations, EmptySubscription<Database>>;
|
||||
use model::Database;
|
||||
use schema::{ Schema, Query, Mutations };
|
||||
|
||||
#[rocket::get("/")]
|
||||
fn graphiql() -> content::Html<String> {
|
||||
|
@ -138,13 +42,15 @@ fn get_rest(id: String, context: &State<Database>) -> Option<String> {
|
|||
|
||||
#[rocket::main]
|
||||
async fn main() {
|
||||
Rocket::build()
|
||||
.manage(Database::new())
|
||||
.manage(Schema::new(
|
||||
let schema = Schema::new(
|
||||
Query,
|
||||
Mutations,
|
||||
EmptySubscription::<Database>::new(),
|
||||
))
|
||||
);
|
||||
|
||||
Rocket::build()
|
||||
.manage(Database::new())
|
||||
.manage(schema)
|
||||
.mount(
|
||||
"/",
|
||||
rocket::routes![graphiql, get_graphql_handler, post_graphql_handler, get_rest],
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
use std::sync::{ atomic::AtomicUsize, atomic::Ordering, Mutex };
|
||||
use std::{ collections::HashMap };
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Person {
|
||||
id: String,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl Person {
|
||||
pub fn new(id: String, name: String) -> Person {
|
||||
Person { id, name }
|
||||
}
|
||||
|
||||
pub fn id(&self) -> &str {
|
||||
self.id.as_str()
|
||||
}
|
||||
|
||||
pub fn name(&self) -> Option<&str> {
|
||||
Some(self.name.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Database {
|
||||
people: Mutex<HashMap<String, Person>>,
|
||||
next_id: AtomicUsize,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn new() -> Database {
|
||||
let database = Database {
|
||||
people: Mutex::new(HashMap::new()),
|
||||
next_id: AtomicUsize::new(1),
|
||||
};
|
||||
database
|
||||
}
|
||||
|
||||
pub fn get_person(&self, id: &str) -> Option<Person> {
|
||||
let people = self.people.lock().expect("Couldn't lock people");
|
||||
match people.get(id) {
|
||||
Some(person) => Some(person.to_owned()),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_person(&self, person: Person) {
|
||||
self.people.lock().expect("Couldn't lock people").insert(person.id.to_owned(), person);
|
||||
}
|
||||
|
||||
pub fn new_id(&self) -> usize {
|
||||
self.next_id.fetch_add(1, Ordering::Relaxed)
|
||||
}
|
||||
|
||||
pub fn new_person(&self, name: String) -> Option<Person> {
|
||||
let person = Person {
|
||||
id: self.new_id().to_string(),
|
||||
name,
|
||||
};
|
||||
self.people.lock().expect("lock people hashmap").insert(person.id.to_owned(), person.to_owned());
|
||||
Some(person)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
impl Context for Database {}
|
||||
|
||||
use juniper::{ EmptySubscription, RootNode, graphql_object, Context };
|
||||
use crate::model::{ Database, Person };
|
||||
|
||||
#[graphql_object(context = Database)]
|
||||
impl Person {
|
||||
fn id(&self) -> &str {
|
||||
self.id()
|
||||
}
|
||||
|
||||
fn name(&self) -> Option<&str> {
|
||||
self.name()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Query;
|
||||
#[graphql_object(context = Database)]
|
||||
impl Query {
|
||||
#[graphql(arguments(id(description = "id of the human")))]
|
||||
fn human(database: &Database, id: String) -> Option<Person> {
|
||||
database.get_person(&id)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Mutations;
|
||||
|
||||
#[graphql_object(context = Database)]
|
||||
impl Mutations {
|
||||
fn addHuman(database: &Database, id: String, name: String) -> Person {
|
||||
let new_guy = Person::new(id, name);
|
||||
database.add_person(new_guy.to_owned());
|
||||
new_guy.to_owned()
|
||||
}
|
||||
|
||||
fn newId(database: &Database) -> i32 {
|
||||
database.new_id() as i32
|
||||
}
|
||||
|
||||
fn newPerson(database: &Database, name: String) -> Option<Person> {
|
||||
database.new_person(name)
|
||||
}
|
||||
}
|
||||
|
||||
pub type Schema = RootNode<'static, Query, Mutations, EmptySubscription<Database>>;
|
Loading…
Reference in New Issue