mirror of
https://github.com/actix/examples
synced 2025-06-27 01:27:43 +02:00
update graphql advanced example
This commit is contained in:
@ -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