UNPKG

5.22 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var GRAPHIQL_VERSION = '0.10.2';
4var SUBSCRIPTIONS_TRANSPORT_VERSION = '0.7.0';
5function safeSerialize(data) {
6 return data ? JSON.stringify(data).replace(/\//g, '\\/') : null;
7}
8function renderGraphiQL(data) {
9 var endpointURL = data.endpointURL;
10 var subscriptionsEndpoint = data.subscriptionsEndpoint;
11 var usingSubscriptions = !!subscriptionsEndpoint;
12 var queryString = data.query;
13 var variablesString = data.variables ? JSON.stringify(data.variables, null, 2) : null;
14 var resultString = null;
15 var operationName = data.operationName;
16 var passHeader = data.passHeader ? data.passHeader : '';
17 return "\n<!DOCTYPE html>\n<html>\n<head>\n <meta charset=\"utf-8\" />\n <title>GraphiQL</title>\n <meta name=\"robots\" content=\"noindex\" />\n <style>\n html, body {\n height: 100%;\n margin: 0;\n overflow: hidden;\n width: 100%;\n }\n </style>\n <link href=\"//cdn.jsdelivr.net/graphiql/" + GRAPHIQL_VERSION + "/graphiql.css\" rel=\"stylesheet\" />\n <script src=\"//cdn.jsdelivr.net/fetch/0.9.0/fetch.min.js\"></script>\n <script src=\"//cdn.jsdelivr.net/react/15.0.0/react.min.js\"></script>\n <script src=\"//cdn.jsdelivr.net/react/15.0.0/react-dom.min.js\"></script>\n <script src=\"//cdn.jsdelivr.net/graphiql/" + GRAPHIQL_VERSION + "/graphiql.min.js\"></script>\n " + (usingSubscriptions ?
18 "<script src=\"//unpkg.com/subscriptions-transport-ws@" + SUBSCRIPTIONS_TRANSPORT_VERSION + "/browser/client.js\"></script>" +
19 '<script src="//unpkg.com/graphiql-subscriptions-fetcher@0.0.2/browser/client.js"></script>'
20 : '') + "\n</head>\n<body>\n <script>\n // Collect the URL parameters\n var parameters = {};\n window.location.search.substr(1).split('&').forEach(function (entry) {\n var eq = entry.indexOf('=');\n if (eq >= 0) {\n parameters[decodeURIComponent(entry.slice(0, eq))] =\n decodeURIComponent(entry.slice(eq + 1));\n }\n });\n // Produce a Location query string from a parameter object.\n function locationQuery(params, location) {\n return (location ? location: '') + '?' + Object.keys(params).map(function (key) {\n return encodeURIComponent(key) + '=' +\n encodeURIComponent(params[key]);\n }).join('&');\n }\n // Derive a fetch URL from the current URL, sans the GraphQL parameters.\n var graphqlParamNames = {\n query: true,\n variables: true,\n operationName: true\n };\n var otherParams = {};\n for (var k in parameters) {\n if (parameters.hasOwnProperty(k) && graphqlParamNames[k] !== true) {\n otherParams[k] = parameters[k];\n }\n }\n\n var fetcher;\n\n if (" + usingSubscriptions + ") {\n var subscriptionsClient = new window.SubscriptionsTransportWs.SubscriptionClient('" + subscriptionsEndpoint + "', {\n reconnect: true\n });\n fetcher = window.GraphiQLSubscriptionsFetcher.graphQLFetcher(subscriptionsClient, graphQLFetcher);\n } else {\n fetcher = graphQLFetcher;\n }\n\n // We don't use safe-serialize for location, because it's not client input.\n var fetchURL = locationQuery(otherParams, '" + endpointURL + "');\n\n // Defines a GraphQL fetcher using the fetch API.\n function graphQLFetcher(graphQLParams) {\n return fetch(fetchURL, {\n method: 'post',\n headers: {\n 'Accept': 'application/json',\n 'Content-Type': 'application/json',\n " + passHeader + "\n },\n body: JSON.stringify(graphQLParams),\n credentials: 'include',\n }).then(function (response) {\n return response.text();\n }).then(function (responseBody) {\n try {\n return JSON.parse(responseBody);\n } catch (error) {\n return responseBody;\n }\n });\n }\n // When the query and variables string is edited, update the URL bar so\n // that it can be easily shared.\n function onEditQuery(newQuery) {\n parameters.query = newQuery;\n updateURL();\n }\n function onEditVariables(newVariables) {\n parameters.variables = newVariables;\n updateURL();\n }\n function onEditOperationName(newOperationName) {\n parameters.operationName = newOperationName;\n updateURL();\n }\n function updateURL() {\n history.replaceState(null, null, locationQuery(parameters) + window.location.hash);\n }\n // Render <GraphiQL /> into the body.\n ReactDOM.render(\n React.createElement(GraphiQL, {\n fetcher: fetcher,\n onEditQuery: onEditQuery,\n onEditVariables: onEditVariables,\n onEditOperationName: onEditOperationName,\n query: " + safeSerialize(queryString) + ",\n response: " + safeSerialize(resultString) + ",\n variables: " + safeSerialize(variablesString) + ",\n operationName: " + safeSerialize(operationName) + ",\n }),\n document.body\n );\n </script>\n</body>\n</html>";
21}
22exports.renderGraphiQL = renderGraphiQL;
23//# sourceMappingURL=renderGraphiQL.js.map
\No newline at end of file