Skip to content

Commit 6f955cf

Browse files
committed
Add error message when using GET for /graphql
Graphql expects a POST request so warn when client uses GET.
1 parent 04ebb66 commit 6f955cf

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/main.rs

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

33
use async_graphql::{EmptyMutation, EmptySubscription, Schema};
44
use axum::http::StatusCode;
5-
use axum::response::Html;
5+
use axum::response::{Html, IntoResponse};
66
use axum::routing::{get, post};
77
use axum::{Extension, Router};
88

@@ -56,7 +56,7 @@ async fn serve(config: GlazedConfig) -> Result<(), Box<dyn error::Error>> {
5656
.finish();
5757

5858
let app = Router::new()
59-
.route("/graphql", post(graphql_handler))
59+
.route("/graphql", post(graphql_handler).get(graphql_get_warning))
6060
.route("/graphiql", get(graphiql_handler))
6161
.fallback((
6262
StatusCode::NOT_FOUND,
@@ -72,6 +72,14 @@ async fn serve(config: GlazedConfig) -> Result<(), Box<dyn error::Error>> {
7272
.await?)
7373
}
7474

75+
async fn graphql_get_warning() -> impl IntoResponse {
76+
(
77+
StatusCode::METHOD_NOT_ALLOWED,
78+
[("Allow", "POST")],
79+
Html(include_str!("../static/get_graphql_warning.html")),
80+
)
81+
}
82+
7583
async fn signal_handler() {
7684
let mut term = signal(SignalKind::terminate()).expect("Failed to create SIGTERM listener");
7785
let mut int = signal(SignalKind::interrupt()).expect("Failed to create SIGINT listener");

static/get_graphql_warning.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Glazed</title>
5+
</head>
6+
<body>
7+
<p>Requests should be made as graphql queries</p>
8+
</body>
9+
</html>

0 commit comments

Comments
 (0)