From c7f8dc6198704205361add9134bee2d3b83299c2 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Tue, 4 Oct 2022 12:06:00 +0200 Subject: [PATCH] with graphql --- .env | 7 +++++++ como_api/src/controllers/graphql.rs | 18 ++++++++++++++---- como_bin/src/main.rs | 25 +++++++++---------------- como_gql/src/graphql.rs | 3 +-- como_gql/src/lib.rs | 23 +++++------------------ como_infrastructure/src/configs/mod.rs | 3 +-- como_infrastructure/src/database/mod.rs | 2 +- scripts/run_como.sh | 2 +- 8 files changed, 39 insertions(+), 44 deletions(-) diff --git a/.env b/.env index 0021ecf..43d682a 100644 --- a/.env +++ b/.env @@ -1,4 +1,11 @@ POSTGRES_DB=como POSTGRES_USER=como POSTGRES_PASSWORD=somenotverysecurepassword + DATABASE_URL="postgres://como:somenotverysecurepassword@localhost:5432/como" +RUST_LOG=como_api=info,como_bin=info,como_core=info,como_domain=info,como_gql=info,como_infrastructure=info,sqlx=debug,tower_http=debug +TOKEN_SECRET=something +API_PORT=3001 +CORS_ORIGIN=http://localhost:3000 +RUN_MIGRATIONS=true +SEED=true diff --git a/como_api/src/controllers/graphql.rs b/como_api/src/controllers/graphql.rs index 4893f67..67bfdfd 100644 --- a/como_api/src/controllers/graphql.rs +++ b/como_api/src/controllers/graphql.rs @@ -1,11 +1,21 @@ -use axum::{routing::get, Router}; -use como_gql::{graphql_handler, graphql_playground}; +use async_graphql::{EmptySubscription, Schema}; +use axum::{routing::get, Extension, Router}; +use como_gql::{ + graphql::{MutationRoot, QueryRoot}, + graphql_handler, graphql_playground, +}; use como_infrastructure::register::ServiceRegister; pub struct GraphQLController; impl GraphQLController { - pub fn new_router(_service_register: ServiceRegister) -> Router { - Router::new().route("/", get(graphql_playground).post(graphql_handler)) + pub fn new_router(service_register: ServiceRegister) -> Router { + let schema = Schema::build(QueryRoot, MutationRoot, EmptySubscription) + .data(service_register) + .finish(); + + Router::new() + .route("/", get(graphql_playground).post(graphql_handler)) + .layer(Extension(schema)) } } diff --git a/como_bin/src/main.rs b/como_bin/src/main.rs index ef178ce..a3739d6 100644 --- a/como_bin/src/main.rs +++ b/como_bin/src/main.rs @@ -1,10 +1,7 @@ -use std::{ - sync::Arc, -}; +use std::sync::Arc; mod error; - use clap::Parser; use anyhow::Context; @@ -14,9 +11,6 @@ use como_infrastructure::{ configs::AppConfig, database::ConnectionPoolManager, register::ServiceRegister, }; - - - use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; #[tokio::main] @@ -27,12 +21,7 @@ async fn main() -> anyhow::Result<()> { let config = Arc::new(AppConfig::parse()); tracing_subscriber::registry() - .with(tracing_subscriber::EnvFilter::new( - std::env::var("RUST_LOG").unwrap_or_else(|_| { - "como_bin=debug,tower_http=debug,axum_extra=debug,hyper=info,mio=info,sqlx=info,async_graphql=debug" - .into() - }), - )) + .with(tracing_subscriber::EnvFilter::new(&config.rust_log)) .with(tracing_subscriber::fmt::layer()) .init(); @@ -40,9 +29,13 @@ async fn main() -> anyhow::Result<()> { let service_register = ServiceRegister::new(pool, config.clone()); - Api::new(3001, &config.cors_origin, service_register.clone()) - .await - .context("could not initialize API")?; + Api::new( + config.api_port, + &config.cors_origin, + service_register.clone(), + ) + .await + .context("could not initialize API")?; Ok(()) } diff --git a/como_gql/src/graphql.rs b/como_gql/src/graphql.rs index 3b10e29..6cf9530 100644 --- a/como_gql/src/graphql.rs +++ b/como_gql/src/graphql.rs @@ -3,8 +3,7 @@ use async_graphql::{Context, EmptySubscription, Object, Schema}; use como_domain::item::{requests::CreateItemDto, responses::CreatedItemDto}; use como_infrastructure::register::ServiceRegister; - -pub type CibusSchema = Schema; +pub type ComoSchema = Schema; pub struct MutationRoot; diff --git a/como_gql/src/lib.rs b/como_gql/src/lib.rs index 47a5563..26783c5 100644 --- a/como_gql/src/lib.rs +++ b/como_gql/src/lib.rs @@ -1,30 +1,17 @@ use async_graphql_axum::{GraphQLRequest, GraphQLResponse}; use axum::{ extract::Extension, - http::{StatusCode}, + http::StatusCode, response::{Html, IntoResponse}, }; - -use async_graphql::{ - http::{playground_source, GraphQLPlaygroundConfig}, -}; -use axum_sessions::{ - extractors::{ReadableSession}, -}; -use graphql::CibusSchema; - - - - - - +use async_graphql::http::{playground_source, GraphQLPlaygroundConfig}; +use graphql::ComoSchema; pub mod graphql; pub async fn graphql_handler( - schema: Extension, - _session: ReadableSession, + schema: Extension, req: GraphQLRequest, ) -> Result { let req = req.into_inner(); @@ -33,5 +20,5 @@ pub async fn graphql_handler( } pub async fn graphql_playground() -> impl IntoResponse { - Html(playground_source(GraphQLPlaygroundConfig::new("/"))) + Html(playground_source(GraphQLPlaygroundConfig::new("/graphql"))) } diff --git a/como_infrastructure/src/configs/mod.rs b/como_infrastructure/src/configs/mod.rs index f34043d..8036530 100644 --- a/como_infrastructure/src/configs/mod.rs +++ b/como_infrastructure/src/configs/mod.rs @@ -7,7 +7,7 @@ pub struct AppConfig { #[clap(long, env)] pub token_secret: String, #[clap(long, env)] - pub port: u32, + pub api_port: u32, #[clap(long, env)] pub run_migrations: bool, #[clap(long, env)] @@ -15,4 +15,3 @@ pub struct AppConfig { #[clap(long, env)] pub cors_origin: String, } - diff --git a/como_infrastructure/src/database/mod.rs b/como_infrastructure/src/database/mod.rs index 8ed0a25..0f4c699 100644 --- a/como_infrastructure/src/database/mod.rs +++ b/como_infrastructure/src/database/mod.rs @@ -25,7 +25,7 @@ impl ConnectionPoolManager { sqlx::migrate!() .run(&pool) .await - .context("error while running database migrations"); + .context("error while running database migrations")?; } Ok(pool) diff --git a/scripts/run_como.sh b/scripts/run_como.sh index c8bd97c..9f4384b 100755 --- a/scripts/run_como.sh +++ b/scripts/run_como.sh @@ -2,4 +2,4 @@ set -e -cargo run como_bin/ +(cd como_bin; cargo watch -x run)