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_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

View File

@ -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))
}
}

View File

@ -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(())
}

View File

@ -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<QueryRoot, MutationRoot, EmptySubscription>;
pub type ComoSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;
pub struct MutationRoot;

View File

@ -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<CibusSchema>,
_session: ReadableSession,
schema: Extension<ComoSchema>,
req: GraphQLRequest,
) -> Result<GraphQLResponse, StatusCode> {
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")))
}

View File

@ -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,
}

View File

@ -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)

View File

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