Skip to content

Commit 32bfef7

Browse files
AttilaTheFunchris-ramon
authored andcommitted
Add ability to configure playground endpoint + subscription endpoint
1 parent f96ffdd commit 32bfef7

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

graphcoolPlayground.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"fmt"
54
"html/template"
65
"net/http"
76
)
@@ -14,7 +13,7 @@ type playgroundData struct {
1413
}
1514

1615
// renderPlayground renders the Playground GUI
17-
func renderPlayground(w http.ResponseWriter, r *http.Request) {
16+
func renderPlayground(w http.ResponseWriter, r *http.Request, endpoint string, subscriptionEndpoint string) {
1817
t := template.New("Playground")
1918
t, err := t.Parse(graphcoolPlaygroundTemplate)
2019
if err != nil {
@@ -24,16 +23,14 @@ func renderPlayground(w http.ResponseWriter, r *http.Request) {
2423

2524
d := playgroundData{
2625
PlaygroundVersion: graphcoolPlaygroundVersion,
27-
Endpoint: r.URL.Path,
28-
SubscriptionEndpoint: fmt.Sprintf("ws://%v/subscriptions", r.Host),
26+
Endpoint: endpoint,
27+
SubscriptionEndpoint: subscriptionEndpoint,
2928
SetTitle: true,
3029
}
3130
err = t.ExecuteTemplate(w, "index", d)
3231
if err != nil {
3332
http.Error(w, err.Error(), http.StatusInternalServerError)
3433
}
35-
36-
return
3734
}
3835

3936
const graphcoolPlaygroundVersion = "1.5.2"

handler.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handler
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"io/ioutil"
67
"net/http"
78
"net/url"
@@ -27,6 +28,7 @@ type Handler struct {
2728
pretty bool
2829
graphiql bool
2930
playground bool
31+
playgroundConfig *PlaygroundConfig
3032
rootObjectFn RootObjectFn
3133
resultCallbackFn ResultCallbackFn
3234
formatErrorFn func(err error) gqlerrors.FormattedError
@@ -162,7 +164,15 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
162164
acceptHeader := r.Header.Get("Accept")
163165
_, raw := r.URL.Query()["raw"]
164166
if !raw && !strings.Contains(acceptHeader, "application/json") && strings.Contains(acceptHeader, "text/html") {
165-
renderPlayground(w, r)
167+
168+
endpoint := r.URL.Path
169+
subscriptionEndpoint := fmt.Sprintf("ws://%v/subscriptions", r.Host)
170+
if h.playgroundConfig != nil {
171+
endpoint = h.playgroundConfig.Endpoint
172+
subscriptionEndpoint = h.playgroundConfig.SubscriptionEndpoint
173+
}
174+
175+
renderPlayground(w, r, endpoint, subscriptionEndpoint)
166176
return
167177
}
168178
}
@@ -196,22 +206,29 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
196206
// RootObjectFn allows a user to generate a RootObject per request
197207
type RootObjectFn func(ctx context.Context, r *http.Request) map[string]interface{}
198208

209+
type PlaygroundConfig struct {
210+
Endpoint string
211+
SubscriptionEndpoint string
212+
}
213+
199214
type Config struct {
200215
Schema *graphql.Schema
201216
Pretty bool
202217
GraphiQL bool
203218
Playground bool
219+
PlaygroundConfig *PlaygroundConfig
204220
RootObjectFn RootObjectFn
205221
ResultCallbackFn ResultCallbackFn
206222
FormatErrorFn func(err error) gqlerrors.FormattedError
207223
}
208224

209225
func NewConfig() *Config {
210226
return &Config{
211-
Schema: nil,
212-
Pretty: true,
213-
GraphiQL: true,
214-
Playground: false,
227+
Schema: nil,
228+
Pretty: true,
229+
GraphiQL: true,
230+
Playground: false,
231+
PlaygroundConfig: nil,
215232
}
216233
}
217234

@@ -229,6 +246,7 @@ func New(p *Config) *Handler {
229246
pretty: p.Pretty,
230247
graphiql: p.GraphiQL,
231248
playground: p.Playground,
249+
playgroundConfig: p.PlaygroundConfig,
232250
rootObjectFn: p.RootObjectFn,
233251
resultCallbackFn: p.ResultCallbackFn,
234252
formatErrorFn: p.FormatErrorFn,

0 commit comments

Comments
 (0)