Skip to content

Commit 86511f0

Browse files
committed
Expose minimal static playground
1 parent 3c047f3 commit 86511f0

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

cmd/server.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
const defaultGraphQLPath = "/graphql"
2121
const defaultGraphQLSchemaPath = "/graphql-schema"
2222
const defaultRESTPath = "/todo"
23+
const defaultGraphQLPlaygroundPath = "/graphql-playground"
2324

2425
// Environment variables prefixed with "ENDPOINT_" can override settings e.g. "ENDPOINT_HOSTS"
2526
const envVarPrefix = "endpoint"
@@ -35,7 +36,7 @@ var serverCmd = &cobra.Command{
3536
// TODO: Validate GraphQL/REST paths, should they be disjointed?
3637

3738
hosts := viper.GetStringSlice("hosts")
38-
if len(hosts) == 0 {
39+
if len(hosts) == 0 {
3940
return errors.New("hosts are required")
4041
}
4142

@@ -118,6 +119,7 @@ func Execute() {
118119
flags.Bool("start-graphql", true, "start the GraphQL endpoint")
119120
flags.String("graphql-path", defaultGraphQLPath, "path for the GraphQL endpoint")
120121
flags.String("graphql-schema-path", defaultGraphQLSchemaPath, "path for the GraphQL schema management")
122+
flags.String("graphql-playground-path", defaultGraphQLPlaygroundPath, "path for the GraphQL playground static file")
121123
flags.Int("graphql-port", 8080, "port for the GraphQL endpoint")
122124

123125
// REST specific flags
@@ -171,6 +173,7 @@ func addGraphQLRoutes(router *httprouter.Router, endpoint *endpoint.DataEndpoint
171173

172174
singleKeyspace := viper.GetString("keyspace")
173175
rootPath := viper.GetString("graphql-path")
176+
playgroundPath := viper.GetString("graphql-playground-path")
174177

175178
if singleKeyspace != "" {
176179
routes, err = endpoint.RoutesKeyspaceGraphQL(rootPath, singleKeyspace)
@@ -187,6 +190,8 @@ func addGraphQLRoutes(router *httprouter.Router, endpoint *endpoint.DataEndpoint
187190
router.Handler(route.Method, route.Pattern, route.Handler)
188191
}
189192

193+
router.GET(playgroundPath, graphql.GetPlaygroundHandle(rootPath, viper.GetInt("graphql-port")))
194+
190195
supportedOps := viper.GetStringSlice("operations")
191196
ops, err := config.Ops(supportedOps...)
192197
if err != nil {
@@ -234,6 +239,6 @@ func listenAndServe(handler http.Handler, port int, endpointNames string) {
234239
if err != nil {
235240
logger.Fatal("unable to start server",
236241
"port", port,
237-
"error", err, )
242+
"error", err)
238243
}
239244
}

graphql/playground.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package graphql
2+
3+
import (
4+
"fmt"
5+
"github.com/julienschmidt/httprouter"
6+
"net/http"
7+
)
8+
9+
func GetPlaygroundHandle(path string, port int) httprouter.Handle {
10+
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
11+
html := `
12+
<!DOCTYPE html>
13+
<html>
14+
15+
<head>
16+
<meta charset=utf-8/>
17+
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
18+
<title>GraphQL Playground</title>
19+
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/graphql-playground-react/build/static/css/index.css" />
20+
<link rel="shortcut icon" href="//cdn.jsdelivr.net/npm/graphql-playground-react/build/favicon.png" />
21+
<script src="//cdn.jsdelivr.net/npm/graphql-playground-react/build/static/js/middleware.js"></script>
22+
</head>
23+
24+
<body>
25+
<div id="root">
26+
<style>
27+
body {
28+
background-color: rgb(23, 42, 58);
29+
font-family: Open Sans, sans-serif;
30+
height: 90vh;
31+
}
32+
33+
#root {
34+
height: 100%%;
35+
width: 100%%;
36+
display: flex;
37+
align-items: center;
38+
justify-content: center;
39+
}
40+
41+
.loading {
42+
font-size: 32px;
43+
font-weight: 200;
44+
color: rgba(255, 255, 255, .6);
45+
margin-left: 20px;
46+
}
47+
48+
img {
49+
width: 78px;
50+
height: 78px;
51+
}
52+
53+
.title {
54+
font-weight: 400;
55+
}
56+
</style>
57+
<img src='//cdn.jsdelivr.net/npm/graphql-playground-react/build/logo.png' alt=''>
58+
<div class="loading"> Loading
59+
<span class="title">GraphQL Playground</span>
60+
</div>
61+
</div>
62+
<script>window.addEventListener('load', function (event) {
63+
GraphQLPlayground.init(document.getElementById('root'), {
64+
endpoint: '%s'
65+
})
66+
})</script>
67+
</body>
68+
69+
</html>
70+
`
71+
html = fmt.Sprintf(html, fmt.Sprintf("http://localhost:%d%s", port, path))
72+
fmt.Fprint(w, html)
73+
}
74+
}

0 commit comments

Comments
 (0)