Skip to content

Commit

Permalink
Change HasStore
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Nov 19, 2023
1 parent 6cb580f commit 95c3945
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 15 deletions.
11 changes: 6 additions & 5 deletions rust/crates/web/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
use std::sync::Arc;

use async_graphql::{EmptySubscription, Schema};

use crate::{
infra::store::InMemoryStore,
mutation::MutationRoot,
query::QueryRoot,
use_case::{HasSchema, HasStore},
use_case::{HasSchema, HasStore, Store},
};

#[derive(Clone)]
pub struct App {
schema: Schema<QueryRoot, MutationRoot, EmptySubscription>,
store: InMemoryStore,
store: Arc<InMemoryStore>,
}

impl App {
pub fn example() -> Self {
let schema = Schema::new(QueryRoot, MutationRoot, EmptySubscription);
Self {
schema,
store: InMemoryStore::example(),
store: Arc::new(InMemoryStore::example()),
}
}
}
Expand All @@ -30,8 +32,7 @@ impl HasSchema for App {
}

impl HasStore for App {
type Store = InMemoryStore;
fn store(&self) -> Self::Store {
fn store(&self) -> Arc<dyn Store + Send + Sync> {
self.store.clone()
}
}
6 changes: 4 additions & 2 deletions rust/crates/web/src/handler/graphql.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use async_graphql::http::GraphiQLSource;
use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
use axum::{
Expand Down Expand Up @@ -27,7 +29,7 @@ async fn handler<T: HasSchema + HasStore>(
}
None => None,
},
state: Box::new(store),
store,
});
Ok(GraphQLResponse::from(schema.execute(request).await))
}
Expand All @@ -38,7 +40,7 @@ async fn graphiql() -> impl IntoResponse {

pub struct Data {
pub bearer: Option<Bearer>,
pub state: Box<dyn Store + Send + Sync + 'static>,
pub store: Arc<dyn Store + Send + Sync>,
}

pub fn route<T: Clone + HasSchema + HasStore + Send + Sync + 'static>() -> Router<T> {
Expand Down
8 changes: 4 additions & 4 deletions rust/crates/web/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub struct QueryRoot;
#[async_graphql::Object]
impl QueryRoot {
async fn bearer<'a>(&self, context: &Context<'a>) -> &'a str {
let bearer = context.data_unchecked::<Bearer>();
bearer.token()
let data = context.data_unchecked::<Data>();
data.bearer.as_ref().unwrap().token()
}

async fn hello(&self) -> &'static str {
Expand All @@ -30,7 +30,7 @@ impl QueryRoot {
&self,
context: &Context<'a>,
) -> async_graphql::Result<Vec<CheckList>> {
let store = &context.data_unchecked::<Data>().state;
let store = &context.data_unchecked::<Data>().store;
Ok(store
.find_all_check_lists()
.await?
Expand All @@ -40,7 +40,7 @@ impl QueryRoot {
}

async fn items<'a>(&self, ctx: &Context<'a>) -> async_graphql::Result<Vec<Item>> {
let store = &ctx.data_unchecked::<Data>().state;
let store = &ctx.data_unchecked::<Data>().store;
Ok(store
.find_all_items()
.await?
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/web/src/query/check_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl CheckList {
}

async fn checked_items(&self, context: &Context<'_>) -> async_graphql::Result<Vec<Item>> {
let store = &context.data_unchecked::<Data>().state;
let store = &context.data_unchecked::<Data>().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
2 changes: 1 addition & 1 deletion rust/crates/web/src/query/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Item {
&self,
context: &Context<'_>,
) -> async_graphql::Result<Vec<CheckList>> {
let store = &context.data_unchecked::<Data>().state;
let store = &context.data_unchecked::<Data>().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
5 changes: 3 additions & 2 deletions rust/crates/web/src/use_case.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use async_graphql::{EmptySubscription, Schema};
use axum::async_trait;

Expand Down Expand Up @@ -26,6 +28,5 @@ pub trait HasSchema {
}

pub trait HasStore {
type Store: Store + Send + Sync + 'static;
fn store(&self) -> Self::Store;
fn store(&self) -> Arc<dyn Store + Send + Sync>;
}

0 comments on commit 95c3945

Please sign in to comment.