@@ -4,18 +4,24 @@ import (
4
4
"encoding/json"
5
5
"html/template"
6
6
"net/http"
7
+ "strings"
7
8
8
9
"github.com/graphql-go/graphql"
9
10
)
10
11
11
12
// page is the page data structure of the rendered GraphiQL page
12
13
type graphiqlPage struct {
13
- GraphiqlVersion string
14
- QueryString string
15
- ResultString string
16
- VariablesString string
17
- OperationName string
18
- EndpointURL template.URL
14
+ GraphiqlVersion string
15
+ SubscriptionTransportVersion string
16
+ QueryString string
17
+ ResultString string
18
+ VariablesString string
19
+ OperationName string
20
+ EndpointURL template.URL
21
+ EndpointURLWS template.URL
22
+ SubscriptionsEndpoint template.URL
23
+ UsingHTTP bool
24
+ UsingWS bool
19
25
}
20
26
21
27
// renderGraphiQL renders the GraphiQL GUI
@@ -51,13 +57,30 @@ func renderGraphiQL(w http.ResponseWriter, params graphql.Params, handler Handle
51
57
resString = string (result )
52
58
}
53
59
60
+ endpointWS := strings .HasPrefix (handler .EndpointURL , "ws://" )
61
+ UsingHTTP := ! endpointWS
62
+ UsingWS := endpointWS || handler .SubscriptionsEndpoint != ""
63
+ EndpointURLWS := ""
64
+ if UsingWS {
65
+ if endpointWS {
66
+ EndpointURLWS = handler .EndpointURL
67
+ } else {
68
+ EndpointURLWS = handler .SubscriptionsEndpoint
69
+ }
70
+ }
71
+
54
72
p := graphiqlPage {
55
- GraphiqlVersion : graphiqlVersion ,
56
- QueryString : params .RequestString ,
57
- ResultString : resString ,
58
- VariablesString : varsString ,
59
- OperationName : params .OperationName ,
60
- EndpointURL : template .URL (handler .EndpointURL ),
73
+ GraphiqlVersion : graphiqlVersion ,
74
+ SubscriptionTransportVersion : subscriptionTransportVersion ,
75
+ QueryString : params .RequestString ,
76
+ ResultString : resString ,
77
+ VariablesString : varsString ,
78
+ OperationName : params .OperationName ,
79
+ EndpointURL : template .URL (handler .EndpointURL ),
80
+ EndpointURLWS : template .URL (EndpointURLWS ),
81
+ SubscriptionsEndpoint : template .URL (handler .SubscriptionsEndpoint ),
82
+ UsingHTTP : UsingHTTP ,
83
+ UsingWS : UsingWS ,
61
84
}
62
85
63
86
err = t .ExecuteTemplate (w , "index" , p )
@@ -70,6 +93,9 @@ func renderGraphiQL(w http.ResponseWriter, params graphql.Params, handler Handle
70
93
// graphiqlVersion is the current version of GraphiQL
71
94
const graphiqlVersion = "0.11.10"
72
95
96
+ // subscriptionTransportVersion is the current version of the subscription transport of GraphiQL
97
+ const subscriptionTransportVersion = "0.8.2"
98
+
73
99
// tmpl is the page template to render GraphiQL
74
100
const graphiqlTemplate = `
75
101
{{ define "index" }}
@@ -96,10 +122,20 @@ add "&raw" to the end of the URL within a browser.
96
122
}
97
123
</style>
98
124
<link href="//cdn.jsdelivr.net/npm/graphiql@{{ .GraphiqlVersion }}/graphiql.css" rel="stylesheet" />
99
- <script src="//cdn.jsdelivr.net/fetch/0.9.0/fetch.min.js"></script>
100
125
<script src="//cdn.jsdelivr.net/react/15.4.2/react.min.js"></script>
101
126
<script src="//cdn.jsdelivr.net/react/15.4.2/react-dom.min.js"></script>
102
127
<script src="//cdn.jsdelivr.net/npm/graphiql@{{ .GraphiqlVersion }}/graphiql.min.js"></script>
128
+
129
+ {{ if .UsingHTTP }}
130
+ <script src="//cdn.jsdelivr.net/fetch/2.0.1/fetch.min.js"></script>
131
+ {{ end }}
132
+ {{ if .UsingWS }}
133
+ <script src="//unpkg.com/subscriptions-transport-ws@{{ .SubscriptionTransportVersion }}/browser/client.js"></script>
134
+ {{ end }}
135
+ {{ if and .UsingWS .UsingHTTP }}
136
+ <script src="//unpkg.com/[email protected] /browser/client.js"></script>
137
+ {{ end }}
138
+
103
139
</head>
104
140
<body>
105
141
<script>
@@ -136,28 +172,49 @@ add "&raw" to the end of the URL within a browser.
136
172
otherParams[k] = parameters[k];
137
173
}
138
174
}
139
- var fetchURL = locationQuery(otherParams, {{ .EndpointURL }});
140
-
141
- // Defines a GraphQL fetcher using the fetch API.
142
- function graphQLFetcher(graphQLParams) {
143
- return fetch(fetchURL, {
144
- method: 'post',
145
- headers: {
146
- 'Accept': 'application/json',
147
- 'Content-Type': 'application/json'
148
- },
149
- body: JSON.stringify(graphQLParams),
150
- credentials: 'include',
151
- }).then(function (response) {
152
- return response.text();
153
- }).then(function (responseBody) {
154
- try {
155
- return JSON.parse(responseBody);
156
- } catch (error) {
157
- return responseBody;
158
- }
175
+
176
+ {{ if .UsingWS }}
177
+ var subscriptionsClient = new window.SubscriptionsTransportWs.SubscriptionClient({{ .EndpointURLWS }}, {
178
+ reconnect: true
159
179
});
160
- }
180
+ var graphQLWSFetcher = subscriptionsClient.request.bind(subscriptionsClient);
181
+ {{ end }}
182
+
183
+ {{ if .UsingHTTP }}
184
+ var fetchURL = locationQuery(otherParams, {{ .EndpointURL }});
185
+
186
+ // Defines a GraphQL fetcher using the fetch API.
187
+ function graphQLHttpFetcher(graphQLParams) {
188
+ return fetch(fetchURL, {
189
+ method: 'post',
190
+ headers: {
191
+ 'Accept': 'application/json',
192
+ 'Content-Type': 'application/json'
193
+ },
194
+ body: JSON.stringify(graphQLParams),
195
+ credentials: 'include',
196
+ }).then(function (response) {
197
+ return response.text();
198
+ }).then(function (responseBody) {
199
+ try {
200
+ return JSON.parse(responseBody);
201
+ } catch (error) {
202
+ return responseBody;
203
+ }
204
+ });
205
+ }
206
+ {{ end }}
207
+
208
+ {{ if and .UsingWS .UsingHTTP }}
209
+ var fetcher = window.GraphiQLSubscriptionsFetcher.graphQLFetcher(subscriptionsClient, graphQLHttpFetcher);
210
+ {{ else }}
211
+ {{ if .UsingWS }}
212
+ var fetcher = 'graphQLWSFetcher';
213
+ {{ end }}
214
+ {{ if .UsingHTTP }}
215
+ var fetcher = 'graphQLHttpFetcher';
216
+ {{ end }}
217
+ {{ end }}
161
218
162
219
// When the query and variables string is edited, update the URL bar so
163
220
// that it can be easily shared.
@@ -183,7 +240,7 @@ add "&raw" to the end of the URL within a browser.
183
240
// Render <GraphiQL /> into the body.
184
241
ReactDOM.render(
185
242
React.createElement(GraphiQL, {
186
- fetcher: graphQLFetcher ,
243
+ fetcher: fetcher ,
187
244
onEditQuery: onEditQuery,
188
245
onEditVariables: onEditVariables,
189
246
onEditOperationName: onEditOperationName,
0 commit comments