Skip to content

Commit

Permalink
Extract graphql_{data,schema} mod
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Nov 20, 2023
1 parent 3a318a7 commit 49856bb
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 37 deletions.
2 changes: 1 addition & 1 deletion rust/crates/web/src/handler.rs
Expand Up @@ -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<T: Clone + HasGraphQLSchema + HasStore + Send + Sync + 'static>(store: T) -> Router {
Expand Down
44 changes: 17 additions & 27 deletions 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,
Expand All @@ -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<QueryRoot, MutationRoot, EmptySubscription>);

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<T: HasGraphQLSchema + HasStore>(
async fn get_handler() -> impl IntoResponse {
Html(GraphiQLSource::build().endpoint("/graphql").finish())
}

async fn post_handler<T: HasGraphQLSchema + HasStore>(
State(state): State<T>,
header_map: HeaderMap,
request: GraphQLRequest,
) -> Result<GraphQLResponse, StatusCode> {
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)?)
Expand All @@ -49,20 +45,14 @@ async fn handler<T: HasGraphQLSchema + HasStore>(
},
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<Bearer>,
pub store: Arc<dyn Store + Send + Sync>,
Ok(GraphQLResponse::from(schema.execute(request).await))
}

pub fn route<T: Clone + HasGraphQLSchema + HasStore + Send + Sync + 'static>() -> Router<T> {
Router::new().route("/graphql", routing::get(graphiql).post(handler::<T>))
Router::new().route(
"/graphql",
routing::get(get_handler).post(post_handler::<T>),
)
}

#[cfg(test)]
Expand Down
10 changes: 10 additions & 0 deletions 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<Bearer>,
pub store: Arc<dyn Store + Send + Sync>,
}
18 changes: 18 additions & 0 deletions 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<QueryRoot, MutationRoot, EmptySubscription>);

impl GraphQLSchema {
pub fn new() -> Self {
Self(Schema::new(QueryRoot, MutationRoot, EmptySubscription))
}

pub async fn execute<R>(&self, request: R) -> Response
where
R: Into<Request>,
{
self.0.execute(request).await
}
}
10 changes: 5 additions & 5 deletions rust/crates/web/src/handler/graphql/query.rs
Expand Up @@ -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::<Data>();
let data = context.data_unchecked::<GraphQLData>();
data.bearer.as_ref().unwrap().token()
}

Expand All @@ -29,7 +29,7 @@ impl QueryRoot {
&self,
context: &Context<'a>,
) -> async_graphql::Result<Vec<CheckList>> {
let store = &context.data_unchecked::<Data>().store;
let store = &context.data_unchecked::<GraphQLData>().store;
Ok(store
.find_all_check_lists()
.await?
Expand All @@ -39,7 +39,7 @@ impl QueryRoot {
}

async fn items<'a>(&self, ctx: &Context<'a>) -> async_graphql::Result<Vec<Item>> {
let store = &ctx.data_unchecked::<Data>().store;
let store = &ctx.data_unchecked::<GraphQLData>().store;
Ok(store
.find_all_items()
.await?
Expand Down
4 changes: 2 additions & 2 deletions 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;

Expand All @@ -19,7 +19,7 @@ impl CheckList {
}

async fn checked_items(&self, context: &Context<'_>) -> async_graphql::Result<Vec<Item>> {
let store = &context.data_unchecked::<Data>().store;
let store = &context.data_unchecked::<GraphQLData>().store;
let items = store.find_all_items().await?;
// TODO: Store::find_checks_by_check_list_id
let checks = store.find_all_checks().await?;
Expand Down
4 changes: 2 additions & 2 deletions 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},
};

Expand All @@ -24,7 +24,7 @@ impl Item {
&self,
context: &Context<'_>,
) -> async_graphql::Result<Vec<CheckList>> {
let store = &context.data_unchecked::<Data>().store;
let store = &context.data_unchecked::<GraphQLData>().store;
let check_lists = store.find_all_check_lists().await?;
// TODO: Store::find_checks_by_item_id
let checks = store.find_all_checks().await?;
Expand Down

0 comments on commit 49856bb

Please sign in to comment.