1
0
mirror of https://github.com/actix/examples synced 2024-12-04 18:51:55 +01:00
examples/graphql-demo/src/schemas/product.rs

52 lines
1.2 KiB
Rust
Raw Normal View History

use juniper;
use mysql::{Error as DBError, from_row, params, Row};
use crate::schemas::root::Context;
use crate::schemas::user::User;
/// Product
#[derive(Default, Debug)]
pub struct Product {
pub id: String,
pub user_id: String,
pub name: String,
pub price: f64,
}
#[juniper::object(Context = Context)]
impl Product {
fn id(&self) -> &str {
&self.id
}
fn user_id(&self) -> &str {
&self.user_id
}
fn name(&self) -> &str {
&self.name
}
fn price(&self) -> f64 {
self.price
}
fn user(&self, context: &Context) -> Option<User> {
let mut conn = context.dbpool.get().unwrap();
let user: Result<Option<Row>, DBError> = conn.first_exec(
"SELECT * FROM user WHERE id=:id",
params! {"id" => &self.user_id},
);
if let Err(err) = user {
None
}else{
let (id, name, email) = from_row(user.unwrap().unwrap());
Some(User { id, name, email })
}
}
}
#[derive(GraphQLInputObject)]
#[graphql(description = "Product Input")]
pub struct ProductInput {
pub user_id: String,
pub name: String,
pub price: f64,
}