mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
update graphql advanced example
This commit is contained in:
@ -1 +1 @@
|
||||
DATABASE_URL=mysql://user:password@127.0.0.1/dbname
|
||||
DATABASE_URL=mysql://user:password@127.0.0.1/graphql_testing
|
||||
|
@ -3,20 +3,20 @@ name = "juniper-advanced"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-web = "4.0.0-rc.3"
|
||||
actix-web-lab = "0.10"
|
||||
actix-cors = "0.6.0-beta.10"
|
||||
|
||||
juniper = "0.15"
|
||||
|
||||
mysql = "17"
|
||||
r2d2 = "0.8"
|
||||
r2d2_mysql = "17.0"
|
||||
r2d2_mysql = "17"
|
||||
|
||||
dotenv = "0.15"
|
||||
env_logger = "0.8"
|
||||
env_logger = "0.9"
|
||||
log = "0.4"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
uuid = { version = "0.8", features = ["serde", "v4"] }
|
||||
|
@ -1,28 +1,34 @@
|
||||
# juniper-advanced
|
||||
|
||||
GraphQL Implementation in Rust using Actix, Juniper, and Mysql as Database
|
||||
GraphQL Implementation in Rust using Actix, Juniper, and MySQL as Database
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Rust Installed
|
||||
- MySql as Database
|
||||
- MySQL server
|
||||
|
||||
## Database Configuration
|
||||
|
||||
Create a new database for this project, and import the existing database schema has been provided named ```mysql-schema.sql```.
|
||||
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
|
||||
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 in there.
|
||||
|
||||
```sh
|
||||
cat mysql-schema.sql | mysql -u root -D graphql_testing
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```sh
|
||||
# go to the root dir
|
||||
cd graphql/juniper-advanced
|
||||
cp .env.example .env
|
||||
# edit .env and insert your DB credentials
|
||||
cargo run
|
||||
```
|
||||
|
||||
## GraphQL Playground
|
||||
|
||||
GraphQL provides its own documentation. Click the "docs" link in the top right of the GraphiQL UI to see what types of queries and mutations are possible.
|
||||
|
||||
```
|
||||
http://localhost:8080/graphiql
|
||||
```
|
||||
http://127.0.0.1:8080/graphiql
|
||||
```
|
@ -1,6 +1,6 @@
|
||||
-- MySQL dump 10.13 Distrib 8.0.16, for osx10.14 (x86_64)
|
||||
--
|
||||
-- Host: 127.0.0.1 Database: rust_graphql
|
||||
-- Host: 127.0.0.1 Database: graphql_testing
|
||||
-- ------------------------------------------------------
|
||||
-- Server version 8.0.15
|
||||
|
||||
|
@ -1,10 +1,14 @@
|
||||
use actix_web::{web, Error, HttpResponse};
|
||||
use juniper::http::graphiql::graphiql_source;
|
||||
use juniper::http::GraphQLRequest;
|
||||
use actix_web::{get, route, web, Error, HttpResponse, Responder};
|
||||
use actix_web_lab::respond::Html;
|
||||
use juniper::http::{graphiql::graphiql_source, GraphQLRequest};
|
||||
|
||||
use crate::db::Pool;
|
||||
use crate::schemas::root::{create_schema, Context, Schema};
|
||||
use crate::{
|
||||
db::Pool,
|
||||
schemas::root::{create_schema, Context, Schema},
|
||||
};
|
||||
|
||||
/// GraphQL endpoint
|
||||
#[route("/graphql", method = "GET", method = "POST")]
|
||||
pub async fn graphql(
|
||||
pool: web::Data<Pool>,
|
||||
schema: web::Data<Schema>,
|
||||
@ -13,27 +17,21 @@ pub async fn graphql(
|
||||
let ctx = Context {
|
||||
dbpool: pool.get_ref().to_owned(),
|
||||
};
|
||||
let res = web::block(move || {
|
||||
let res = data.execute_sync(&schema, &ctx);
|
||||
serde_json::to_string(&res)
|
||||
})
|
||||
.await
|
||||
.map_err(Error::from)?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(res))
|
||||
let res = data.execute(&schema, &ctx).await;
|
||||
|
||||
Ok(HttpResponse::Ok().json(res))
|
||||
}
|
||||
|
||||
pub async fn graphql_playground() -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(graphiql_source("/graphql", None))
|
||||
/// GraphiQL UI
|
||||
#[get("/graphiql")]
|
||||
async fn graphql_playground() -> impl Responder {
|
||||
Html(graphiql_source("/graphql", None))
|
||||
}
|
||||
|
||||
pub fn register(config: &mut web::ServiceConfig) {
|
||||
config
|
||||
.data(create_schema())
|
||||
.route("/graphql", web::post().to(graphql))
|
||||
.route("/graphiql", web::get().to(graphql_playground));
|
||||
.app_data(web::Data::new(create_schema()))
|
||||
.service(graphql)
|
||||
.service(graphql_playground);
|
||||
}
|
||||
|
@ -1,32 +1,33 @@
|
||||
#[macro_use]
|
||||
extern crate juniper;
|
||||
extern crate r2d2;
|
||||
extern crate r2d2_mysql;
|
||||
extern crate serde_json;
|
||||
|
||||
use actix_web::{middleware, web, App, HttpServer};
|
||||
|
||||
use crate::db::get_db_pool;
|
||||
use crate::handlers::register;
|
||||
use actix_cors::Cors;
|
||||
use actix_web::{middleware::Logger, web::Data, App, HttpServer};
|
||||
|
||||
mod db;
|
||||
mod handlers;
|
||||
mod schemas;
|
||||
|
||||
use self::{db::get_db_pool, handlers::register};
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
dotenv::dotenv().ok();
|
||||
std::env::set_var("RUST_LOG", "actix_web=info,info");
|
||||
env_logger::init();
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
let pool = get_db_pool();
|
||||
|
||||
log::info!("starting HTTP server on port 8080");
|
||||
log::info!("the GraphiQL interface HTTP server at http://localhost:8080/graphiql");
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.data(pool.clone())
|
||||
.wrap(middleware::Logger::default())
|
||||
.app_data(Data::new(pool.clone()))
|
||||
.configure(register)
|
||||
.default_service(web::to(|| async { "404" }))
|
||||
.wrap(Cors::permissive())
|
||||
.wrap(Logger::default())
|
||||
})
|
||||
.workers(2)
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
|
Reference in New Issue
Block a user