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

52 lines
1.2 KiB
Rust
Raw Normal View History

2019-12-07 18:59:24 +01:00
use mysql::{from_row, params, Error as DBError, 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,
}
2021-04-26 13:32:32 +02:00
#[juniper::graphql_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},
);
2021-04-26 13:32:32 +02:00
if let Err(_err) = user {
None
2019-12-07 18:59:24 +01:00
} 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,
2019-12-07 18:59:24 +01:00
}