Skip to content

Commit

Permalink
Change handler::graphql::Data struct
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Nov 16, 2023
1 parent f979b4c commit f27ea20
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
21 changes: 13 additions & 8 deletions rust/crates/web/src/handler/graphql.rs
Expand Up @@ -20,21 +20,26 @@ async fn handler<T: HasSchema + HasStore>(
) -> Result<GraphQLResponse, StatusCode> {
let schema = state.schema();
let store = state.store();
let request = request.into_inner().data(Data(Box::new(store)));
let request = if let Some(header_value) = header_map.get(axum::http::header::AUTHORIZATION) {
let bearer = Bearer::decode(header_value).ok_or(StatusCode::UNAUTHORIZED)?;
request.data(bearer)
} else {
request
};
let request = request.into_inner().data(Data {
bearer: match header_map.get(axum::http::header::AUTHORIZATION) {
Some(header_value) => {
Some(Bearer::decode(header_value).ok_or(StatusCode::UNAUTHORIZED)?)
}
None => None,
},
state: Box::new(store),
});
Ok(GraphQLResponse::from(schema.execute(request).await))
}

async fn graphiql() -> impl IntoResponse {
Html(GraphiQLSource::build().endpoint("/graphql").finish())
}

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

pub fn route<T: Clone + HasSchema + HasStore + Send + Sync + 'static>() -> Router<T> {
Router::new().route("/graphql", routing::get(graphiql).post(handler::<T>))
Expand Down
4 changes: 2 additions & 2 deletions rust/crates/web/src/query.rs
Expand Up @@ -30,7 +30,7 @@ impl QueryRoot {
&self,
context: &Context<'a>,
) -> async_graphql::Result<Vec<CheckList>> {
let store = &context.data_unchecked::<Data>().0;
let store = &context.data_unchecked::<Data>().state;
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>().0;
let store = &ctx.data_unchecked::<Data>().state;
Ok(store
.find_all_items()
.await?
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/web/src/query/check_list.rs
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>().0;
let store = &context.data_unchecked::<Data>().state;
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
Expand Up @@ -24,7 +24,7 @@ impl Item {
&self,
context: &Context<'_>,
) -> async_graphql::Result<Vec<CheckList>> {
let store = &context.data_unchecked::<Data>().0;
let store = &context.data_unchecked::<Data>().state;
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 f27ea20

Please sign in to comment.