|
1 | | -// Parse the search string to get url parameters. |
2 | 1 | var search = window.location.search; |
3 | | -var parameters = {}; |
| 2 | +var initParameters = {}; |
4 | 3 | search.substr(1).split('&').forEach(function (entry) { |
5 | 4 | var eq = entry.indexOf('='); |
6 | 5 | if (eq >= 0) { |
7 | | - parameters[decodeURIComponent(entry.slice(0, eq))] = |
8 | | - decodeURIComponent(entry.slice(eq + 1)); |
| 6 | + initParameters[decodeURIComponent(entry.slice(0, eq))] = decodeURIComponent(entry.slice(eq + 1)); |
9 | 7 | } |
10 | 8 | }); |
11 | 9 |
|
12 | | -// If variables was provided, try to format it. |
13 | | -if (parameters.variables) { |
| 10 | +if (initParameters.variables) { |
14 | 11 | try { |
15 | | - parameters.variables = |
16 | | - JSON.stringify(JSON.parse(parameters.variables), null, 2); |
| 12 | + initParameters.variables = JSON.stringify(JSON.parse(initParameters.variables), null, 2); |
17 | 13 | } catch (e) { |
18 | | - // Do nothing, we want to display the invalid JSON as a string, rather |
19 | | - // than present an error. |
| 14 | + // Do nothing, we want to display the invalid JSON as a string, rather than present an error. |
20 | 15 | } |
21 | 16 | } |
22 | 17 |
|
23 | | -// When the query and variables string is edited, update the URL bar so |
24 | | -// that it can be easily shared |
25 | | -function onEditQuery(newQuery) { |
26 | | - parameters.query = newQuery; |
27 | | - updateURL(); |
28 | | -} |
| 18 | +function App() { |
| 19 | + var self = this; |
| 20 | + var graphiql = GraphiQL; |
29 | 21 |
|
30 | | -function onEditVariables(newVariables) { |
31 | | - parameters.variables = newVariables; |
32 | | - updateURL(); |
33 | | -} |
| 22 | + var useStateParameters = React.useState(initParameters); |
| 23 | + var parameters = useStateParameters[0]; |
| 24 | + var setParameters = useStateParameters[1]; |
34 | 25 |
|
35 | | -function onEditOperationName(newOperationName) { |
36 | | - parameters.operationName = newOperationName; |
37 | | - updateURL(); |
38 | | -} |
| 26 | + var useStateQuery = React.useState(parameters.query); |
| 27 | + var query = useStateQuery[0]; |
| 28 | + var setQuery = useStateQuery[1]; |
39 | 29 |
|
40 | | -function updateURL() { |
41 | | - var newSearch = '?' + Object.keys(parameters).filter(function (key) { |
42 | | - return Boolean(parameters[key]); |
43 | | - }).map(function (key) { |
44 | | - return encodeURIComponent(key) + '=' + |
45 | | - encodeURIComponent(parameters[key]); |
46 | | - }).join('&'); |
47 | | - history.replaceState(null, null, newSearch); |
48 | | -} |
| 30 | + var useStateExplorerIsOpen = React.useState(true); |
| 31 | + var explorerIsOpen = useStateExplorerIsOpen[0]; |
| 32 | + var setExplorerIsOpen = useStateExplorerIsOpen[1]; |
| 33 | + |
| 34 | + function graphQLFetcher(graphQLParams) { |
| 35 | + return fetch(window.location.pathname, { |
| 36 | + method: 'post', |
| 37 | + headers: { |
| 38 | + 'Accept': 'application/json', |
| 39 | + 'Content-Type': 'application/json' |
| 40 | + }, |
| 41 | + body: JSON.stringify(graphQLParams), |
| 42 | + credentials: 'include' |
| 43 | + }).then(function (response) { |
| 44 | + return response.text(); |
| 45 | + }).then(function (responseBody) { |
| 46 | + try { |
| 47 | + return JSON.parse(responseBody); |
| 48 | + } catch (error) { |
| 49 | + return responseBody; |
| 50 | + } |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + function onEditQuery(newQuery) { |
| 55 | + setParameters(Object.assign(parameters, {query: newQuery})); |
| 56 | + setQuery(newQuery); |
| 57 | + updateURL(); |
| 58 | + } |
| 59 | + |
| 60 | + function onEditVariables(newVariables) { |
| 61 | + setParameters(Object.assign(parameters, {variables: newVariables})); |
| 62 | + updateURL(); |
| 63 | + } |
| 64 | + |
| 65 | + function onEditOperationName(newOperationName) { |
| 66 | + setParameters(Object.assign(parameters, {operationName: newOperationName})); |
| 67 | + updateURL(); |
| 68 | + } |
| 69 | + |
| 70 | + function onRunOperation(operationName) { |
| 71 | + self.graphiql.handleRunQuery(operationName); |
| 72 | + } |
| 73 | + |
| 74 | + function updateURL() { |
| 75 | + var newSearch = '?' + Object.keys(parameters).filter(function (key) { |
| 76 | + return Boolean(parameters[key]); |
| 77 | + }).map(function (key) { |
| 78 | + return encodeURIComponent(key) + '=' + encodeURIComponent(parameters[key]); |
| 79 | + }).join('&'); |
| 80 | + history.replaceState(null, null, newSearch); |
| 81 | + } |
| 82 | + |
| 83 | + function handleToggleExplorer() { |
| 84 | + setExplorerIsOpen(!explorerIsOpen) |
| 85 | + } |
| 86 | + |
| 87 | + function handlePrettifyQuery() { |
| 88 | + self.graphiql.handlePrettifyQuery(); |
| 89 | + } |
| 90 | + |
| 91 | + function handleMergeQuery() { |
| 92 | + self.graphiql.handleMergeQuery(); |
| 93 | + } |
| 94 | + |
| 95 | + function handleCopyQuery() { |
| 96 | + self.graphiql.handleCopyQuery(); |
| 97 | + } |
| 98 | + |
| 99 | + function handleToggleHistory() { |
| 100 | + self.graphiql.handleToggleHistory(); |
| 101 | + } |
49 | 102 |
|
50 | | -// Defines a GraphQL fetcher using the fetch API. |
51 | | -function graphQLFetcher(graphQLParams) { |
52 | | - return fetch(window.location.pathname, { |
53 | | - method: 'post', |
54 | | - headers: { |
55 | | - 'Accept': 'application/json', |
56 | | - 'Content-Type': 'application/json', |
57 | | - }, |
58 | | - body: JSON.stringify(graphQLParams), |
59 | | - credentials: 'include', |
60 | | - }).then(function (response) { |
61 | | - return response.text(); |
62 | | - }).then(function (responseBody) { |
63 | | - try { |
64 | | - return JSON.parse(responseBody); |
65 | | - } catch (error) { |
66 | | - return responseBody; |
67 | | - } |
68 | | - }); |
| 103 | + return React.createElement( |
| 104 | + 'div', |
| 105 | + {className: 'graphiql-container'}, |
| 106 | + React.createElement(GraphiQLExplorer.Explorer, { |
| 107 | + fetcher: graphQLFetcher, |
| 108 | + query: query, |
| 109 | + onEdit: onEditQuery, |
| 110 | + onRunOperation: onRunOperation, |
| 111 | + explorerIsOpen: explorerIsOpen, |
| 112 | + onToggleExplorer: handleToggleExplorer |
| 113 | + }), |
| 114 | + React.createElement( |
| 115 | + GraphiQL, |
| 116 | + { |
| 117 | + ref: function (ref) { self.graphiql = ref }, |
| 118 | + fetcher: graphQLFetcher, |
| 119 | + query: query, |
| 120 | + variables: parameters.variables, |
| 121 | + operationName: parameters.operationName, |
| 122 | + onEditQuery: onEditQuery, |
| 123 | + onEditVariables: onEditVariables, |
| 124 | + onEditOperationName: onEditOperationName |
| 125 | + }, |
| 126 | + React.createElement( |
| 127 | + GraphiQL.Toolbar, |
| 128 | + {}, |
| 129 | + React.createElement( |
| 130 | + GraphiQL.ToolbarButton, |
| 131 | + { |
| 132 | + onClick: handlePrettifyQuery, |
| 133 | + label: 'Prettify', |
| 134 | + title: 'Prettify Query (Shift-Ctrl-P)' |
| 135 | + } |
| 136 | + ), |
| 137 | + React.createElement( |
| 138 | + GraphiQL.ToolbarButton, |
| 139 | + { |
| 140 | + onClick: handleMergeQuery, |
| 141 | + label: 'Merge', |
| 142 | + title: 'Merge Query (Shift-Ctrl-M)' |
| 143 | + } |
| 144 | + ), |
| 145 | + React.createElement( |
| 146 | + GraphiQL.ToolbarButton, |
| 147 | + { |
| 148 | + onClick: handleCopyQuery, |
| 149 | + label: 'Copy', |
| 150 | + title: 'Copy Query (Shift-Ctrl-C)' |
| 151 | + } |
| 152 | + ), |
| 153 | + React.createElement( |
| 154 | + GraphiQL.ToolbarButton, |
| 155 | + { |
| 156 | + onClick: handleToggleHistory, |
| 157 | + label: 'History', |
| 158 | + title: 'Show History' |
| 159 | + } |
| 160 | + ), |
| 161 | + React.createElement( |
| 162 | + GraphiQL.ToolbarButton, |
| 163 | + { |
| 164 | + onClick: handleToggleExplorer, |
| 165 | + label: 'Explorer', |
| 166 | + title: 'Show Explorer' |
| 167 | + } |
| 168 | + ) |
| 169 | + ) |
| 170 | + ) |
| 171 | + ); |
69 | 172 | } |
70 | 173 |
|
71 | | -// Render <GraphiQL /> into the body. |
72 | | -ReactDOM.render( |
73 | | - React.createElement(GraphiQL, { |
74 | | - fetcher: graphQLFetcher, |
75 | | - query: parameters.query, |
76 | | - variables: parameters.variables, |
77 | | - operationName: parameters.operationName, |
78 | | - onEditQuery: onEditQuery, |
79 | | - onEditVariables: onEditVariables, |
80 | | - onEditOperationName: onEditOperationName |
81 | | - }), |
82 | | - document.getElementById('graphiql') |
83 | | -); |
| 174 | +ReactDOM.render(React.createElement(App), document.getElementById('graphiql')); |
0 commit comments