mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
Upgrade juniper
to 0.15 (#423)
This commit is contained in:
@ -9,7 +9,7 @@ edition = "2018"
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
|
||||
juniper = "0.14"
|
||||
juniper = "0.15"
|
||||
|
||||
mysql = "17"
|
||||
r2d2 = "0.8"
|
||||
|
@ -2,18 +2,18 @@
|
||||
|
||||
GraphQL Implementation in Rust using Actix, Juniper, and Mysql as Database
|
||||
|
||||
# Prerequites
|
||||
## Prerequisites
|
||||
|
||||
- Rust Installed
|
||||
- MySql as Database
|
||||
|
||||
# Database Configuration
|
||||
## Database Configuration
|
||||
|
||||
Create a new database for this project, and import the existing database schema has been provided named ```mysql-schema.sql```.
|
||||
|
||||
Create ```.env``` file on the root directory of this project and set environment variable named ```DATABASE_URL```, the example file has been provided named ```.env.example```, you can see the format on there.
|
||||
|
||||
# Run
|
||||
|
||||
## Run
|
||||
|
||||
```sh
|
||||
# go to the root dir
|
||||
@ -23,6 +23,6 @@ cd juniper-advanced
|
||||
cargo run
|
||||
```
|
||||
|
||||
### GraphQL Playground
|
||||
## GraphQL Playground
|
||||
|
||||
http://127.0.0.1:8080/graphiql
|
||||
<http://127.0.0.1:8080/graphiql>
|
||||
|
@ -14,7 +14,7 @@ pub async fn graphql(
|
||||
dbpool: pool.get_ref().to_owned(),
|
||||
};
|
||||
let res = web::block(move || {
|
||||
let res = data.execute(&schema, &ctx);
|
||||
let res = data.execute_sync(&schema, &ctx);
|
||||
Ok::<_, serde_json::error::Error>(serde_json::to_string(&res)?)
|
||||
})
|
||||
.await
|
||||
@ -28,7 +28,7 @@ pub async fn graphql(
|
||||
pub async fn graphql_playground() -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(graphiql_source("/graphql"))
|
||||
.body(graphiql_source("/graphql", None))
|
||||
}
|
||||
|
||||
pub fn register(config: &mut web::ServiceConfig) {
|
||||
|
@ -12,7 +12,7 @@ pub struct Product {
|
||||
pub price: f64,
|
||||
}
|
||||
|
||||
#[juniper::object(Context = Context)]
|
||||
#[juniper::graphql_object(Context = Context)]
|
||||
impl Product {
|
||||
fn id(&self) -> &str {
|
||||
&self.id
|
||||
@ -33,7 +33,7 @@ impl Product {
|
||||
"SELECT * FROM user WHERE id=:id",
|
||||
params! {"id" => &self.user_id},
|
||||
);
|
||||
if let Err(err) = user {
|
||||
if let Err(_err) = user {
|
||||
None
|
||||
} else {
|
||||
let (id, name, email) = from_row(user.unwrap().unwrap());
|
||||
|
@ -1,4 +1,4 @@
|
||||
use juniper::{FieldError, FieldResult, RootNode};
|
||||
use juniper::{EmptySubscription, FieldError, FieldResult, RootNode};
|
||||
use mysql::{from_row, params, Error as DBError, Row};
|
||||
|
||||
use crate::db::Pool;
|
||||
@ -14,7 +14,7 @@ impl juniper::Context for Context {}
|
||||
|
||||
pub struct QueryRoot;
|
||||
|
||||
#[juniper::object(Context = Context)]
|
||||
#[juniper::graphql_object(Context = Context)]
|
||||
impl QueryRoot {
|
||||
#[graphql(description = "List of all users")]
|
||||
fn users(context: &Context) -> FieldResult<Vec<User>> {
|
||||
@ -24,7 +24,7 @@ impl QueryRoot {
|
||||
.map(|result| {
|
||||
result
|
||||
.map(|x| x.unwrap())
|
||||
.map(|mut row| {
|
||||
.map(|row| {
|
||||
let (id, name, email) = from_row(row);
|
||||
User { id, name, email }
|
||||
})
|
||||
@ -41,7 +41,7 @@ impl QueryRoot {
|
||||
let user: Result<Option<Row>, DBError> =
|
||||
conn.first_exec("SELECT * FROM user WHERE id=:id", params! {"id" => id});
|
||||
|
||||
if let Err(err) = user {
|
||||
if let Err(_err) = user {
|
||||
return Err(FieldError::new(
|
||||
"User Not Found",
|
||||
graphql_value!({ "not_found": "user not found" }),
|
||||
@ -60,7 +60,7 @@ impl QueryRoot {
|
||||
.map(|result| {
|
||||
result
|
||||
.map(|x| x.unwrap())
|
||||
.map(|mut row| {
|
||||
.map(|row| {
|
||||
let (id, user_id, name, price) = from_row(row);
|
||||
Product {
|
||||
id,
|
||||
@ -80,7 +80,7 @@ impl QueryRoot {
|
||||
let mut conn = context.dbpool.get().unwrap();
|
||||
let product: Result<Option<Row>, DBError> =
|
||||
conn.first_exec("SELECT * FROM user WHERE id=:id", params! {"id" => id});
|
||||
if let Err(err) = product {
|
||||
if let Err(_err) = product {
|
||||
return Err(FieldError::new(
|
||||
"Product Not Found",
|
||||
graphql_value!({ "not_found": "product not found" }),
|
||||
@ -99,7 +99,7 @@ impl QueryRoot {
|
||||
|
||||
pub struct MutationRoot;
|
||||
|
||||
#[juniper::object(Context = Context)]
|
||||
#[juniper::graphql_object(Context = Context)]
|
||||
impl MutationRoot {
|
||||
fn create_user(context: &Context, user: UserInput) -> FieldResult<User> {
|
||||
let mut conn = context.dbpool.get().unwrap();
|
||||
@ -115,7 +115,7 @@ impl MutationRoot {
|
||||
);
|
||||
|
||||
match insert {
|
||||
Ok(opt_row) => Ok(User {
|
||||
Ok(_opt_row) => Ok(User {
|
||||
id: new_id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
@ -148,7 +148,7 @@ impl MutationRoot {
|
||||
);
|
||||
|
||||
match insert {
|
||||
Ok(opt_row) => Ok(Product {
|
||||
Ok(_opt_row) => Ok(Product {
|
||||
id: new_id,
|
||||
user_id: product.user_id,
|
||||
name: product.name,
|
||||
@ -168,8 +168,8 @@ impl MutationRoot {
|
||||
}
|
||||
}
|
||||
|
||||
pub type Schema = RootNode<'static, QueryRoot, MutationRoot>;
|
||||
pub type Schema = RootNode<'static, QueryRoot, MutationRoot, EmptySubscription<Context>>;
|
||||
|
||||
pub fn create_schema() -> Schema {
|
||||
Schema::new(QueryRoot, MutationRoot)
|
||||
Schema::new(QueryRoot, MutationRoot, EmptySubscription::new())
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ pub struct UserInput {
|
||||
pub email: String,
|
||||
}
|
||||
|
||||
#[juniper::object(Context = Context)]
|
||||
#[juniper::graphql_object(Context = Context)]
|
||||
impl User {
|
||||
fn id(&self) -> &str {
|
||||
&self.id
|
||||
@ -42,7 +42,7 @@ impl User {
|
||||
.map(|result| {
|
||||
result
|
||||
.map(|x| x.unwrap())
|
||||
.map(|mut row| {
|
||||
.map(|row| {
|
||||
let (id, user_id, name, price) = from_row(row);
|
||||
Product {
|
||||
id,
|
||||
|
Reference in New Issue
Block a user