|
1
|
|
|
// Parse the search string to get url parameters. |
|
2
|
|
|
var search = window.location.search; |
|
3
|
|
|
var parameters = {}; |
|
4
|
|
|
search.substr(1).split('&').forEach(function (entry) { |
|
5
|
|
|
var eq = entry.indexOf('='); |
|
6
|
|
|
if (eq >= 0) { |
|
7
|
|
|
parameters[decodeURIComponent(entry.slice(0, eq))] = |
|
8
|
|
|
decodeURIComponent(entry.slice(eq + 1)); |
|
9
|
|
|
} |
|
10
|
|
|
}); |
|
11
|
|
|
|
|
12
|
|
|
// If variables was provided, try to format it. |
|
13
|
|
|
if (parameters.variables) { |
|
14
|
|
|
try { |
|
15
|
|
|
parameters.variables = |
|
16
|
|
|
JSON.stringify(JSON.parse(parameters.variables), null, 2); |
|
17
|
|
|
} catch (e) { |
|
|
|
|
|
|
18
|
|
|
// Do nothing, we want to display the invalid JSON as a string, rather |
|
19
|
|
|
// than present an error. |
|
20
|
|
|
} |
|
21
|
|
|
} |
|
22
|
|
|
|
|
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
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
function onEditVariables(newVariables) { |
|
31
|
|
|
parameters.variables = newVariables; |
|
32
|
|
|
updateURL(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
function onEditOperationName(newOperationName) { |
|
36
|
|
|
parameters.operationName = newOperationName; |
|
37
|
|
|
updateURL(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
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
|
|
|
} |
|
49
|
|
|
|
|
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
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
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
|
|
|
); |
|
84
|
|
|
|