index.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!--
  2. * Copyright (c) 2019 GraphQL Contributors
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. -->
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11. <style>
  12. body {
  13. height: 100%;
  14. margin: 0;
  15. width: 100%;
  16. overflow: hidden;
  17. }
  18. #graphiql {
  19. height: 100vh;
  20. }
  21. </style>
  22. <!--
  23. This GraphiQL example depends on Promise and fetch, which are available in
  24. modern browsers, but can be "polyfilled" for older browsers.
  25. GraphiQL itself depends on React DOM.
  26. If you do not want to rely on a CDN, you can host these files locally or
  27. include them directly in your favored resource bunder.
  28. -->
  29. <script src="es6-promise.auto.min.js"></script>
  30. <script src="fetch.min.js"></script>
  31. <script src="react.min.js"></script>
  32. <script src="react-dom.min.js"></script>
  33. <script src="subscription-transport-ws-client.js"></script>
  34. <script src="graphql-subscriptions-fetcher-client.js"></script>
  35. <!--
  36. These two files can be found in the npm module, however you may wish to
  37. copy them directly into your environment, or perhaps include them in your
  38. favored resource bundler.
  39. -->
  40. <link rel="stylesheet" href="graphiql.css" />
  41. <script src="graphiql.js" charset="utf-8"></script>
  42. </head>
  43. <body>
  44. <div id="graphiql">Loading...</div>
  45. <script>
  46. /**
  47. * This GraphiQL example illustrates how to use some of GraphiQL's props
  48. * in order to enable reading and updating the URL parameters, making
  49. * link sharing of queries a little bit easier.
  50. *
  51. * This is only one example of this kind of feature, GraphiQL exposes
  52. * various React params to enable interesting integrations.
  53. */
  54. // Parse the search string to get url parameters.
  55. var search = window.location.search;
  56. var parameters = {};
  57. search.substr(1).split('&').forEach(function (entry) {
  58. var eq = entry.indexOf('=');
  59. if (eq >= 0) {
  60. parameters[decodeURIComponent(entry.slice(0, eq))] =
  61. decodeURIComponent(entry.slice(eq + 1));
  62. }
  63. });
  64. // if variables was provided, try to format it.
  65. if (parameters.variables) {
  66. try {
  67. parameters.variables =
  68. JSON.stringify(JSON.parse(parameters.variables), null, 2);
  69. } catch (e) {
  70. // Do nothing, we want to display the invalid JSON as a string, rather
  71. // than present an error.
  72. }
  73. }
  74. // When the query and variables string is edited, update the URL bar so
  75. // that it can be easily shared
  76. function onEditQuery(newQuery) {
  77. parameters.query = newQuery;
  78. updateURL();
  79. }
  80. function onEditVariables(newVariables) {
  81. parameters.variables = newVariables;
  82. updateURL();
  83. }
  84. function onEditOperationName(newOperationName) {
  85. parameters.operationName = newOperationName;
  86. updateURL();
  87. }
  88. function updateURL() {
  89. var newSearch = '?' + Object.keys(parameters).filter(function (key) {
  90. return Boolean(parameters[key]);
  91. }).map(function (key) {
  92. return encodeURIComponent(key) + '=' +
  93. encodeURIComponent(parameters[key]);
  94. }).join('&');
  95. history.replaceState(null, null, newSearch);
  96. }
  97. // Defines a GraphQL fetcher using the fetch API. You're not required to
  98. // use fetch, and could instead implement graphQLFetcher however you like,
  99. // as long as it returns a Promise or Observable.
  100. function graphQLFetcher(graphQLParams) {
  101. // When working locally, the example expects a GraphQL server at the path /graphql.
  102. // In a PR preview, it connects to the Star Wars API externally.
  103. // Change this to point wherever you host your GraphQL server.
  104. const isDev = !window.location.hostname.match(/(^|\.)netlify\.com$|(^|\.)graphql\.org$/)
  105. const api = isDev ? '/db/v1/graphql/main' : 'https://swapi.graph.cool/'
  106. return fetch(api, {
  107. method: 'post',
  108. headers: {
  109. 'Accept': 'application/json',
  110. 'Content-Type': 'application/json',
  111. },
  112. body: JSON.stringify(graphQLParams),
  113. credentials: 'include',
  114. }).then(function (response) {
  115. return response.text();
  116. }).then(function (responseBody) {
  117. try {
  118. return JSON.parse(responseBody);
  119. } catch (error) {
  120. return responseBody;
  121. }
  122. });
  123. }
  124. var subscriptionsURL = 'wss://' + location.hostname + ':' + location.port + '/db/v1/graphql-subscriptions/main';
  125. var subscriptionsClient = new window.SubscriptionsTransportWs.SubscriptionClient(subscriptionsURL, {
  126. reconnect: true
  127. });
  128. var fetcher = window.GraphiQLSubscriptionsFetcher.graphQLFetcher(subscriptionsClient, graphQLFetcher);
  129. // Render <GraphiQL /> into the body.
  130. // See the README in the top level of this module to learn more about
  131. // how you can customize GraphiQL by providing different values or
  132. // additional child elements.
  133. ReactDOM.render(
  134. React.createElement(GraphiQL, {
  135. fetcher: fetcher,
  136. query: parameters.query,
  137. variables: parameters.variables,
  138. operationName: parameters.operationName,
  139. onEditQuery: onEditQuery,
  140. onEditVariables: onEditVariables,
  141. onEditOperationName: onEditOperationName
  142. }),
  143. document.getElementById('graphiql')
  144. );
  145. </script>
  146. </body>
  147. </html>