forked from authgear/authgear-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphiql.go
More file actions
65 lines (61 loc) · 1.36 KB
/
graphiql.go
File metadata and controls
65 lines (61 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package graphqlutil
import (
htmltemplate "html/template"
"net/http"
)
var graphiqlTemplate = htmltemplate.Must(htmltemplate.New("graphiql").Parse(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ .Title }}</title>
<style>
body {
padding: 0;
margin: 0;
min-height: 100vh;
}
#root {
height: 100vh;
}
</style>
<script
crossorigin
src="https://unpkg.com/react@17.0.2/umd/react.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.production.min.js"
></script>
<link rel="stylesheet" href="https://unpkg.com/graphiql@2.0.13/graphiql.min.css" />
</head>
<body>
<div id="root">Loading...</div>
<script
crossorigin
src="https://unpkg.com/graphiql@2.0.13/graphiql.min.js"
></script>
<script>
ReactDOM.render(
React.createElement(GraphiQL, {
fetcher: GraphiQL.createFetcher({
url: "",
}),
query: (new URLSearchParams(window.location.search)).get("query") || "",
}),
document.getElementById("root"),
);
</script>
</body>
</html>
`))
type GraphiQL struct {
Title string
}
func (g *GraphiQL) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
err := graphiqlTemplate.Execute(w, g)
if err != nil {
panic(err)
}
}