From 128a3de7c5e91670d409462b62c778df329ed627 Mon Sep 17 00:00:00 2001 From: Marie Ramlow Date: Fri, 30 Sep 2022 14:20:04 +0000 Subject: [PATCH] implement dynamic title for graphql playground --- src/http/playground_source.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/http/playground_source.rs b/src/http/playground_source.rs index 59aa2b31..7223c5ff 100644 --- a/src/http/playground_source.rs +++ b/src/http/playground_source.rs @@ -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##" @@ -22,7 +23,7 @@ pub fn playground_source(config: GraphQLPlaygroundConfig) -> String { - GraphQL Playground + %GRAPHQL_PLAYGROUND_TITLE% @@ -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>, settings: Option>, + 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. /// /// ```