Merge pull request #1099 from NyCodeGHG/feature/document-title

This commit is contained in:
Sunli 2022-09-30 23:02:21 +08:00 committed by GitHub
commit f1bac10120
2 changed files with 26 additions and 3 deletions

View File

@ -18,6 +18,7 @@ pub struct GraphiQLSource<'a> {
endpoint: &'a str,
subscription_endpoint: Option<&'a str>,
headers: Option<HashMap<&'a str, &'a str>>,
title: Option<&'a str>,
}
impl<'a> GraphiQLSource<'a> {
@ -53,6 +54,14 @@ impl<'a> GraphiQLSource<'a> {
}
}
/// Sets the html document title.
pub fn title(self, title: &'a str) -> GraphiQLSource<'a> {
GraphiQLSource {
title: Some(title),
..self
}
}
/// Returns a GraphiQL (v2) HTML page.
pub fn finish(self) -> String {
let graphiql_url = format!("'{}'", self.endpoint);
@ -64,6 +73,7 @@ impl<'a> GraphiQLSource<'a> {
Some(headers) => serde_json::to_string(&headers).unwrap(),
None => "undefined".into(),
};
let graphiql_title = self.title.unwrap_or("GraphiQL IDE");
r#"
<!DOCTYPE html>
@ -74,7 +84,7 @@ impl<'a> GraphiQLSource<'a> {
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="referrer" content="origin">
<title>GraphiQL IDE</title>
<title>%GRAPHIQL_TITLE%</title>
<style>
body {
@ -125,6 +135,7 @@ impl<'a> GraphiQLSource<'a> {
.replace("%GRAPHIQL_URL%", &graphiql_url)
.replace("%GRAPHIQL_SUBSCRIPTION_URL%", &graphiql_subscription_url)
.replace("%GRAPHIQL_HEADERS%", &graphiql_headers)
.replace("%GRAPHIQL_TITLE%", &graphiql_title)
}
}
@ -275,6 +286,7 @@ mod tests {
.endpoint("http://localhost:8000")
.subscription_endpoint("ws://localhost:8000/ws")
.header("Authorization", "Bearer <token>")
.title("Awesome GraphiQL IDE Test")
.finish();
assert_eq!(
@ -288,7 +300,7 @@ mod tests {
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="referrer" content="origin">
<title>GraphiQL IDE</title>
<title>Awesome GraphiQL IDE Test</title>
<style>
body {

View File

@ -14,6 +14,7 @@ use crate::Value;
/// playground_source(GraphQLPlaygroundConfig::new("http://localhost:8000"));
/// ```
pub fn playground_source(config: GraphQLPlaygroundConfig) -> String {
let title = config.title.unwrap_or("GraphQL Playground");
r##"
<!DOCTYPE html>
@ -22,7 +23,7 @@ pub fn playground_source(config: GraphQLPlaygroundConfig) -> String {
<head>
<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">
<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="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>
@ -557,6 +558,7 @@ pub fn playground_source(config: GraphQLPlaygroundConfig) -> String {
Ok(str) => str,
Err(_) => "{}".to_string()
})
.replace("%GRAPHQL_PLAYGROUND_TITLE%", title)
}
/// Config for GraphQL Playground
@ -567,6 +569,7 @@ pub struct GraphQLPlaygroundConfig<'a> {
subscription_endpoint: Option<&'a str>,
headers: Option<HashMap<&'a str, &'a str>>,
settings: Option<HashMap<&'a str, Value>>,
title: Option<&'a str>,
}
impl<'a> GraphQLPlaygroundConfig<'a> {
@ -577,6 +580,7 @@ impl<'a> GraphQLPlaygroundConfig<'a> {
subscription_endpoint: None,
headers: Default::default(),
settings: Default::default(),
title: Default::default(),
}
}
@ -600,6 +604,13 @@ impl<'a> GraphQLPlaygroundConfig<'a> {
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.
///
/// ```