Add the ability to specify full config for GraphQLPlayground (#155)
* add the ability to specify full config for GraphQLPlayground
This commit is contained in:
parent
ad4d3fc732
commit
3889f219cd
@ -10,7 +10,7 @@ use itertools::Itertools;
|
|||||||
|
|
||||||
pub use graphiql_source::graphiql_source;
|
pub use graphiql_source::graphiql_source;
|
||||||
pub use multipart_stream::multipart_stream;
|
pub use multipart_stream::multipart_stream;
|
||||||
pub use playground_source::playground_source;
|
pub use playground_source::{playground_source, GraphQLPlaygroundConfig};
|
||||||
pub use stream_body::StreamBody;
|
pub use stream_body::StreamBody;
|
||||||
|
|
||||||
use crate::query::{IntoQueryBuilder, IntoQueryBuilderOpts};
|
use crate::query::{IntoQueryBuilder, IntoQueryBuilderOpts};
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
|
use serde::Serialize;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
/// Generate the page for GraphQL Playground
|
/// Generate the page for GraphQL Playground
|
||||||
pub fn playground_source(
|
pub fn playground_source<T: Serialize>(config: &T) -> String {
|
||||||
graphql_endpoint_url: &str,
|
|
||||||
subscription_endpoint: Option<&str>,
|
|
||||||
) -> String {
|
|
||||||
r##"
|
r##"
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
|
|
||||||
@ -537,14 +537,21 @@ pub fn playground_source(
|
|||||||
const root = document.getElementById('root');
|
const root = document.getElementById('root');
|
||||||
root.classList.add('playgroundIn');
|
root.classList.add('playgroundIn');
|
||||||
|
|
||||||
GraphQLPlayground.init(root, { endpoint: GRAPHQL_URL, subscriptionEndpoint: GRAPHQL_SUBSCRIPTION_URL })
|
GraphQLPlayground.init(root, GRAPHQL_PLAYGROUND_CONFIG)
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
"##.replace("GRAPHQL_URL", &format!("'{}'", graphql_endpoint_url))
|
"##.replace("GRAPHQL_PLAYGROUND_CONFIG", &match serde_json::to_string(config) {
|
||||||
.replace("GRAPHQL_SUBSCRIPTION_URL", &match subscription_endpoint {
|
Ok(str) => str,
|
||||||
Some(url) => format!("'{}'", url),
|
_ => "{}".to_string()
|
||||||
None => "null".to_string()
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct GraphQLPlaygroundConfig {
|
||||||
|
pub endpoint: String,
|
||||||
|
pub subscription_endpoint: Option<String>,
|
||||||
|
pub headers: Option<HashMap<String, String>>,
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user