use std::collections::HashMap;
use serde::Serialize;
// From async-graphql-2.5.7/src/http/playground_source.rs
/// Generate the page for GraphQL Playground
///
/// # Example
///
/// ```rust
/// use async_graphql::http::*;
///
/// playground_source(GraphQLPlaygroundConfig::new("http://localhost:8000"));
/// ```
#[allow(clippy::too_many_lines)]
#[must_use]
pub fn playground_source(config: GraphQLPlaygroundConfig) -> String {
r##"
GraphQL Playground
Loading
GraphQL Playground
"##.replace("GRAPHQL_PLAYGROUND_CONFIG", &match serde_json::to_string(&config) {
Ok(str) => str,
Err(_) => "{}".to_string()
})
}
/// Config for GraphQL Playground
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GraphQLPlaygroundConfig<'a> {
endpoint: &'a str,
subscription_endpoint: Option<&'a str>,
headers: Option>,
}
impl<'a> GraphQLPlaygroundConfig<'a> {
/// Create a config for GraphQL playground.
pub fn new(endpoint: &'a str) -> Self {
Self {
endpoint,
subscription_endpoint: None,
headers: Default::default(),
}
}
/// Set subscription endpoint, for example: `ws://localhost:8000`.
pub fn subscription_endpoint(mut self, endpoint: &'a str) -> Self {
self.subscription_endpoint = Some(endpoint);
self
}
/// Set HTTP header for per query.
pub fn with_header(mut self, name: &'a str, value: &'a str) -> Self {
if let Some(headers) = &mut self.headers {
headers.insert(name, value);
} else {
let mut headers = HashMap::new();
headers.insert(name, value);
self.headers = Some(headers);
}
self
}
}