@@ -2,6 +2,7 @@ package handler
2
2
3
3
import (
4
4
"encoding/json"
5
+ "fmt"
5
6
"io/ioutil"
6
7
"net/http"
7
8
"net/url"
@@ -27,6 +28,7 @@ type Handler struct {
27
28
pretty bool
28
29
graphiql bool
29
30
playground bool
31
+ playgroundConfig * PlaygroundConfig
30
32
rootObjectFn RootObjectFn
31
33
resultCallbackFn ResultCallbackFn
32
34
formatErrorFn func (err error ) gqlerrors.FormattedError
@@ -162,7 +164,15 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
162
164
acceptHeader := r .Header .Get ("Accept" )
163
165
_ , raw := r .URL .Query ()["raw" ]
164
166
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 )
166
176
return
167
177
}
168
178
}
@@ -196,22 +206,29 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
196
206
// RootObjectFn allows a user to generate a RootObject per request
197
207
type RootObjectFn func (ctx context.Context , r * http.Request ) map [string ]interface {}
198
208
209
+ type PlaygroundConfig struct {
210
+ Endpoint string
211
+ SubscriptionEndpoint string
212
+ }
213
+
199
214
type Config struct {
200
215
Schema * graphql.Schema
201
216
Pretty bool
202
217
GraphiQL bool
203
218
Playground bool
219
+ PlaygroundConfig * PlaygroundConfig
204
220
RootObjectFn RootObjectFn
205
221
ResultCallbackFn ResultCallbackFn
206
222
FormatErrorFn func (err error ) gqlerrors.FormattedError
207
223
}
208
224
209
225
func NewConfig () * Config {
210
226
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 ,
215
232
}
216
233
}
217
234
@@ -229,6 +246,7 @@ func New(p *Config) *Handler {
229
246
pretty : p .Pretty ,
230
247
graphiql : p .GraphiQL ,
231
248
playground : p .Playground ,
249
+ playgroundConfig : p .PlaygroundConfig ,
232
250
rootObjectFn : p .RootObjectFn ,
233
251
resultCallbackFn : p .ResultCallbackFn ,
234
252
formatErrorFn : p .FormatErrorFn ,
0 commit comments