Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow GraphiQL headers to be set when creating the playground handler #2740

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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