forked from thani-sh/go-graphiql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
55 lines (52 loc) · 1.92 KB
/
server.go
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
package graphiql
import "net/http"
// Content ...
var Content = []byte(`
<!DOCTYPE html>
<head>
<style>body {height: 100vh; margin: 0; width: 100%; overflow: hidden;}</style>
<link rel="stylesheet" href="//cdn.jsdelivr.net/sweetalert/1.1.3/sweetalert.css" />
<link rel="stylesheet" href="//cdn.jsdelivr.net/graphiql/0.6.3/graphiql.css" />
<script src="//cdn.jsdelivr.net/sweetalert/1.1.3/sweetalert.min.js"></script>
<script src="//cdn.jsdelivr.net/fetch/0.9.0/fetch.min.js"></script>
<script src="//cdn.jsdelivr.net/react/0.14.7/react.min.js"></script>
<script src="//cdn.jsdelivr.net/react/0.14.7/react-dom.min.js"></script>
<script src="//cdn.jsdelivr.net/graphiql/0.6.3/graphiql.min.js"></script>
<script>
(function () {
var PROMPT_OPTIONS = {
title: "GraphQL Endpoint",
text: "Please give the GraphQL HTTP Endpoint",
type: "input",
showCancelButton: false,
inputPlaceholder: window.location.origin + '/graphql',
};
document.addEventListener('DOMContentLoaded', function () {
swal(PROMPT_OPTIONS, function(endpoint){
if (!endpoint) {
endpoint = window.location.origin + '/graphql';
}
function fetcher(params) {
var options = {
method: 'post',
headers: {'Accept': 'application/json', 'Content-Type': 'application/json'},
body: JSON.stringify(params),
credentials: 'include',
};
return fetch(endpoint, options)
.then(function (res) { return res.json() });
}
var body = React.createElement(GraphiQL, {fetcher: fetcher, query: '', variables: ''});
ReactDOM.render(body, document.body);
});
});
}());
</script>
</head>
<body>
</body>
`)
// ServeGraphiQL is a handler function for HTTP servers
func ServeGraphiQL(res http.ResponseWriter, req *http.Request) {
res.Write(Content)
}