with graphql

This commit is contained in:
Kasper Juul Hermansen 2022-10-04 12:06:00 +02:00
parent 5e9001b998
commit c7f8dc6198
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
8 changed files with 39 additions and 44 deletions

7
.env
View File

@ -1,4 +1,11 @@
POSTGRES_DB=como POSTGRES_DB=como
POSTGRES_USER=como POSTGRES_USER=como
POSTGRES_PASSWORD=somenotverysecurepassword POSTGRES_PASSWORD=somenotverysecurepassword
DATABASE_URL="postgres://como:somenotverysecurepassword@localhost:5432/como" 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

View File

@ -1,11 +1,21 @@
use axum::{routing::get, Router}; use async_graphql::{EmptySubscription, Schema};
use como_gql::{graphql_handler, graphql_playground}; use axum::{routing::get, Extension, Router};
use como_gql::{
graphql::{MutationRoot, QueryRoot},
graphql_handler, graphql_playground,
};
use como_infrastructure::register::ServiceRegister; use como_infrastructure::register::ServiceRegister;
pub struct GraphQLController; pub struct GraphQLController;
impl GraphQLController { impl GraphQLController {
pub fn new_router(_service_register: ServiceRegister) -> Router { pub fn new_router(service_register: ServiceRegister) -> Router {
Router::new().route("/", get(graphql_playground).post(graphql_handler)) let schema = Schema::build(QueryRoot, MutationRoot, EmptySubscription)
.data(service_register)
.finish();
Router::new()
.route("/", get(graphql_playground).post(graphql_handler))
.layer(Extension(schema))
} }
} }

View File

@ -1,10 +1,7 @@
use std::{ use std::sync::Arc;
sync::Arc,
};
mod error; mod error;
use clap::Parser; use clap::Parser;
use anyhow::Context; use anyhow::Context;
@ -14,9 +11,6 @@ use como_infrastructure::{
configs::AppConfig, database::ConnectionPoolManager, register::ServiceRegister, configs::AppConfig, database::ConnectionPoolManager, register::ServiceRegister,
}; };
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[tokio::main] #[tokio::main]
@ -27,12 +21,7 @@ async fn main() -> anyhow::Result<()> {
let config = Arc::new(AppConfig::parse()); let config = Arc::new(AppConfig::parse());
tracing_subscriber::registry() tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new( .with(tracing_subscriber::EnvFilter::new(&config.rust_log))
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::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();
@ -40,9 +29,13 @@ async fn main() -> anyhow::Result<()> {
let service_register = ServiceRegister::new(pool, config.clone()); let service_register = ServiceRegister::new(pool, config.clone());
Api::new(3001, &config.cors_origin, service_register.clone()) Api::new(
.await config.api_port,
.context("could not initialize API")?; &config.cors_origin,
service_register.clone(),
)
.await
.context("could not initialize API")?;
Ok(()) Ok(())
} }

View File

@ -3,8 +3,7 @@ use async_graphql::{Context, EmptySubscription, Object, Schema};
use como_domain::item::{requests::CreateItemDto, responses::CreatedItemDto}; use como_domain::item::{requests::CreateItemDto, responses::CreatedItemDto};
use como_infrastructure::register::ServiceRegister; use como_infrastructure::register::ServiceRegister;
pub type ComoSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;
pub type CibusSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;
pub struct MutationRoot; pub struct MutationRoot;

View File

@ -1,30 +1,17 @@
use async_graphql_axum::{GraphQLRequest, GraphQLResponse}; use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
use axum::{ use axum::{
extract::Extension, extract::Extension,
http::{StatusCode}, http::StatusCode,
response::{Html, IntoResponse}, response::{Html, IntoResponse},
}; };
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
use async_graphql::{ use graphql::ComoSchema;
http::{playground_source, GraphQLPlaygroundConfig},
};
use axum_sessions::{
extractors::{ReadableSession},
};
use graphql::CibusSchema;
pub mod graphql; pub mod graphql;
pub async fn graphql_handler( pub async fn graphql_handler(
schema: Extension<CibusSchema>, schema: Extension<ComoSchema>,
_session: ReadableSession,
req: GraphQLRequest, req: GraphQLRequest,
) -> Result<GraphQLResponse, StatusCode> { ) -> Result<GraphQLResponse, StatusCode> {
let req = req.into_inner(); let req = req.into_inner();
@ -33,5 +20,5 @@ pub async fn graphql_handler(
} }
pub async fn graphql_playground() -> impl IntoResponse { pub async fn graphql_playground() -> impl IntoResponse {
Html(playground_source(GraphQLPlaygroundConfig::new("/"))) Html(playground_source(GraphQLPlaygroundConfig::new("/graphql")))
} }

View File

@ -7,7 +7,7 @@ pub struct AppConfig {
#[clap(long, env)] #[clap(long, env)]
pub token_secret: String, pub token_secret: String,
#[clap(long, env)] #[clap(long, env)]
pub port: u32, pub api_port: u32,
#[clap(long, env)] #[clap(long, env)]
pub run_migrations: bool, pub run_migrations: bool,
#[clap(long, env)] #[clap(long, env)]
@ -15,4 +15,3 @@ pub struct AppConfig {
#[clap(long, env)] #[clap(long, env)]
pub cors_origin: String, pub cors_origin: String,
} }

View File

@ -25,7 +25,7 @@ impl ConnectionPoolManager {
sqlx::migrate!() sqlx::migrate!()
.run(&pool) .run(&pool)
.await .await
.context("error while running database migrations"); .context("error while running database migrations")?;
} }
Ok(pool) Ok(pool)

View File

@ -2,4 +2,4 @@
set -e set -e
cargo run como_bin/ (cd como_bin; cargo watch -x run)