|
1 | 1 | (function ( |
2 | 2 | document, |
| 3 | + |
3 | 4 | GRAPHENE_SETTINGS, |
4 | 5 | GraphiQL, |
5 | 6 | React, |
6 | 7 | ReactDOM, |
7 | 8 | SubscriptionsTransportWs, |
| 9 | + fetch, |
8 | 10 | history, |
9 | 11 | location, |
10 | 12 | ) { |
11 | 13 | // Parse the cookie value for a CSRF token |
12 | 14 | var csrftoken; |
13 | | - var cookies = ('; ' + document.cookie).split('; csrftoken='); |
| 15 | + var cookies = ("; " + document.cookie).split("; csrftoken="); |
14 | 16 | if (cookies.length == 2) { |
15 | | - csrftoken = cookies.pop().split(';').shift(); |
| 17 | + csrftoken = cookies.pop().split(";").shift(); |
16 | 18 | } else { |
17 | 19 | csrftoken = document.querySelector("[name=csrfmiddlewaretoken]").value; |
18 | 20 | } |
19 | 21 |
|
20 | 22 | // Collect the URL parameters |
21 | 23 | var parameters = {}; |
22 | | - location.hash.substr(1).split('&').forEach(function (entry) { |
23 | | - var eq = entry.indexOf('='); |
24 | | - if (eq >= 0) { |
25 | | - parameters[decodeURIComponent(entry.slice(0, eq))] = |
26 | | - decodeURIComponent(entry.slice(eq + 1)); |
27 | | - } |
28 | | - }); |
| 24 | + location.hash |
| 25 | + .substr(1) |
| 26 | + .split("&") |
| 27 | + .forEach(function (entry) { |
| 28 | + var eq = entry.indexOf("="); |
| 29 | + if (eq >= 0) { |
| 30 | + parameters[decodeURIComponent(entry.slice(0, eq))] = decodeURIComponent( |
| 31 | + entry.slice(eq + 1), |
| 32 | + ); |
| 33 | + } |
| 34 | + }); |
29 | 35 | // Produce a Location fragment string from a parameter object. |
30 | 36 | function locationQuery(params) { |
31 | | - return '#' + Object.keys(params).map(function (key) { |
32 | | - return encodeURIComponent(key) + '=' + |
33 | | - encodeURIComponent(params[key]); |
34 | | - }).join('&'); |
| 37 | + return ( |
| 38 | + "#" + |
| 39 | + Object.keys(params) |
| 40 | + .map(function (key) { |
| 41 | + return ( |
| 42 | + encodeURIComponent(key) + "=" + encodeURIComponent(params[key]) |
| 43 | + ); |
| 44 | + }) |
| 45 | + .join("&") |
| 46 | + ); |
35 | 47 | } |
36 | 48 | // Derive a fetch URL from the current URL, sans the GraphQL parameters. |
37 | 49 | var graphqlParamNames = { |
38 | 50 | query: true, |
39 | 51 | variables: true, |
40 | | - operationName: true |
| 52 | + operationName: true, |
41 | 53 | }; |
42 | 54 | var otherParams = {}; |
43 | 55 | for (var k in parameters) { |
|
51 | 63 | // Defines a GraphQL fetcher using the fetch API. |
52 | 64 | function httpClient(graphQLParams) { |
53 | 65 | var headers = { |
54 | | - 'Accept': 'application/json', |
55 | | - 'Content-Type': 'application/json' |
| 66 | + Accept: "application/json", |
| 67 | + "Content-Type": "application/json", |
56 | 68 | }; |
57 | 69 | if (csrftoken) { |
58 | | - headers['X-CSRFToken'] = csrftoken; |
| 70 | + headers["X-CSRFToken"] = csrftoken; |
59 | 71 | } |
60 | 72 | return fetch(fetchURL, { |
61 | | - method: 'post', |
| 73 | + method: "post", |
62 | 74 | headers: headers, |
63 | 75 | body: JSON.stringify(graphQLParams), |
64 | | - credentials: 'include', |
65 | | - }).then(function (response) { |
66 | | - return response.text(); |
67 | | - }).then(function (responseBody) { |
68 | | - try { |
69 | | - return JSON.parse(responseBody); |
70 | | - } catch (error) { |
71 | | - return responseBody; |
72 | | - } |
73 | | - }); |
| 76 | + credentials: "include", |
| 77 | + }) |
| 78 | + .then(function (response) { |
| 79 | + return response.text(); |
| 80 | + }) |
| 81 | + .then(function (responseBody) { |
| 82 | + try { |
| 83 | + return JSON.parse(responseBody); |
| 84 | + } catch (error) { |
| 85 | + return responseBody; |
| 86 | + } |
| 87 | + }); |
74 | 88 | } |
75 | 89 |
|
76 | 90 | // Derive the subscription URL. If the SUBSCRIPTION_URL setting is specified, uses that value. Otherwise |
|
157 | 171 | onEditVariables: onEditVariables, |
158 | 172 | onEditOperationName: onEditOperationName, |
159 | 173 | query: parameters.query, |
160 | | - } |
| 174 | + }; |
161 | 175 | if (parameters.variables) { |
162 | 176 | options.variables = parameters.variables; |
163 | 177 | } |
|
167 | 181 | // Render <GraphiQL /> into the body. |
168 | 182 | ReactDOM.render( |
169 | 183 | React.createElement(GraphiQL, options), |
170 | | - document.getElementById("editor") |
| 184 | + document.getElementById("editor"), |
171 | 185 | ); |
172 | 186 | })( |
173 | 187 | document, |
| 188 | + |
174 | 189 | window.GRAPHENE_SETTINGS, |
175 | 190 | window.GraphiQL, |
176 | 191 | window.React, |
177 | 192 | window.ReactDOM, |
178 | 193 | window.SubscriptionsTransportWs, |
| 194 | + window.fetch, |
179 | 195 | window.history, |
180 | 196 | window.location, |
181 | 197 | ); |
0 commit comments