mirror of
https://github.com/actix/examples
synced 2024-11-23 14:31:07 +01:00
update mysql deps
This commit is contained in:
parent
bbb2e54f07
commit
ecc8522725
686
Cargo.lock
generated
686
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -46,7 +46,7 @@ members = [
|
|||||||
"shutdown-server",
|
"shutdown-server",
|
||||||
"templating/askama",
|
"templating/askama",
|
||||||
"templating/handlebars",
|
"templating/handlebars",
|
||||||
"templating/sailfish",
|
# "templating/sailfish",
|
||||||
"templating/tera",
|
"templating/tera",
|
||||||
"templating/tinytemplate",
|
"templating/tinytemplate",
|
||||||
"templating/yarte",
|
"templating/yarte",
|
||||||
|
@ -10,9 +10,9 @@ actix-cors = "0.6"
|
|||||||
|
|
||||||
juniper = "0.15"
|
juniper = "0.15"
|
||||||
|
|
||||||
mysql = "17"
|
mysql = "21"
|
||||||
r2d2 = "0.8"
|
r2d2 = "0.8"
|
||||||
r2d2_mysql = "17"
|
r2d2_mysql = "21"
|
||||||
|
|
||||||
dotenv = "0.15"
|
dotenv = "0.15"
|
||||||
env_logger = "0.9"
|
env_logger = "0.9"
|
||||||
|
@ -15,7 +15,7 @@ pub async fn graphql(
|
|||||||
data: web::Json<GraphQLRequest>,
|
data: web::Json<GraphQLRequest>,
|
||||||
) -> Result<HttpResponse, Error> {
|
) -> Result<HttpResponse, Error> {
|
||||||
let ctx = Context {
|
let ctx = Context {
|
||||||
dbpool: pool.get_ref().to_owned(),
|
db_pool: pool.get_ref().to_owned(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let res = data.execute(&schema, &ctx).await;
|
let res = data.execute(&schema, &ctx).await;
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
use juniper::GraphQLInputObject;
|
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::{root::Context, user::User};
|
||||||
use crate::schemas::user::User;
|
|
||||||
|
|
||||||
/// Product
|
/// Product
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
@ -13,6 +12,19 @@ pub struct Product {
|
|||||||
pub price: f64,
|
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)]
|
#[juniper::graphql_object(Context = Context)]
|
||||||
impl Product {
|
impl Product {
|
||||||
fn id(&self) -> &str {
|
fn id(&self) -> &str {
|
||||||
@ -29,8 +41,8 @@ impl Product {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn user(&self, context: &Context) -> Option<User> {
|
fn user(&self, context: &Context) -> Option<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(
|
let user: Result<Option<Row>, DBError> = conn.exec_first(
|
||||||
"SELECT * FROM user WHERE id=:id",
|
"SELECT * FROM user WHERE id=:id",
|
||||||
params! {"id" => &self.user_id},
|
params! {"id" => &self.user_id},
|
||||||
);
|
);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use juniper::{
|
use juniper::{
|
||||||
graphql_object, graphql_value, EmptySubscription, FieldError, FieldResult, RootNode,
|
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::{
|
use super::{
|
||||||
product::{Product, ProductInput},
|
product::{Product, ProductInput},
|
||||||
@ -10,7 +10,7 @@ use super::{
|
|||||||
use crate::db::Pool;
|
use crate::db::Pool;
|
||||||
|
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
pub dbpool: Pool,
|
pub db_pool: Pool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl juniper::Context for Context {}
|
impl juniper::Context for Context {}
|
||||||
@ -21,28 +21,24 @@ pub struct QueryRoot;
|
|||||||
impl QueryRoot {
|
impl QueryRoot {
|
||||||
#[graphql(description = "List of all users")]
|
#[graphql(description = "List of all users")]
|
||||||
fn users(context: &Context) -> FieldResult<Vec<User>> {
|
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
|
let users = conn
|
||||||
.prep_exec("select * from user", ())
|
.exec("SELECT * FROM user", ())
|
||||||
.map(|result| {
|
.unwrap()
|
||||||
result
|
.into_iter()
|
||||||
.map(|x| x.unwrap())
|
.map(User::from_row)
|
||||||
.map(|row| {
|
.collect();
|
||||||
let (id, name, email) = from_row(row);
|
|
||||||
User { id, name, email }
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
Ok(users)
|
Ok(users)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[graphql(description = "Get Single user reference by user ID")]
|
#[graphql(description = "Get Single user reference by user ID")]
|
||||||
fn user(context: &Context, id: String) -> FieldResult<User> {
|
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> =
|
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 {
|
if let Err(_err) = user {
|
||||||
return Err(FieldError::new(
|
return Err(FieldError::new(
|
||||||
@ -57,32 +53,23 @@ impl QueryRoot {
|
|||||||
|
|
||||||
#[graphql(description = "List of all users")]
|
#[graphql(description = "List of all users")]
|
||||||
fn products(context: &Context) -> FieldResult<Vec<Product>> {
|
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
|
let products = conn
|
||||||
.prep_exec("select * from product", ())
|
.exec("SELECT * FROM product", ())
|
||||||
.map(|result| {
|
.unwrap()
|
||||||
result
|
.into_iter()
|
||||||
.map(|x| x.unwrap())
|
.map(Product::from_row)
|
||||||
.map(|row| {
|
.collect();
|
||||||
let (id, user_id, name, price) = from_row(row);
|
|
||||||
Product {
|
|
||||||
id,
|
|
||||||
user_id,
|
|
||||||
name,
|
|
||||||
price,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
Ok(products)
|
Ok(products)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[graphql(description = "Get Single user reference by user ID")]
|
#[graphql(description = "Get Single user reference by user ID")]
|
||||||
fn product(context: &Context, id: String) -> FieldResult<Product> {
|
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> =
|
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 {
|
if let Err(_err) = product {
|
||||||
return Err(FieldError::new(
|
return Err(FieldError::new(
|
||||||
"Product Not Found",
|
"Product Not Found",
|
||||||
@ -105,10 +92,10 @@ pub struct MutationRoot;
|
|||||||
#[graphql_object(Context = Context)]
|
#[graphql_object(Context = Context)]
|
||||||
impl MutationRoot {
|
impl MutationRoot {
|
||||||
fn create_user(context: &Context, user: UserInput) -> FieldResult<User> {
|
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 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)",
|
"INSERT INTO user(id, name, email) VALUES(:id, :name, :email)",
|
||||||
params! {
|
params! {
|
||||||
"id" => &new_id,
|
"id" => &new_id,
|
||||||
@ -137,10 +124,10 @@ impl MutationRoot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn create_product(context: &Context, product: ProductInput) -> FieldResult<Product> {
|
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 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)",
|
"INSERT INTO product(id, user_id, name, price) VALUES(:id, :user_id, :name, :price)",
|
||||||
params! {
|
params! {
|
||||||
"id" => &new_id,
|
"id" => &new_id,
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
use juniper::{graphql_object, GraphQLInputObject};
|
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::{product::Product, root::Context};
|
||||||
use crate::schemas::root::Context;
|
|
||||||
|
|
||||||
/// User
|
/// User
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
@ -12,6 +11,13 @@ pub struct User {
|
|||||||
pub email: String,
|
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)]
|
#[derive(GraphQLInputObject)]
|
||||||
#[graphql(description = "User Input")]
|
#[graphql(description = "User Input")]
|
||||||
pub struct UserInput {
|
pub struct UserInput {
|
||||||
@ -32,28 +38,15 @@ impl User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn products(&self, context: &Context) -> Vec<Product> {
|
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(
|
conn.exec(
|
||||||
"select * from product where user_id=:user_id",
|
"SELECT * FROM product WHERE user_id = :user_id",
|
||||||
params! {
|
params! { "user_id" => &self.id },
|
||||||
"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()
|
.unwrap()
|
||||||
|
.into_iter()
|
||||||
|
.map(Product::from_row)
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user