implement dynamic title for graphql playground

This commit is contained in:
Marie Ramlow 2022-09-30 14:20:04 +00:00 committed by GitHub
parent 9586a7200b
commit 128a3de7c5
1 changed files with 12 additions and 1 deletions

View File

@ -14,6 +14,7 @@ use crate::Value;
/// playground_source(GraphQLPlaygroundConfig::new("http://localhost:8000")); /// playground_source(GraphQLPlaygroundConfig::new("http://localhost:8000"));
/// ``` /// ```
pub fn playground_source(config: GraphQLPlaygroundConfig) -> String { pub fn playground_source(config: GraphQLPlaygroundConfig) -> String {
let title = config.title.unwrap_or("GraphQL Playground");
r##" r##"
<!DOCTYPE html> <!DOCTYPE html>
@ -22,7 +23,7 @@ pub fn playground_source(config: GraphQLPlaygroundConfig) -> String {
<head> <head>
<meta charset=utf-8 /> <meta charset=utf-8 />
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui"> <meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
<title>GraphQL Playground</title> <title>%GRAPHQL_PLAYGROUND_TITLE%</title>
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/graphql-playground-react/build/static/css/index.css" /> <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/graphql-playground-react/build/static/css/index.css" />
<link rel="shortcut icon" href="//cdn.jsdelivr.net/npm/graphql-playground-react/build/favicon.png" /> <link rel="shortcut icon" href="//cdn.jsdelivr.net/npm/graphql-playground-react/build/favicon.png" />
<script src="//cdn.jsdelivr.net/npm/graphql-playground-react/build/static/js/middleware.js"></script> <script src="//cdn.jsdelivr.net/npm/graphql-playground-react/build/static/js/middleware.js"></script>
@ -557,6 +558,7 @@ pub fn playground_source(config: GraphQLPlaygroundConfig) -> String {
Ok(str) => str, Ok(str) => str,
Err(_) => "{}".to_string() Err(_) => "{}".to_string()
}) })
.replace("%GRAPHQL_PLAYGROUND_TITLE%", title)
} }
/// Config for GraphQL Playground /// Config for GraphQL Playground
@ -567,6 +569,7 @@ pub struct GraphQLPlaygroundConfig<'a> {
subscription_endpoint: Option<&'a str>, subscription_endpoint: Option<&'a str>,
headers: Option<HashMap<&'a str, &'a str>>, headers: Option<HashMap<&'a str, &'a str>>,
settings: Option<HashMap<&'a str, Value>>, settings: Option<HashMap<&'a str, Value>>,
title: Option<&'a str>,
} }
impl<'a> GraphQLPlaygroundConfig<'a> { impl<'a> GraphQLPlaygroundConfig<'a> {
@ -577,6 +580,7 @@ impl<'a> GraphQLPlaygroundConfig<'a> {
subscription_endpoint: None, subscription_endpoint: None,
headers: Default::default(), headers: Default::default(),
settings: Default::default(), settings: Default::default(),
title: Default::default(),
} }
} }
@ -600,6 +604,13 @@ impl<'a> GraphQLPlaygroundConfig<'a> {
self self
} }
/// Set the html document title.
#[must_use]
pub fn title(mut self, title: &'a str) -> Self {
self.title = Some(title);
self
}
/// Set Playground setting for per query. /// Set Playground setting for per query.
/// ///
/// ``` /// ```