1
0
mirror of https://github.com/actix/examples synced 2024-11-23 14:31:07 +01:00

update mysql deps

This commit is contained in:
Rob Ede 2022-07-09 23:11:36 +01:00
parent bbb2e54f07
commit ecc8522725
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
7 changed files with 507 additions and 313 deletions

686
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -46,7 +46,7 @@ members = [
"shutdown-server",
"templating/askama",
"templating/handlebars",
"templating/sailfish",
# "templating/sailfish",
"templating/tera",
"templating/tinytemplate",
"templating/yarte",

View File

@ -10,9 +10,9 @@ actix-cors = "0.6"
juniper = "0.15"
mysql = "17"
mysql = "21"
r2d2 = "0.8"
r2d2_mysql = "17"
r2d2_mysql = "21"
dotenv = "0.15"
env_logger = "0.9"

View File

@ -15,7 +15,7 @@ pub async fn graphql(
data: web::Json<GraphQLRequest>,
) -> Result<HttpResponse, Error> {
let ctx = Context {
dbpool: pool.get_ref().to_owned(),
db_pool: pool.get_ref().to_owned(),
};
let res = data.execute(&schema, &ctx).await;

View File

@ -1,8 +1,7 @@
use juniper::GraphQLInputObject;
use mysql::{from_row, params, Error as DBError, Row};
use mysql::{from_row, params, prelude::*, Error as DBError, Row};
use crate::schemas::root::Context;
use crate::schemas::user::User;
use crate::schemas::{root::Context, user::User};
/// Product
#[derive(Default, Debug)]
@ -13,6 +12,19 @@ pub struct Product {
pub price: f64,
}
impl Product {
pub(crate) fn from_row(row: Row) -> Self {
let (id, user_id, name, price) = from_row(row);
Self {
id,
user_id,
name,
price,
}
}
}
#[juniper::graphql_object(Context = Context)]
impl Product {
fn id(&self) -> &str {
@ -29,8 +41,8 @@ impl Product {
}
fn user(&self, context: &Context) -> Option<User> {
let mut conn = context.dbpool.get().unwrap();
let user: Result<Option<Row>, DBError> = conn.first_exec(
let mut conn = context.db_pool.get().unwrap();
let user: Result<Option<Row>, DBError> = conn.exec_first(
"SELECT * FROM user WHERE id=:id",
params! {"id" => &self.user_id},
);

View File

@ -1,7 +1,7 @@
use juniper::{
graphql_object, graphql_value, EmptySubscription, FieldError, FieldResult, RootNode,
};
use mysql::{from_row, params, Error as DBError, Row};
use mysql::{from_row, params, prelude::*, Error as DBError, Row};
use super::{
product::{Product, ProductInput},
@ -10,7 +10,7 @@ use super::{
use crate::db::Pool;
pub struct Context {
pub dbpool: Pool,
pub db_pool: Pool,
}
impl juniper::Context for Context {}
@ -21,28 +21,24 @@ pub struct QueryRoot;
impl QueryRoot {
#[graphql(description = "List of all users")]
fn users(context: &Context) -> FieldResult<Vec<User>> {
let mut conn = context.dbpool.get().unwrap();
let mut conn = context.db_pool.get().unwrap();
let users = conn
.prep_exec("select * from user", ())
.map(|result| {
result
.map(|x| x.unwrap())
.map(|row| {
let (id, name, email) = from_row(row);
User { id, name, email }
})
.collect()
})
.unwrap();
.exec("SELECT * FROM user", ())
.unwrap()
.into_iter()
.map(User::from_row)
.collect();
Ok(users)
}
#[graphql(description = "Get Single user reference by user ID")]
fn user(context: &Context, id: String) -> FieldResult<User> {
let mut conn = context.dbpool.get().unwrap();
let mut conn = context.db_pool.get().unwrap();
let user: Result<Option<Row>, DBError> =
conn.first_exec("SELECT * FROM user WHERE id=:id", params! {"id" => id});
conn.exec_first("SELECT * FROM user WHERE id=:id", params! {"id" => id});
if let Err(_err) = user {
return Err(FieldError::new(
@ -57,32 +53,23 @@ impl QueryRoot {
#[graphql(description = "List of all users")]
fn products(context: &Context) -> FieldResult<Vec<Product>> {
let mut conn = context.dbpool.get().unwrap();
let mut conn = context.db_pool.get().unwrap();
let products = conn
.prep_exec("select * from product", ())
.map(|result| {
result
.map(|x| x.unwrap())
.map(|row| {
let (id, user_id, name, price) = from_row(row);
Product {
id,
user_id,
name,
price,
}
})
.collect()
})
.unwrap();
.exec("SELECT * FROM product", ())
.unwrap()
.into_iter()
.map(Product::from_row)
.collect();
Ok(products)
}
#[graphql(description = "Get Single user reference by user ID")]
fn product(context: &Context, id: String) -> FieldResult<Product> {
let mut conn = context.dbpool.get().unwrap();
let mut conn = context.db_pool.get().unwrap();
let product: Result<Option<Row>, DBError> =
conn.first_exec("SELECT * FROM user WHERE id=:id", params! {"id" => id});
conn.exec_first("SELECT * FROM user WHERE id=:id", params! {"id" => id});
if let Err(_err) = product {
return Err(FieldError::new(
"Product Not Found",
@ -105,10 +92,10 @@ pub struct MutationRoot;
#[graphql_object(Context = Context)]
impl MutationRoot {
fn create_user(context: &Context, user: UserInput) -> FieldResult<User> {
let mut conn = context.dbpool.get().unwrap();
let mut conn = context.db_pool.get().unwrap();
let new_id = uuid::Uuid::new_v4().to_simple().to_string();
let insert: Result<Option<Row>, DBError> = conn.first_exec(
let insert: Result<Option<Row>, DBError> = conn.exec_first(
"INSERT INTO user(id, name, email) VALUES(:id, :name, :email)",
params! {
"id" => &new_id,
@ -137,10 +124,10 @@ impl MutationRoot {
}
fn create_product(context: &Context, product: ProductInput) -> FieldResult<Product> {
let mut conn = context.dbpool.get().unwrap();
let mut conn = context.db_pool.get().unwrap();
let new_id = uuid::Uuid::new_v4().to_simple().to_string();
let insert: Result<Option<Row>, DBError> = conn.first_exec(
let insert: Result<Option<Row>, DBError> = conn.exec_first(
"INSERT INTO product(id, user_id, name, price) VALUES(:id, :user_id, :name, :price)",
params! {
"id" => &new_id,

View File

@ -1,8 +1,7 @@
use juniper::{graphql_object, GraphQLInputObject};
use mysql::{from_row, params};
use mysql::{from_row, params, prelude::*, Row};
use crate::schemas::product::Product;
use crate::schemas::root::Context;
use crate::schemas::{product::Product, root::Context};
/// User
#[derive(Default, Debug)]
@ -12,6 +11,13 @@ pub struct User {
pub email: String,
}
impl User {
pub(crate) fn from_row(row: Row) -> Self {
let (id, name, email) = from_row(row);
User { id, name, email }
}
}
#[derive(GraphQLInputObject)]
#[graphql(description = "User Input")]
pub struct UserInput {
@ -32,28 +38,15 @@ impl User {
}
fn products(&self, context: &Context) -> Vec<Product> {
let mut conn = context.dbpool.get().unwrap();
let mut conn = context.db_pool.get().unwrap();
conn.prep_exec(
"select * from product where user_id=:user_id",
params! {
"user_id" => &self.id
},
conn.exec(
"SELECT * FROM product WHERE user_id = :user_id",
params! { "user_id" => &self.id },
)
.map(|result| {
result
.map(|x| x.unwrap())
.map(|row| {
let (id, user_id, name, price) = from_row(row);
Product {
id,
user_id,
name,
price,
}
})
.collect()
})
.unwrap()
.into_iter()
.map(Product::from_row)
.collect()
}
}