Skip to content

Commit

Permalink
Allow GraphiQL headers to be set when creating the playground handler
Browse files Browse the repository at this point in the history
Fixes #2739
  • Loading branch information
mihaip committed Jul 28, 2023
1 parent 0eb95dc commit dd53f26
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion graphql/playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ var page = template.Must(template.New("graphiql").Parse(`<!DOCTYPE html>
const wsProto = location.protocol == 'https:' ? 'wss:' : 'ws:';
const subscriptionUrl = wsProto + '//' + location.host + {{.endpoint}};
{{- end}}
{{- if .headers}}
const headers = {{.headers}};
{{- else}}
const headers = undefined;
{{- end}}
const fetcher = GraphiQL.createFetcher({ url, subscriptionUrl });
const fetcher = GraphiQL.createFetcher({ url, subscriptionUrl, headers });
ReactDOM.render(
React.createElement(GraphiQL, {
fetcher: fetcher,
Expand All @@ -75,11 +80,16 @@ var page = template.Must(template.New("graphiql").Parse(`<!DOCTYPE html>

// Handler responsible for setting up the playground
func Handler(title string, endpoint string) http.HandlerFunc {
return HandlerWithHeaders(title, endpoint, nil)
}

func HandlerWithHeaders(title string, endpoint string, headers map[string]string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html; charset=UTF-8")
err := page.Execute(w, map[string]interface{}{
"title": title,
"endpoint": endpoint,
"headers": headers,
"endpointIsAbsolute": endpointHasScheme(endpoint),
"subscriptionEndpoint": getSubscriptionEndpoint(endpoint),
"version": "3.0.1",
Expand Down

0 comments on commit dd53f26

Please sign in to comment.