1
0
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:
Yuki Okushi
2021-04-26 20:32:32 +09:00
committed by GitHub
parent ef13969220
commit bb4f97ba41
11 changed files with 137 additions and 50 deletions

View File

@ -9,7 +9,7 @@ edition = "2018"
[dependencies]
actix-web = "3"
juniper = "0.14"
juniper = "0.15"
mysql = "17"
r2d2 = "0.8"

View File

@ -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>

View File

@ -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) {

View File

@ -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());

View File

@ -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())
}

View File

@ -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,

View File

@ -11,4 +11,4 @@ env_logger = "0.8"
serde = "1.0.103"
serde_json = "1.0.44"
serde_derive = "1.0.103"
juniper = "0.14.2"
juniper = "0.15"

View File

@ -20,6 +20,7 @@ cargo run (or ``cargo watch -x run``)
[http://127.0.0.1:8080/graphiql](http://127.0.0.1:8080/graphiql)
_Query example:_
```graphql
{
human(id: "1234") {
@ -29,7 +30,9 @@ _Query example:_
}
}
```
_Result:_
```json
{
"data": {
@ -58,6 +61,7 @@ mutation {
```
_Result:_
```json
{
"data": {

View File

@ -14,7 +14,7 @@ mod schema;
use crate::schema::{create_schema, Schema};
async fn graphiql() -> HttpResponse {
let html = graphiql_source("http://127.0.0.1:8080/graphql");
let html = graphiql_source("http://127.0.0.1:8080/graphql", None);
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(html)
@ -25,7 +25,7 @@ async fn graphql(
data: web::Json<GraphQLRequest>,
) -> Result<HttpResponse, Error> {
let user = web::block(move || {
let res = data.execute(&st, &());
let res = data.execute_sync(&st, &());
Ok::<_, serde_json::error::Error>(serde_json::to_string(&res)?)
})
.await?;

View File

@ -1,5 +1,5 @@
use juniper::FieldResult;
use juniper::RootNode;
use juniper::{EmptySubscription, RootNode};
#[derive(GraphQLEnum)]
enum Episode {
@ -29,9 +29,9 @@ struct NewHuman {
pub struct QueryRoot;
#[juniper::object]
#[juniper::graphql_object]
impl QueryRoot {
fn human(id: String) -> FieldResult<Human> {
fn human(_id: String) -> FieldResult<Human> {
Ok(Human {
id: "1234".to_owned(),
name: "Luke".to_owned(),
@ -43,7 +43,7 @@ impl QueryRoot {
pub struct MutationRoot;
#[juniper::object]
#[juniper::graphql_object]
impl MutationRoot {
fn create_human(new_human: NewHuman) -> FieldResult<Human> {
Ok(Human {
@ -55,8 +55,8 @@ impl MutationRoot {
}
}
pub type Schema = RootNode<'static, QueryRoot, MutationRoot>;
pub type Schema = RootNode<'static, QueryRoot, MutationRoot, EmptySubscription>;
pub fn create_schema() -> Schema {
Schema::new(QueryRoot {}, MutationRoot {})
Schema::new(QueryRoot {}, MutationRoot {}, EmptySubscription::new())
}