|
5 | 5 | If you wish to receive JSON, provide the header "Accept: application/json" or
|
6 | 6 | add "&raw" to the end of the URL within a browser.
|
7 | 7 | -->
|
| 8 | +{% load static %} |
8 | 9 | <!DOCTYPE html>
|
9 | 10 | <html>
|
10 | 11 | <head>
|
|
23 | 24 | <script src="//cdn.jsdelivr.net/npm/graphiql@{{graphiql_version}}/graphiql.min.js"></script>
|
24 | 25 | </head>
|
25 | 26 | <body>
|
26 |
| - <script> |
27 |
| - // Parse the cookie value for a CSRF token |
28 |
| - var csrftoken; |
29 |
| - var cookies = ('; ' + document.cookie).split('; csrftoken='); |
30 |
| - if (cookies.length == 2) |
31 |
| - csrftoken = cookies.pop().split(';').shift(); |
32 |
| - |
33 |
| - // Collect the URL parameters |
34 |
| - var parameters = {}; |
35 |
| - window.location.hash.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 fragment 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 |
| - |
62 |
| - // If there are any fragment parameters, confirm the user wants to use them. |
63 |
| - var isReload = window.performance ? performance.navigation.type === 1 : false; |
64 |
| - var isQueryTrusted = Object.keys(parameters).length === 0 || isReload; |
65 |
| - |
66 |
| - var fetchURL = locationQuery(otherParams); |
67 |
| - |
68 |
| - // Defines a GraphQL fetcher using the fetch API. |
69 |
| - function graphQLFetcher(graphQLParams) { |
70 |
| - var isIntrospectionQuery = ( |
71 |
| - graphQLParams.query !== parameters.query |
72 |
| - && graphQLParams.query.indexOf('IntrospectionQuery') !== -1 |
73 |
| - ); |
74 |
| - |
75 |
| - if (!isQueryTrusted |
76 |
| - && !isIntrospectionQuery |
77 |
| - && !window.confirm("This query was loaded from a link, are you sure you want to execute it?")) { |
78 |
| - return Promise.resolve('Aborting query.'); |
79 |
| - } |
80 |
| - |
81 |
| - // We don't want to set this for the introspection query |
82 |
| - if (!isIntrospectionQuery) { |
83 |
| - isQueryTrusted = true; |
84 |
| - } |
85 |
| - |
86 |
| - var headers = { |
87 |
| - 'Accept': 'application/json', |
88 |
| - 'Content-Type': 'application/json' |
89 |
| - }; |
90 |
| - if (csrftoken) { |
91 |
| - headers['X-CSRFToken'] = csrftoken; |
92 |
| - } |
93 |
| - return fetch(fetchURL, { |
94 |
| - method: 'post', |
95 |
| - headers: headers, |
96 |
| - body: JSON.stringify(graphQLParams), |
97 |
| - credentials: 'include', |
98 |
| - }).then(function (response) { |
99 |
| - return response.text(); |
100 |
| - }).then(function (responseBody) { |
101 |
| - try { |
102 |
| - return JSON.parse(responseBody); |
103 |
| - } catch (error) { |
104 |
| - return responseBody; |
105 |
| - } |
106 |
| - }); |
107 |
| - } |
108 |
| - // When the query and variables string is edited, update the URL bar so |
109 |
| - // that it can be easily shared. |
110 |
| - function onEditQuery(newQuery) { |
111 |
| - parameters.query = newQuery; |
112 |
| - updateURL(); |
113 |
| - } |
114 |
| - function onEditVariables(newVariables) { |
115 |
| - parameters.variables = newVariables; |
116 |
| - updateURL(); |
117 |
| - } |
118 |
| - function onEditOperationName(newOperationName) { |
119 |
| - parameters.operationName = newOperationName; |
120 |
| - updateURL(); |
121 |
| - } |
122 |
| - function updateURL() { |
123 |
| - history.replaceState(null, null, locationQuery(parameters)); |
124 |
| - } |
125 |
| - var options = { |
126 |
| - fetcher: graphQLFetcher, |
127 |
| - onEditQuery: onEditQuery, |
128 |
| - onEditVariables: onEditVariables, |
129 |
| - onEditOperationName: onEditOperationName, |
130 |
| - query: parameters.query, |
131 |
| - } |
132 |
| - if (parameters.variables) { |
133 |
| - options.variables = parameters.variables; |
134 |
| - } |
135 |
| - if (parameters.operation_name) { |
136 |
| - options.operationName = parameters.operation_name; |
137 |
| - } |
138 |
| - // Render <GraphiQL /> into the body. |
139 |
| - ReactDOM.render( |
140 |
| - React.createElement(GraphiQL, options), |
141 |
| - document.body |
142 |
| - ); |
143 |
| - </script> |
| 27 | + <script src="{% static "graphene_django/graphiql.js" %}"></script> |
144 | 28 | </body>
|
145 | 29 | </html>
|
0 commit comments