Created
July 24, 2022 10:39
-
-
Save Mr-Malomz/1106f2b247aea875cfd772722cc795ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| mod config; | |
| mod handler; | |
| mod schemas; | |
| //add | |
| use actix_web::{ | |
| guard, | |
| web::{self, Data}, | |
| App, HttpResponse, HttpServer, | |
| }; | |
| use async_graphql::{ | |
| http::{playground_source, GraphQLPlaygroundConfig}, | |
| EmptySubscription, Schema, | |
| }; | |
| use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse}; | |
| use config::mongo::DBMongo; | |
| use handler::graphql_handler::{Mutation, ProjectSchema, Query}; | |
| //graphql entry | |
| async fn index(schema: Data<ProjectSchema>, req: GraphQLRequest) -> GraphQLResponse { | |
| schema.execute(req.into_inner()).await.into() | |
| } | |
| async fn graphql_playground() -> HttpResponse { | |
| HttpResponse::Ok() | |
| .content_type("text/html; charset=utf-8") | |
| .body(playground_source(GraphQLPlaygroundConfig::new("/"))) | |
| } | |
| #[actix_web::main] | |
| async fn main() -> std::io::Result<()> { | |
| //connect to the data source | |
| let db = DBMongo::init().await; | |
| let schema_data = Schema::build(Query, Mutation, EmptySubscription) | |
| .data(db) | |
| .finish(); | |
| HttpServer::new(move || { | |
| App::new() | |
| .app_data(Data::new(schema_data.clone())) | |
| .service(web::resource("/").guard(guard::Post()).to(index)) | |
| .service( | |
| web::resource("/") | |
| .guard(guard::Get()) | |
| .to(graphql_playground), | |
| ) | |
| }) | |
| .bind(("127.0.0.1", 8080))? | |
| .run() | |
| .await | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment