Skip to content

Commit

Permalink
Add GraphQLSchema struct
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Nov 20, 2023
1 parent ef8cd6f commit 3a318a7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
14 changes: 5 additions & 9 deletions rust/crates/web/src/app.rs
@@ -1,33 +1,29 @@
use std::sync::Arc;

use async_graphql::{EmptySubscription, Schema};

use crate::{
handler::graphql::mutation::MutationRoot,
handler::{graphql::query::QueryRoot, HasGraphQLSchema},
handler::{graphql::GraphQLSchema, HasGraphQLSchema},
infra::store::InMemoryStore,
use_case::{HasStore, Store},
};

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

impl App {
pub fn example() -> Self {
let schema = Schema::new(QueryRoot, MutationRoot, EmptySubscription);
Self {
schema,
graphql_schema: GraphQLSchema::new(),
store: Arc::new(InMemoryStore::example()),
}
}
}

impl HasGraphQLSchema for App {
fn schema(&self) -> &Schema<QueryRoot, MutationRoot, EmptySubscription> {
&self.schema
fn graphql_schema(&self) -> &GraphQLSchema {
&self.graphql_schema
}
}

Expand Down
28 changes: 16 additions & 12 deletions rust/crates/web/src/handler/graphql.rs
@@ -1,5 +1,5 @@
pub mod mutation;
pub mod query;
mod mutation;
mod query;

use std::sync::Arc;

Expand All @@ -18,23 +18,27 @@ use hyper::StatusCode;

use crate::use_case::{HasStore, Store};

use self::{mutation::MutationRoot, query::QueryRoot};

#[derive(Clone)]
pub struct GraphQLSchema(Schema<QueryRoot, MutationRoot, EmptySubscription>);

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

pub trait HasGraphQLSchema {
// TODO: query, mutation, subscription
fn schema(
&self,
) -> &Schema<
crate::handler::graphql::query::QueryRoot,
crate::handler::graphql::mutation::MutationRoot,
EmptySubscription,
>;
fn graphql_schema(&self) -> &GraphQLSchema;
}

async fn handler<T: HasGraphQLSchema + HasStore>(
State(state): State<T>,
header_map: HeaderMap,
request: GraphQLRequest,
) -> Result<GraphQLResponse, StatusCode> {
let schema = state.schema();
let schema = state.graphql_schema();
let store = state.store();
let request = request.into_inner().data(Data {
bearer: match header_map.get(axum::http::header::AUTHORIZATION) {
Expand All @@ -45,7 +49,7 @@ async fn handler<T: HasGraphQLSchema + HasStore>(
},
store,
});
Ok(GraphQLResponse::from(schema.execute(request).await))
Ok(GraphQLResponse::from(schema.0.execute(request).await))
}

async fn graphiql() -> impl IntoResponse {
Expand Down

0 comments on commit 3a318a7

Please sign in to comment.