|
24 | 24 | </head>
|
25 | 25 | <body>
|
26 | 26 | <script>
|
| 27 | + |
27 | 28 | // Parse the cookie value for a CSRF token
|
28 | 29 | var csrftoken;
|
29 | 30 | var cookies = ('; ' + document.cookie).split('; csrftoken=');
|
30 | 31 | if (cookies.length == 2)
|
31 | 32 | csrftoken = cookies.pop().split(';').shift();
|
32 | 33 |
|
33 |
| - // Collect the URL parameters |
34 |
| - var parameters = {}; |
35 |
| - window.location.search.substr(1).split('&').forEach(function (entry) { |
36 |
| - var eq = entry.indexOf('='); |
37 |
| - if (eq >= 0) { |
38 |
| - parameters[decodeURIComponent(entry.slice(0, eq))] = |
39 |
| - decodeURIComponent(entry.slice(eq + 1)); |
40 |
| - } |
41 |
| - }); |
42 |
| - // Produce a Location query string from a parameter object. |
43 |
| - function locationQuery(params) { |
44 |
| - return '?' + Object.keys(params).map(function (key) { |
45 |
| - return encodeURIComponent(key) + '=' + |
46 |
| - encodeURIComponent(params[key]); |
47 |
| - }).join('&'); |
48 |
| - } |
49 |
| - // Derive a fetch URL from the current URL, sans the GraphQL parameters. |
50 |
| - var graphqlParamNames = { |
51 |
| - query: true, |
52 |
| - variables: true, |
53 |
| - operationName: true |
54 |
| - }; |
55 |
| - var otherParams = {}; |
56 |
| - for (var k in parameters) { |
57 |
| - if (parameters.hasOwnProperty(k) && graphqlParamNames[k] !== true) { |
58 |
| - otherParams[k] = parameters[k]; |
59 |
| - } |
60 |
| - } |
61 |
| - var fetchURL = locationQuery(otherParams); |
62 | 34 | // Defines a GraphQL fetcher using the fetch API.
|
63 | 35 | function graphQLFetcher(graphQLParams) {
|
64 | 36 | var headers = {
|
|
68 | 40 | if (csrftoken) {
|
69 | 41 | headers['X-CSRFToken'] = csrftoken;
|
70 | 42 | }
|
71 |
| - return fetch(fetchURL, { |
| 43 | + return fetch('', { |
72 | 44 | method: 'post',
|
73 | 45 | headers: headers,
|
74 | 46 | body: JSON.stringify(graphQLParams),
|
|
83 | 55 | }
|
84 | 56 | });
|
85 | 57 | }
|
86 |
| - // When the query and variables string is edited, update the URL bar so |
87 |
| - // that it can be easily shared. |
88 |
| - function onEditQuery(newQuery) { |
89 |
| - parameters.query = newQuery; |
90 |
| - updateURL(); |
91 |
| - } |
92 |
| - function onEditVariables(newVariables) { |
93 |
| - parameters.variables = newVariables; |
94 |
| - updateURL(); |
95 |
| - } |
96 |
| - function onEditOperationName(newOperationName) { |
97 |
| - parameters.operationName = newOperationName; |
98 |
| - updateURL(); |
99 |
| - } |
100 |
| - function updateURL() { |
101 |
| - history.replaceState(null, null, locationQuery(parameters)); |
102 |
| - } |
| 58 | + |
103 | 59 | // Render <GraphiQL /> into the body.
|
104 | 60 | ReactDOM.render(
|
105 |
| - React.createElement(GraphiQL, { |
106 |
| - fetcher: graphQLFetcher, |
107 |
| - onEditQuery: onEditQuery, |
108 |
| - onEditVariables: onEditVariables, |
109 |
| - onEditOperationName: onEditOperationName, |
110 |
| - query: '{{ query|escapejs }}', |
111 |
| - response: '{{ result|escapejs }}', |
112 |
| - {% if variables %} |
113 |
| - variables: '{{ variables|escapejs }}', |
114 |
| - {% endif %} |
115 |
| - {% if operation_name %} |
116 |
| - operationName: '{{ operation_name|escapejs }}', |
117 |
| - {% endif %} |
118 |
| - }), |
| 61 | + React.createElement(GraphiQL, {fetcher: graphQLFetcher}), |
119 | 62 | document.body
|
120 | 63 | );
|
| 64 | + |
121 | 65 | </script>
|
122 | 66 | </body>
|
123 | 67 | </html>
|
0 commit comments