@@ -2,6 +2,7 @@ package handler
22
33import (
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
197207type RootObjectFn func (ctx context.Context , r * http.Request ) map [string ]interface {}
198208
209+ type PlaygroundConfig struct {
210+ Endpoint string
211+ SubscriptionEndpoint string
212+ }
213+
199214type 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
209225func 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