|
| 1 | +import json |
| 2 | +import re |
| 3 | + |
| 4 | +GRAPHIQL_VERSION = "0.17.5" |
| 5 | + |
| 6 | +TEMPLATE = """<!-- |
| 7 | +The request to this GraphQL server provided the header "Accept: text/html" |
| 8 | +and as a result has been presented GraphiQL - an in-browser IDE for |
| 9 | +exploring GraphQL. |
| 10 | +If you wish to receive JSON, provide the header "Accept: application/json" or |
| 11 | +add "&raw" to the end of the URL within a browser. |
| 12 | +--> |
| 13 | +<!DOCTYPE html> |
| 14 | +<html> |
| 15 | +<head> |
| 16 | + <style> |
| 17 | + html, body { |
| 18 | + height: 100%; |
| 19 | + margin: 0; |
| 20 | + overflow: hidden; |
| 21 | + width: 100%; |
| 22 | + } |
| 23 | + </style> |
| 24 | + <meta name="referrer" content="no-referrer"> |
| 25 | + <link |
| 26 | + href="//cdn.jsdelivr.net/graphiql/{{graphiql_version}}/graphiql.css" |
| 27 | + rel="stylesheet" /> |
| 28 | + <script src="//cdn.jsdelivr.net/fetch/0.9.0/fetch.min.js"></script> |
| 29 | + <script src="//cdn.jsdelivr.net/react/15.0.0/react.min.js"></script> |
| 30 | + <script src="//cdn.jsdelivr.net/react/15.0.0/react-dom.min.js"></script> |
| 31 | + <script |
| 32 | + src="//cdn.jsdelivr.net/graphiql/{{graphiql_version}}/graphiql.min.js"> |
| 33 | + </script> |
| 34 | +</head> |
| 35 | +<body> |
| 36 | + <script> |
| 37 | + // Collect the URL parameters |
| 38 | + var parameters = {}; |
| 39 | + window.location.search.substr(1).split('&').forEach(function (entry) { |
| 40 | + var eq = entry.indexOf('='); |
| 41 | + if (eq >= 0) { |
| 42 | + parameters[decodeURIComponent(entry.slice(0, eq))] = |
| 43 | + decodeURIComponent(entry.slice(eq + 1)); |
| 44 | + } |
| 45 | + }); |
| 46 | + // Produce a Location query string from a parameter object. |
| 47 | + function locationQuery(params) { |
| 48 | + return '?' + Object.keys(params).map(function (key) { |
| 49 | + return encodeURIComponent(key) + '=' + |
| 50 | + encodeURIComponent(params[key]); |
| 51 | + }).join('&'); |
| 52 | + } |
| 53 | + // Derive a fetch URL from the current URL, sans the GraphQL parameters. |
| 54 | + var graphqlParamNames = { |
| 55 | + query: true, |
| 56 | + variables: true, |
| 57 | + operationName: true |
| 58 | + }; |
| 59 | + var otherParams = {}; |
| 60 | + for (var k in parameters) { |
| 61 | + if (parameters.hasOwnProperty(k) && graphqlParamNames[k] !== true) { |
| 62 | + otherParams[k] = parameters[k]; |
| 63 | + } |
| 64 | + } |
| 65 | + var fetchURL = locationQuery(otherParams); |
| 66 | + // Defines a GraphQL fetcher using the fetch API. |
| 67 | + function graphQLFetcher(graphQLParams) { |
| 68 | + return fetch(fetchURL, { |
| 69 | + method: 'post', |
| 70 | + headers: { |
| 71 | + 'Accept': 'application/json', |
| 72 | + 'Content-Type': 'application/json' |
| 73 | + }, |
| 74 | + body: JSON.stringify(graphQLParams), |
| 75 | + credentials: 'include', |
| 76 | + }).then(function (response) { |
| 77 | + return response.text(); |
| 78 | + }).then(function (responseBody) { |
| 79 | + try { |
| 80 | + return JSON.parse(responseBody); |
| 81 | + } catch (error) { |
| 82 | + return responseBody; |
| 83 | + } |
| 84 | + }); |
| 85 | + } |
| 86 | + // When the query and variables string is edited, update the URL bar so |
| 87 | + // that it can be easily shared. |
| 88 | + function onEditQuery(newQuery) { |
| 89 | + parameters.query = newQuery; |
| 90 | + updateURL(); |
| 91 | + } |
| 92 | + function onEditVariables(newVariables) { |
| 93 | + parameters.variables = newVariables; |
| 94 | + updateURL(); |
| 95 | + } |
| 96 | + function onEditOperationName(newOperationName) { |
| 97 | + parameters.operationName = newOperationName; |
| 98 | + updateURL(); |
| 99 | + } |
| 100 | + function updateURL() { |
| 101 | + history.replaceState(null, null, locationQuery(parameters)); |
| 102 | + } |
| 103 | + // Render <GraphiQL /> into the body. |
| 104 | + ReactDOM.render( |
| 105 | + React.createElement(GraphiQL, { |
| 106 | + fetcher: graphQLFetcher, |
| 107 | + onEditQuery: onEditQuery, |
| 108 | + onEditVariables: onEditVariables, |
| 109 | + onEditOperationName: onEditOperationName, |
| 110 | + query: {{query|tojson}}, |
| 111 | + response: {{result|tojson}}, |
| 112 | + variables: {{variables|tojson}}, |
| 113 | + operationName: {{operation_name|tojson}}, |
| 114 | + }), |
| 115 | + document.body |
| 116 | + ); |
| 117 | + </script> |
| 118 | +</body> |
| 119 | +</html>""" |
| 120 | + |
| 121 | + |
| 122 | +def escape_js_value(value): |
| 123 | + quotation = False |
| 124 | + if value.startswith('"') and value.endswith('"'): |
| 125 | + quotation = True |
| 126 | + value = value[1 : len(value) - 1] |
| 127 | + |
| 128 | + value = value.replace("\\\\n", "\\\\\\n").replace("\\n", "\\\\n") |
| 129 | + if quotation: |
| 130 | + value = '"' + value.replace('\\\\"', '"').replace('"', '\\"') + '"' |
| 131 | + |
| 132 | + return value |
| 133 | + |
| 134 | + |
| 135 | +def process_var(template, name, value, jsonify=False): |
| 136 | + pattern = r"{{\s*" + name + r"(\s*|[^}]+)*\s*}}" |
| 137 | + if jsonify and value not in ["null", "undefined"]: |
| 138 | + value = json.dumps(value) |
| 139 | + value = escape_js_value(value) |
| 140 | + |
| 141 | + return re.sub(pattern, value, template) |
| 142 | + |
| 143 | + |
| 144 | +def simple_renderer(template, **values): |
| 145 | + replace = ["graphiql_version"] |
| 146 | + replace_jsonify = ["query", "result", "variables", "operation_name"] |
| 147 | + |
| 148 | + for r in replace: |
| 149 | + template = process_var(template, r, values.get(r, "")) |
| 150 | + |
| 151 | + for r in replace_jsonify: |
| 152 | + template = process_var(template, r, values.get(r, ""), True) |
| 153 | + |
| 154 | + return template |
| 155 | + |
| 156 | + |
| 157 | +def render_graphiql( |
| 158 | + graphiql_version=None, graphiql_template=None, params=None, result=None, |
| 159 | +): |
| 160 | + graphiql_version = graphiql_version or GRAPHIQL_VERSION |
| 161 | + template = graphiql_template or TEMPLATE |
| 162 | + |
| 163 | + template_vars = { |
| 164 | + "graphiql_version": graphiql_version, |
| 165 | + "query": params and params.query, |
| 166 | + "variables": params and params.variables, |
| 167 | + "operation_name": params and params.operation_name, |
| 168 | + "result": result, |
| 169 | + } |
| 170 | + |
| 171 | + source = simple_renderer(template, **template_vars) |
| 172 | + return source |
0 commit comments