mina_node_native/http_server/routes/
graphql.rs1use std::sync::Arc;
10
11use axum::{
12 extract::State,
13 response::{Html, IntoResponse},
14 routing::{get, post},
15 Extension, Json, Router,
16};
17use juniper::{http::GraphQLBatchRequest, EmptySubscription, RootNode};
18
19use crate::{
20 graphql::{Context, Mutation, Query},
21 http_server::AppState,
22};
23
24type Schema = RootNode<Query, Mutation, EmptySubscription<Context>>;
25
26pub fn routes(router: Router<AppState>) -> Router<AppState> {
28 let schema = Arc::new(Schema::new(Query, Mutation, EmptySubscription::new()));
29
30 let router = router.route("/graphql", post(graphql_handler));
31
32 #[cfg(feature = "graphiql")]
33 let router = router.route("/graphiql", get(graphiql_handler));
34
35 router.layer(Extension(schema))
36}
37
38async fn graphql_handler(
40 State(state): State<AppState>,
41 Extension(schema): Extension<Arc<Schema>>,
42 Json(request): Json<GraphQLBatchRequest>,
43) -> impl IntoResponse {
44 let context = Context::new(state.rpc_sender().clone());
45 let response = request.execute(&*schema, &context).await;
46 Json(response)
47}
48
49#[cfg(feature = "graphiql")]
51async fn graphiql_handler() -> impl IntoResponse {
52 Html(juniper::http::graphiql::graphiql_source("/graphql", None))
53}