diff --git a/rust/crates/web/src/handler.rs b/rust/crates/web/src/handler.rs index 1c67a66..e205d42 100644 --- a/rust/crates/web/src/handler.rs +++ b/rust/crates/web/src/handler.rs @@ -3,7 +3,7 @@ mod root; use axum::Router; -pub use self::graphql::{Data, HasGraphQLSchema}; +pub use self::graphql::HasGraphQLSchema; use crate::use_case::HasStore; pub fn route(store: T) -> Router { diff --git a/rust/crates/web/src/handler/graphql.rs b/rust/crates/web/src/handler/graphql.rs index 567f150..41fc0f0 100644 --- a/rust/crates/web/src/handler/graphql.rs +++ b/rust/crates/web/src/handler/graphql.rs @@ -1,9 +1,9 @@ +mod graphql_data; +mod graphql_schema; mod mutation; mod query; -use std::sync::Arc; - -use async_graphql::{http::GraphiQLSource, EmptySubscription, Schema}; +use async_graphql::http::GraphiQLSource; use async_graphql_axum::{GraphQLRequest, GraphQLResponse}; use axum::{ extract::State, @@ -16,31 +16,27 @@ use axum::{ }; use hyper::StatusCode; -use crate::use_case::{HasStore, Store}; - -use self::{mutation::MutationRoot, query::QueryRoot}; +use crate::use_case::HasStore; -#[derive(Clone)] -pub struct GraphQLSchema(Schema); - -impl GraphQLSchema { - pub fn new() -> Self { - Self(Schema::new(QueryRoot, MutationRoot, EmptySubscription)) - } -} +use self::graphql_data::GraphQLData; +pub use self::graphql_schema::GraphQLSchema; pub trait HasGraphQLSchema { fn graphql_schema(&self) -> &GraphQLSchema; } -async fn handler( +async fn get_handler() -> impl IntoResponse { + Html(GraphiQLSource::build().endpoint("/graphql").finish()) +} + +async fn post_handler( State(state): State, header_map: HeaderMap, request: GraphQLRequest, ) -> Result { let schema = state.graphql_schema(); let store = state.store(); - let request = request.into_inner().data(Data { + let request = request.into_inner().data(GraphQLData { bearer: match header_map.get(axum::http::header::AUTHORIZATION) { Some(header_value) => { Some(Bearer::decode(header_value).ok_or(StatusCode::UNAUTHORIZED)?) @@ -49,20 +45,14 @@ async fn handler( }, store, }); - Ok(GraphQLResponse::from(schema.0.execute(request).await)) -} - -async fn graphiql() -> impl IntoResponse { - Html(GraphiQLSource::build().endpoint("/graphql").finish()) -} - -pub struct Data { - pub bearer: Option, - pub store: Arc, + Ok(GraphQLResponse::from(schema.execute(request).await)) } pub fn route() -> Router { - Router::new().route("/graphql", routing::get(graphiql).post(handler::)) + Router::new().route( + "/graphql", + routing::get(get_handler).post(post_handler::), + ) } #[cfg(test)] diff --git a/rust/crates/web/src/handler/graphql/graphql_data.rs b/rust/crates/web/src/handler/graphql/graphql_data.rs new file mode 100644 index 0000000..55c04c0 --- /dev/null +++ b/rust/crates/web/src/handler/graphql/graphql_data.rs @@ -0,0 +1,10 @@ +use std::sync::Arc; + +use axum::headers::authorization::Bearer; + +use crate::use_case::Store; + +pub struct GraphQLData { + pub bearer: Option, + pub store: Arc, +} diff --git a/rust/crates/web/src/handler/graphql/graphql_schema.rs b/rust/crates/web/src/handler/graphql/graphql_schema.rs new file mode 100644 index 0000000..f2b8726 --- /dev/null +++ b/rust/crates/web/src/handler/graphql/graphql_schema.rs @@ -0,0 +1,18 @@ +use super::{mutation::MutationRoot, query::QueryRoot}; +use async_graphql::{EmptySubscription, Request, Response, Schema}; + +#[derive(Clone)] +pub struct GraphQLSchema(Schema); + +impl GraphQLSchema { + pub fn new() -> Self { + Self(Schema::new(QueryRoot, MutationRoot, EmptySubscription)) + } + + pub async fn execute(&self, request: R) -> Response + where + R: Into, + { + self.0.execute(request).await + } +} diff --git a/rust/crates/web/src/handler/graphql/query.rs b/rust/crates/web/src/handler/graphql/query.rs index 1200826..9305a6e 100644 --- a/rust/crates/web/src/handler/graphql/query.rs +++ b/rust/crates/web/src/handler/graphql/query.rs @@ -4,16 +4,16 @@ mod item; use async_graphql::Context; -use crate::handler::Data; - use self::{check_list::CheckList, item::Item}; +use super::graphql_data::GraphQLData; + pub struct QueryRoot; #[async_graphql::Object] impl QueryRoot { async fn bearer<'a>(&self, context: &Context<'a>) -> &'a str { - let data = context.data_unchecked::(); + let data = context.data_unchecked::(); data.bearer.as_ref().unwrap().token() } @@ -29,7 +29,7 @@ impl QueryRoot { &self, context: &Context<'a>, ) -> async_graphql::Result> { - let store = &context.data_unchecked::().store; + let store = &context.data_unchecked::().store; Ok(store .find_all_check_lists() .await? @@ -39,7 +39,7 @@ impl QueryRoot { } async fn items<'a>(&self, ctx: &Context<'a>) -> async_graphql::Result> { - let store = &ctx.data_unchecked::().store; + let store = &ctx.data_unchecked::().store; Ok(store .find_all_items() .await? diff --git a/rust/crates/web/src/handler/graphql/query/check_list.rs b/rust/crates/web/src/handler/graphql/query/check_list.rs index 3b30af8..1ddeaad 100644 --- a/rust/crates/web/src/handler/graphql/query/check_list.rs +++ b/rust/crates/web/src/handler/graphql/query/check_list.rs @@ -1,6 +1,6 @@ use async_graphql::Context; -use crate::{handler::Data, model}; +use crate::{handler::graphql::graphql_data::GraphQLData, model}; use super::item::Item; @@ -19,7 +19,7 @@ impl CheckList { } async fn checked_items(&self, context: &Context<'_>) -> async_graphql::Result> { - let store = &context.data_unchecked::().store; + let store = &context.data_unchecked::().store; let items = store.find_all_items().await?; // TODO: Store::find_checks_by_check_list_id let checks = store.find_all_checks().await?; diff --git a/rust/crates/web/src/handler/graphql/query/item.rs b/rust/crates/web/src/handler/graphql/query/item.rs index 7de70fe..2b8034c 100644 --- a/rust/crates/web/src/handler/graphql/query/item.rs +++ b/rust/crates/web/src/handler/graphql/query/item.rs @@ -1,7 +1,7 @@ use async_graphql::Context; use crate::{ - handler::Data, + handler::graphql::graphql_data::GraphQLData, model::{self}, }; @@ -24,7 +24,7 @@ impl Item { &self, context: &Context<'_>, ) -> async_graphql::Result> { - let store = &context.data_unchecked::().store; + let store = &context.data_unchecked::().store; let check_lists = store.find_all_check_lists().await?; // TODO: Store::find_checks_by_item_id let checks = store.find_all_checks().await?;