Skip to content

Commit 3e5c616

Browse files
authored
Add graphiql handler (#11)
1 parent 2ab7f5f commit 3e5c616

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/handlers/graphql.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use async_graphql::http::GraphiQLSource;
12
use async_graphql::*;
23
use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
34
use axum::Extension;
5+
use axum::response::{Html, IntoResponse};
46

57
use crate::clients::Client;
68
use crate::schemas::TiledQuery;
@@ -14,6 +16,10 @@ pub async fn graphql_handler<T: Client + Send + Sync + 'static>(
1416
schema.execute(query).await.into()
1517
}
1618

19+
pub async fn graphiql_handler() -> impl IntoResponse {
20+
Html(GraphiQLSource::build().endpoint("/graphql").finish())
21+
}
22+
1723
#[cfg(test)]
1824
mod tests {
1925
use async_graphql::{EmptyMutation, EmptySubscription, Schema};

src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::error;
22

33
use async_graphql::{EmptyMutation, EmptySubscription, Schema};
4-
use axum::routing::post;
4+
use axum::routing::{get, post};
55
use axum::{Extension, Router};
66

77
mod cli;
@@ -14,7 +14,7 @@ use cli::{Cli, Commands};
1414

1515
use crate::clients::tiled_client::TiledClient;
1616
use crate::config::GlazedConfig;
17-
use crate::handlers::graphql::graphql_handler;
17+
use crate::handlers::graphql::{graphiql_handler, graphql_handler};
1818
use crate::schemas::TiledQuery;
1919

2020
#[tokio::main]
@@ -42,6 +42,7 @@ async fn serve(config: GlazedConfig) -> Result<(), Box<dyn error::Error>> {
4242

4343
let app = Router::new()
4444
.route("/graphql", post(graphql_handler::<TiledClient>))
45+
.route("/graphiql", get(graphiql_handler))
4546
.layer(Extension(schema));
4647

4748
let listener = tokio::net::TcpListener::bind(config.bind_address).await?;

0 commit comments

Comments
 (0)