forked from graphql/express-graphql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
renderGraphiQL.js
188 lines (172 loc) · 5.5 KB
/
renderGraphiQL.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// @flow strict
type GraphiQLData = {|
query: ?string,
variables: ?{ +[name: string]: mixed, ... },
operationName: ?string,
result?: mixed,
options: GraphiQLOptions,
|};
export type GraphiQLOptions = {|
/**
* An optional GraphQL string to use when no query is provided and no stored
* query exists from a previous session. If undefined is provided, GraphiQL
* will use its own default query.
*/
defaultQuery?: ?string,
|};
// Ensures string values are safe to be used within a <script> tag.
function safeSerialize(data) {
return data != null
? JSON.stringify(data).replace(/\//g, '\\/')
: 'undefined';
}
// Implemented as Babel transformation, see ../resources/load-staticly-from-npm.js
declare function loadFileStaticlyFromNPM(npmPath: string): string;
/**
* When express-graphql receives a request which does not Accept JSON, but does
* Accept HTML, it may present GraphiQL, the in-browser GraphQL explorer IDE.
*
* When shown, it will be pre-populated with the result of having executed the
* requested query.
*/
export function renderGraphiQL(data: GraphiQLData): string {
const queryString = data.query;
const variablesString =
data.variables != null ? JSON.stringify(data.variables, null, 2) : null;
const resultString =
data.result != null ? JSON.stringify(data.result, null, 2) : null;
const operationName = data.operationName;
const defaultQuery = data.options.defaultQuery;
return `<!--
The request to this GraphQL server provided the header "Accept: text/html"
and as a result has been presented GraphiQL - an in-browser IDE for
exploring GraphQL.
If you wish to receive JSON, provide the header "Accept: application/json" or
add "&raw" to the end of the URL within a browser.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>GraphiQL</title>
<meta name="robots" content="noindex" />
<meta name="referrer" content="origin" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
margin: 0;
overflow: hidden;
}
#graphiql {
height: 100vh;
}
</style>
<style>
/* graphiql/graphiql.css */
${loadFileStaticlyFromNPM('graphiql/graphiql.css')}
</style>
<script>
// promise-polyfill/dist/polyfill.min.js
${loadFileStaticlyFromNPM('promise-polyfill/dist/polyfill.min.js')}
</script>
<script>
// unfetch/dist/unfetch.umd.js
${loadFileStaticlyFromNPM('unfetch/dist/unfetch.umd.js')}
</script>
<script>
// react/umd/react.production.min.js
${loadFileStaticlyFromNPM('react/umd/react.production.min.js')}
</script>
<script>
// react-dom/umd/react-dom.production.min.js
${loadFileStaticlyFromNPM('react-dom/umd/react-dom.production.min.js')}
</script>
<script>
// graphiql/graphiql.min.js
${loadFileStaticlyFromNPM('graphiql/graphiql.min.js')}
</script>
</head>
<body>
<div id="graphiql">Loading...</div>
<script>
// Collect the URL parameters
var parameters = {};
window.location.search.substr(1).split('&').forEach(function (entry) {
var eq = entry.indexOf('=');
if (eq >= 0) {
parameters[decodeURIComponent(entry.slice(0, eq))] =
decodeURIComponent(entry.slice(eq + 1));
}
});
// Produce a Location query string from a parameter object.
function locationQuery(params) {
return '?' + Object.keys(params).filter(function (key) {
return Boolean(params[key]);
}).map(function (key) {
return encodeURIComponent(key) + '=' +
encodeURIComponent(params[key]);
}).join('&');
}
// Derive a fetch URL from the current URL, sans the GraphQL parameters.
var graphqlParamNames = {
query: true,
variables: true,
operationName: true
};
var otherParams = {};
for (var k in parameters) {
if (parameters.hasOwnProperty(k) && graphqlParamNames[k] !== true) {
otherParams[k] = parameters[k];
}
}
var fetchURL = locationQuery(otherParams);
// Defines a GraphQL fetcher using the fetch API.
function graphQLFetcher(graphQLParams) {
return fetch(fetchURL, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(graphQLParams),
credentials: 'include',
}).then(function (response) {
return response.json();
});
}
// When the query and variables string is edited, update the URL bar so
// that it can be easily shared.
function onEditQuery(newQuery) {
parameters.query = newQuery;
updateURL();
}
function onEditVariables(newVariables) {
parameters.variables = newVariables;
updateURL();
}
function onEditOperationName(newOperationName) {
parameters.operationName = newOperationName;
updateURL();
}
function updateURL() {
history.replaceState(null, null, locationQuery(parameters));
}
// Render <GraphiQL /> into the body.
ReactDOM.render(
React.createElement(GraphiQL, {
fetcher: graphQLFetcher,
onEditQuery: onEditQuery,
onEditVariables: onEditVariables,
onEditOperationName: onEditOperationName,
query: ${safeSerialize(queryString)},
response: ${safeSerialize(resultString)},
variables: ${safeSerialize(variablesString)},
operationName: ${safeSerialize(operationName)},
defaultQuery: ${safeSerialize(defaultQuery)},
}),
document.getElementById('graphiql')
);
</script>
</body>
</html>`;
}