@@ -1,21 +1,48 @@
|
||||
use super::auth::UserFromSession;
|
||||
use crate::{router::AppState};
|
||||
|
||||
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
|
||||
use async_graphql::{EmptySubscription, Schema};
|
||||
use axum::{routing::get, Extension, Router};
|
||||
use como_gql::{
|
||||
graphql::{MutationRoot, QueryRoot},
|
||||
graphql_handler, graphql_playground,
|
||||
use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
|
||||
use axum::response::Html;
|
||||
use axum::{
|
||||
http::{StatusCode},
|
||||
response::{IntoResponse},
|
||||
routing::get,
|
||||
Extension, Router,
|
||||
};
|
||||
|
||||
|
||||
use como_gql::graphql::{ComoSchema, MutationRoot, QueryRoot};
|
||||
use como_infrastructure::register::ServiceRegister;
|
||||
use tower::ServiceBuilder;
|
||||
|
||||
pub struct GraphQLController;
|
||||
|
||||
impl GraphQLController {
|
||||
pub fn new_router(service_register: ServiceRegister) -> Router {
|
||||
pub fn new_router(service_register: ServiceRegister, state: AppState) -> Router {
|
||||
let schema = Schema::build(QueryRoot, MutationRoot, EmptySubscription)
|
||||
.data(service_register)
|
||||
.finish();
|
||||
|
||||
Router::new()
|
||||
.route("/", get(graphql_playground).post(graphql_handler))
|
||||
.layer(Extension(schema))
|
||||
.layer(ServiceBuilder::new().layer(Extension(schema)))
|
||||
.with_state(state)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn graphql_handler(
|
||||
user: UserFromSession,
|
||||
schema: Extension<ComoSchema>,
|
||||
req: GraphQLRequest,
|
||||
) -> Result<GraphQLResponse, StatusCode> {
|
||||
let req = req.into_inner();
|
||||
let req = req.data(user);
|
||||
|
||||
Ok(schema.execute(req).await.into())
|
||||
}
|
||||
|
||||
pub async fn graphql_playground() -> impl IntoResponse {
|
||||
Html(playground_source(GraphQLPlaygroundConfig::new("/graphql")))
|
||||
}
|
||||
|
Reference in New Issue
Block a user