Skip to content

Commit c22e9fc

Browse files
Forward playground settings as props to react component
1 parent f96ffdd commit c22e9fc

File tree

2 files changed

+75
-11
lines changed

2 files changed

+75
-11
lines changed

graphcoolPlayground.go

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,83 @@ import (
66
"net/http"
77
)
88

9+
type EditorCursorShape string
10+
11+
const (
12+
Line EditorCursorShape = "line"
13+
Block EditorCursorShape = "block"
14+
Underline EditorCursorShape = "underline"
15+
)
16+
17+
type EditorTheme string
18+
19+
const (
20+
Dark EditorTheme = "dark"
21+
Light EditorTheme = "light"
22+
)
23+
24+
type RequestCredentials string
25+
26+
const (
27+
Omit RequestCredentials = "omit"
28+
Include RequestCredentials = "include"
29+
SameOrigin RequestCredentials = "same-origin"
30+
)
31+
32+
type PlaygroundSettings struct {
33+
EditorCursorShape EditorCursorShape `json:"editor.cursorShape,omitempty"`
34+
EditorFontFamily string `json:"editor.fontFamily,omitempty"`
35+
EditorFontSize float64 `json:"editor.fontSize,omitempty"`
36+
EditorReuseHeaders bool `json:"editor.reuseHeaders,omitempty"`
37+
EditorTheme EditorTheme `json:"editor.theme,omitempty"`
38+
GeneralBetaUpdates bool `json:"general.betaUpdates,omitempty"`
39+
PrettierPrintWidth float64 `json:"prettier.printWidth,omitempty"`
40+
PrettierTabWidth float64 `json:"prettier.tabWidth,omitempty"`
41+
PrettierUseTabs bool `json:"prettier.useTabs,omitempty"`
42+
RequestCredentials RequestCredentials `json:"request.credentials,omitempty"`
43+
RequestGlobalHeaders map[string]string `json:"request.globalHeaders,omitempty"`
44+
SchemaPollingEnable bool `json:"schema.polling.enable,omitempty"`
45+
SchemaPollingEndpointFilter string `json:"schema.polling.endpointFilter,omitempty"`
46+
SchemaPollingInterval float64 `json:"schema.polling.interval,omitempty"`
47+
SchemaDisableComments bool `json:"schema.disableComments,omitempty"`
48+
TracingHideTracingResponse bool `json:"tracing.hideTracingResponse,omitempty"`
49+
TracingTracingSupported bool `json:"tracing.tracingSupported,omitempty"`
50+
}
51+
952
type playgroundData struct {
1053
PlaygroundVersion string
1154
Endpoint string
1255
SubscriptionEndpoint string
1356
SetTitle bool
57+
Settings *PlaygroundSettings
1458
}
1559

1660
// renderPlayground renders the Playground GUI
17-
func renderPlayground(w http.ResponseWriter, r *http.Request) {
61+
func (h *Handler) renderPlayground(w http.ResponseWriter, r *http.Request) {
1862
t := template.New("Playground")
1963
t, err := t.Parse(graphcoolPlaygroundTemplate)
2064
if err != nil {
2165
http.Error(w, err.Error(), http.StatusInternalServerError)
2266
return
2367
}
2468

69+
var endpoint string
70+
if h.playground.Endpoint != "" {
71+
// in case the endpoint was explicitly set in the configuration use it here
72+
endpoint = h.playground.Endpoint
73+
} else {
74+
// in case no endpoint was specified assume the graphql api is served under the request's url
75+
endpoint = r.URL.Path
76+
}
77+
2578
d := playgroundData{
2679
PlaygroundVersion: graphcoolPlaygroundVersion,
27-
Endpoint: r.URL.Path,
80+
Endpoint: endpoint,
2881
SubscriptionEndpoint: fmt.Sprintf("ws://%v/subscriptions", r.Host),
2982
SetTitle: true,
83+
Settings: h.playground.Settings,
3084
}
85+
3186
err = t.ExecuteTemplate(w, "index", d)
3287
if err != nil {
3388
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -99,7 +154,8 @@ add "&raw" to the end of the URL within a browser.
99154
// options as 'endpoint' belong here
100155
endpoint: {{ .Endpoint }},
101156
subscriptionEndpoint: {{ .SubscriptionEndpoint }},
102-
setTitle: {{ .SetTitle }}
157+
setTitle: {{ .SetTitle }},
158+
settings: {{ .Settings }}
103159
})
104160
})</script>
105161
</body>

handler.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ const (
2222

2323
type ResultCallbackFn func(ctx context.Context, params *graphql.Params, result *graphql.Result, responseBody []byte)
2424

25+
type PlaygroundConfig struct {
26+
Enabled bool
27+
Endpoint string
28+
Settings *PlaygroundSettings
29+
}
30+
2531
type Handler struct {
2632
Schema *graphql.Schema
2733
pretty bool
2834
graphiql bool
29-
playground bool
35+
playground *PlaygroundConfig
3036
rootObjectFn RootObjectFn
3137
resultCallbackFn ResultCallbackFn
3238
formatErrorFn func(err error) gqlerrors.FormattedError
@@ -158,11 +164,11 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
158164
}
159165
}
160166

161-
if h.playground {
167+
if h.playground != nil && h.playground.Enabled {
162168
acceptHeader := r.Header.Get("Accept")
163169
_, raw := r.URL.Query()["raw"]
164170
if !raw && !strings.Contains(acceptHeader, "application/json") && strings.Contains(acceptHeader, "text/html") {
165-
renderPlayground(w, r)
171+
h.renderPlayground(w, r)
166172
return
167173
}
168174
}
@@ -200,18 +206,20 @@ type Config struct {
200206
Schema *graphql.Schema
201207
Pretty bool
202208
GraphiQL bool
203-
Playground bool
209+
Playground *PlaygroundConfig
204210
RootObjectFn RootObjectFn
205211
ResultCallbackFn ResultCallbackFn
206212
FormatErrorFn func(err error) gqlerrors.FormattedError
207213
}
208214

209215
func NewConfig() *Config {
210216
return &Config{
211-
Schema: nil,
212-
Pretty: true,
213-
GraphiQL: true,
214-
Playground: false,
217+
Schema: nil,
218+
Pretty: true,
219+
GraphiQL: true,
220+
Playground: &PlaygroundConfig{
221+
Enabled: false,
222+
},
215223
}
216224
}
217225

0 commit comments

Comments
 (0)