implement dynamic title for graphql playground
This commit is contained in:
parent
9586a7200b
commit
128a3de7c5
|
@ -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.
|
||||
///
|
||||
/// ```
|
||||
|
|
Loading…
Reference in New Issue