Skip to main content

tako_rs_core/
graphiql.rs

1//! `GraphiQL` HTML responder and helper for Tako.
2//!
3//! Enable with the `graphiql` feature. This module provides a `graphiql()` function that
4//! returns an HTML page rendering the `GraphiQL` UI, wired to your `GraphQL` and WS endpoints.
5#![cfg(feature = "graphiql")]
6#![cfg_attr(docsrs, doc(cfg(feature = "graphiql")))]
7
8use http::HeaderValue;
9use http::header;
10
11use crate::body::TakoBody;
12use crate::responder::Responder;
13use crate::types::Response;
14
15/// Response wrapper for `GraphiQL` HTML.
16pub struct GraphiQL(pub(crate) String);
17
18impl Responder for GraphiQL {
19  fn into_response(self) -> Response {
20    let mut res = Response::new(TakoBody::from(self.0));
21    res.headers_mut().insert(
22      header::CONTENT_TYPE,
23      HeaderValue::from_static("text/html; charset=utf-8"),
24    );
25    res
26  }
27}
28
29/// Build a `GraphiQL` HTML response.
30///
31/// - `endpoint`: HTTP endpoint for `GraphQL` queries/mutations (e.g., "/graphql")
32/// - `subscription_endpoint`: optional WS URL for subscriptions (e.g., "<ws://localhost:8080/ws>")
33pub fn graphiql(endpoint: &str, subscription_endpoint: Option<&str>) -> GraphiQL {
34  let mut builder = async_graphql::http::GraphiQLSource::build().endpoint(endpoint);
35  if let Some(ws) = subscription_endpoint {
36    builder = builder.subscription_endpoint(ws);
37  }
38  GraphiQL(builder.finish())
39}