|
1 | 1 | <!DOCTYPE html>
|
2 | 2 | <html lang="en">
|
3 |
| -<head> |
4 |
| - <meta charset="utf-8" /> |
5 |
| - <meta name="robots" content="noindex" /> |
6 |
| - <meta name="referrer" content="origin" /> |
7 |
| - <meta name="viewport" content="width=device-width, initial-scale=1" /> |
8 |
| - <title>SWAPI GraphQL API</title> |
9 |
| - <style> |
10 |
| - body { |
11 |
| - height: 100vh; |
12 |
| - margin: 0; |
13 |
| - overflow: hidden; |
14 |
| - } |
15 |
| - #splash { |
16 |
| - color: #333; |
17 |
| - display: flex; |
18 |
| - flex-direction: column; |
19 |
| - font-family: system, -apple-system, "San Francisco", ".SFNSDisplay-Regular", "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", helvetica, "Lucida Grande", arial, sans-serif; |
20 |
| - height: 100vh; |
21 |
| - justify-content: center; |
22 |
| - text-align: center; |
23 |
| - } |
24 |
| - </style> |
25 |
| - <link rel="icon" href="favicon.ico"> |
26 |
| - <link type="text/css" href="//unpkg.com/graphiql/graphiql.min.css" rel="stylesheet" /> |
27 |
| -</head> |
28 |
| -<body> |
29 |
| - <div id="splash"> |
30 |
| - Loading… |
31 |
| - </div> |
32 |
| - <script src="//cdn.jsdelivr.net/es6-promise/4.0.5/es6-promise.auto.min.js"></script> |
33 |
| - <script src="https://cdn.jsdelivr.net/npm/react/umd/react.production.min.js"></script> |
34 |
| - <script src="https://cdn.jsdelivr.net/npm/react-dom/umd/react-dom.production.min.js"></script> |
35 |
| - <script src="//unpkg.com/graphiql/graphiql.min.js"></script> |
36 |
| - <script> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <meta name="robots" content="noindex" /> |
| 6 | + <meta name="referrer" content="origin" /> |
| 7 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 8 | + <title>SWAPI GraphQL API</title> |
| 9 | + <style> |
| 10 | + body { |
| 11 | + height: 100vh; |
| 12 | + margin: 0; |
| 13 | + overflow: hidden; |
| 14 | + } |
| 15 | + #splash { |
| 16 | + color: #333; |
| 17 | + display: flex; |
| 18 | + flex-direction: column; |
| 19 | + font-family: system, -apple-system, "San Francisco", |
| 20 | + ".SFNSDisplay-Regular", "Segoe UI", Segoe, "Segoe WP", |
| 21 | + "Helvetica Neue", helvetica, "Lucida Grande", arial, sans-serif; |
| 22 | + height: 100vh; |
| 23 | + justify-content: center; |
| 24 | + text-align: center; |
| 25 | + } |
| 26 | + </style> |
| 27 | + <link rel="icon" href="favicon.ico" /> |
| 28 | + <link |
| 29 | + type="text/css" |
| 30 | + href="//unpkg.com/graphiql/graphiql.min.css" |
| 31 | + rel="stylesheet" |
| 32 | + /> |
| 33 | + </head> |
| 34 | + <body> |
| 35 | + <div id="splash">Loading…</div> |
| 36 | + <script src="//unpkg.com/react/umd/react.production.min.js"></script> |
| 37 | + <script src="//unpkg.com/react-dom/umd/react-dom.production.min.js"></script> |
| 38 | + <script src="//unpkg.com/graphiql/graphiql.min.js"></script> |
| 39 | + <script> |
37 | 40 | // Parse the search string to get url parameters.
|
38 | 41 | var search = window.location.search;
|
39 | 42 | var parameters = {};
|
40 |
| - search.substr(1).split('&').forEach(function (entry) { |
41 |
| - var eq = entry.indexOf('='); |
42 |
| - if (eq >= 0) { |
43 |
| - parameters[decodeURIComponent(entry.slice(0, eq))] = |
44 |
| - decodeURIComponent(entry.slice(eq + 1)); |
45 |
| - } |
46 |
| - }); |
| 43 | + search |
| 44 | + .substr(1) |
| 45 | + .split("&") |
| 46 | + .forEach(function (entry) { |
| 47 | + var eq = entry.indexOf("="); |
| 48 | + if (eq >= 0) { |
| 49 | + parameters[decodeURIComponent(entry.slice(0, eq))] = |
| 50 | + decodeURIComponent(entry.slice(eq + 1)); |
| 51 | + } |
| 52 | + }); |
47 | 53 |
|
48 | 54 | // if variables was provided, try to format it.
|
49 | 55 | if (parameters.variables) {
|
50 | 56 | try {
|
51 |
| - parameters.variables = |
52 |
| - JSON.stringify(JSON.parse(parameters.variables), null, 2); |
| 57 | + parameters.variables = JSON.stringify( |
| 58 | + JSON.parse(parameters.variables), |
| 59 | + null, |
| 60 | + 2 |
| 61 | + ); |
53 | 62 | } catch (e) {
|
54 | 63 | // Do nothing, we want to display the invalid JSON as a string, rather
|
55 | 64 | // than present an error.
|
|
71 | 80 | updateURL();
|
72 | 81 | }
|
73 | 82 | function updateURL() {
|
74 |
| - var newSearch = '?' + Object.keys(parameters).filter(function (key) { |
75 |
| - return Boolean(parameters[key]); |
76 |
| - }).map(function (key) { |
77 |
| - return encodeURIComponent(key) + '=' + |
78 |
| - encodeURIComponent(parameters[key]); |
79 |
| - }).join('&'); |
| 83 | + var newSearch = |
| 84 | + "?" + |
| 85 | + Object.keys(parameters) |
| 86 | + .filter(function (key) { |
| 87 | + return Boolean(parameters[key]); |
| 88 | + }) |
| 89 | + .map(function (key) { |
| 90 | + return ( |
| 91 | + encodeURIComponent(key) + |
| 92 | + "=" + |
| 93 | + encodeURIComponent(parameters[key]) |
| 94 | + ); |
| 95 | + }) |
| 96 | + .join("&"); |
80 | 97 | history.replaceState(null, null, newSearch);
|
81 | 98 | }
|
82 | 99 |
|
83 |
| - function graphQLFetcher(graphQLParams) { |
84 |
| - // This example expects a GraphQL server at the path /graphql. |
85 |
| - // Change this to point wherever you host your GraphQL server. |
86 |
| - return fetch(parameters.fetchURL || 'https://swapi-graphql.netlify.app/.netlify/functions/index', { |
87 |
| - method: 'post', |
88 |
| - headers: { |
89 |
| - 'Accept': 'application/json', |
90 |
| - 'Content-Type': 'application/json' |
91 |
| - }, |
92 |
| - body: JSON.stringify(graphQLParams), |
93 |
| - }).then(function (response) { |
94 |
| - return response.text(); |
95 |
| - }).then(function (responseBody) { |
96 |
| - try { |
97 |
| - return JSON.parse(responseBody); |
98 |
| - } catch (error) { |
99 |
| - return responseBody; |
100 |
| - } |
101 |
| - }); |
102 |
| - } |
| 100 | + const fetcher = GraphiQL.createFetcher({ |
| 101 | + url: |
| 102 | + parameters.fetchURL || |
| 103 | + "https://swapi-graphql.netlify.app/.netlify/functions/index", |
| 104 | + }); |
103 | 105 |
|
104 | 106 | // Render <GraphiQL /> into the body.
|
105 | 107 | ReactDOM.render(
|
106 | 108 | React.createElement(GraphiQL, {
|
107 |
| - fetcher: graphQLFetcher, |
| 109 | + fetcher: fetcher, |
108 | 110 | query: parameters.query,
|
109 | 111 | variables: parameters.variables,
|
110 | 112 | operationName: parameters.operationName,
|
111 | 113 | onEditQuery: onEditQuery,
|
112 | 114 | onEditVariables: onEditVariables,
|
113 |
| - onEditOperationName: onEditOperationName |
| 115 | + onEditOperationName: onEditOperationName, |
114 | 116 | }),
|
115 |
| - document.body, |
| 117 | + document.body |
116 | 118 | );
|
117 |
| - </script> |
118 |
| -</body> |
| 119 | + </script> |
| 120 | + </body> |
119 | 121 | </html>
|
120 |
| - |
|
0 commit comments