forked from graphql-go/handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphiql.go
More file actions
254 lines (227 loc) · 7.37 KB
/
graphiql.go
File metadata and controls
254 lines (227 loc) · 7.37 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package handler
import (
"encoding/json"
"html/template"
"net/http"
"strings"
"github.com/graphql-go/graphql"
)
// page is the page data structure of the rendered GraphiQL page
type graphiqlPage struct {
GraphiqlVersion string
SubscriptionTransportVersion string
QueryString string
ResultString string
VariablesString string
OperationName string
Endpoint template.URL
SubscriptionEndpoint template.URL
UsingHTTP bool
UsingWS bool
}
// renderGraphiQL renders the GraphiQL GUI
func renderGraphiQL(w http.ResponseWriter, params graphql.Params, handler Handler) {
t := template.New("GraphiQL")
t, err := t.Parse(graphiqlTemplate)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Create variables string
vars, err := json.MarshalIndent(params.VariableValues, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
varsString := string(vars)
if varsString == "null" {
varsString = ""
}
// Create result string
var resString string
if params.RequestString == "" {
resString = ""
} else {
result, err := json.MarshalIndent(graphql.Do(params), "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resString = string(result)
}
isEndpointUsingWS := strings.HasPrefix(handler.Endpoint, "ws://")
UsingHTTP := !isEndpointUsingWS
UsingWS := isEndpointUsingWS || handler.SubscriptionsEndpoint != ""
SubscriptionEndpoint := ""
if UsingWS {
if isEndpointUsingWS {
SubscriptionEndpoint = handler.Endpoint
} else {
SubscriptionEndpoint = handler.SubscriptionsEndpoint
}
}
p := graphiqlPage{
GraphiqlVersion: graphiqlVersion,
SubscriptionTransportVersion: subscriptionTransportVersion,
QueryString: params.RequestString,
ResultString: resString,
VariablesString: varsString,
OperationName: params.OperationName,
Endpoint: template.URL(handler.Endpoint),
SubscriptionEndpoint: template.URL(SubscriptionEndpoint),
UsingHTTP: UsingHTTP,
UsingWS: UsingWS,
}
err = t.ExecuteTemplate(w, "index", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
// graphiqlVersion is the current version of GraphiQL
const graphiqlVersion = "0.11.10"
// subscriptionTransportVersion is the current version of the subscription transport of GraphiQL
const subscriptionTransportVersion = "0.8.2"
// tmpl is the page template to render GraphiQL
const graphiqlTemplate = `
{{ define "index" }}
<!--
The request to this GraphQL server provided the header "Accept: text/html"
and as a result has been presented GraphiQL - an in-browser IDE for
exploring GraphQL.
If you wish to receive JSON, provide the header "Accept: application/json" or
add "&raw" to the end of the URL within a browser.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>GraphiQL</title>
<meta name="robots" content="noindex" />
<style>
html, body {
height: 100%;
margin: 0;
overflow: hidden;
width: 100%;
}
</style>
<link href="//cdn.jsdelivr.net/npm/graphiql@{{ .GraphiqlVersion }}/graphiql.css" rel="stylesheet" />
<script src="//cdn.jsdelivr.net/react/15.4.2/react.min.js"></script>
<script src="//cdn.jsdelivr.net/react/15.4.2/react-dom.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/graphiql@{{ .GraphiqlVersion }}/graphiql.min.js"></script>
{{ if .UsingHTTP }}
<script src="//cdn.jsdelivr.net/fetch/2.0.1/fetch.min.js"></script>
{{ end }}
{{ if .UsingWS }}
<script src="//unpkg.com/subscriptions-transport-ws@{{ .SubscriptionTransportVersion }}/browser/client.js"></script>
{{ end }}
{{ if and .UsingWS .UsingHTTP }}
<script src="//unpkg.com/graphiql-subscriptions-fetcher@0.0.2/browser/client.js"></script>
{{ end }}
</head>
<body>
<script>
// Collect the URL parameters
var parameters = {};
window.location.search.substr(1).split('&').forEach(function (entry) {
var eq = entry.indexOf('=');
if (eq >= 0) {
parameters[decodeURIComponent(entry.slice(0, eq))] =
decodeURIComponent(entry.slice(eq + 1));
}
});
// Produce a Location query string from a parameter object.
function locationQuery(params, location) {
return (location ? location: '') + '?' + Object.keys(params).map(function (key) {
return encodeURIComponent(key) + '=' +
encodeURIComponent(params[key]);
}).join('&');
}
// Derive a fetch URL from the current URL, sans the GraphQL parameters.
var graphqlParamNames = {
query: true,
variables: true,
operationName: true
};
var otherParams = {};
for (var k in parameters) {
if (parameters.hasOwnProperty(k) && graphqlParamNames[k] !== true) {
otherParams[k] = parameters[k];
}
}
{{ if .UsingWS }}
var subscriptionsClient = new window.SubscriptionsTransportWs.SubscriptionClient({{ .SubscriptionEndpoint }}, {
reconnect: true
});
var graphQLWSFetcher = subscriptionsClient.request.bind(subscriptionsClient);
{{ end }}
{{ if .UsingHTTP }}
var fetchURL = locationQuery(otherParams, {{ .Endpoint }});
// Defines a GraphQL fetcher using the fetch API.
function graphQLHttpFetcher(graphQLParams) {
return fetch(fetchURL, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(graphQLParams),
credentials: 'include',
}).then(function (response) {
return response.text();
}).then(function (responseBody) {
try {
return JSON.parse(responseBody);
} catch (error) {
return responseBody;
}
});
}
{{ end }}
{{ if and .UsingWS .UsingHTTP }}
var fetcher = window.GraphiQLSubscriptionsFetcher.graphQLFetcher(subscriptionsClient, graphQLHttpFetcher);
{{ else }}
{{ if .UsingWS }}
var fetcher = graphQLWSFetcher;
{{ end }}
{{ if .UsingHTTP }}
var fetcher = graphQLHttpFetcher;
{{ end }}
{{ end }}
// When the query and variables string is edited, update the URL bar so
// that it can be easily shared.
function onEditQuery(newQuery) {
parameters.query = newQuery;
updateURL();
}
function onEditVariables(newVariables) {
parameters.variables = newVariables;
updateURL();
}
function onEditOperationName(newOperationName) {
parameters.operationName = newOperationName;
updateURL();
}
function updateURL() {
history.replaceState(null, null, locationQuery(parameters));
}
// Render <GraphiQL /> into the body.
ReactDOM.render(
React.createElement(GraphiQL, {
fetcher: fetcher,
onEditQuery: onEditQuery,
onEditVariables: onEditVariables,
onEditOperationName: onEditOperationName,
query: {{ .QueryString }},
response: {{ .ResultString }},
variables: {{ .VariablesString }},
operationName: {{ .OperationName }},
}),
document.body
);
</script>
</body>
</html>
{{ end }}
`