Sistema de Gestión Documental
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.4 KiB

5 years ago
  1. var initParameters = {};
  2. var entrypoint = null;
  3. function onEditQuery(newQuery) {
  4. initParameters.query = newQuery;
  5. updateURL();
  6. }
  7. function onEditVariables(newVariables) {
  8. initParameters.variables = newVariables;
  9. updateURL();
  10. }
  11. function onEditOperationName(newOperationName) {
  12. initParameters.operationName = newOperationName;
  13. updateURL();
  14. }
  15. function updateURL() {
  16. var newSearch = '?' + Object.keys(initParameters).filter(function (key) {
  17. return Boolean(initParameters[key]);
  18. }).map(function (key) {
  19. return encodeURIComponent(key) + '=' + encodeURIComponent(initParameters[key]);
  20. }).join('&');
  21. history.replaceState(null, null, newSearch);
  22. }
  23. function graphQLFetcher(graphQLParams) {
  24. return fetch(entrypoint, {
  25. method: 'post',
  26. headers: {
  27. 'Accept': 'application/json',
  28. 'Content-Type': 'application/json'
  29. },
  30. body: JSON.stringify(graphQLParams),
  31. credentials: 'include'
  32. }).then(function (response) {
  33. return response.text();
  34. }).then(function (responseBody) {
  35. try {
  36. return JSON.parse(responseBody);
  37. } catch (error) {
  38. return responseBody;
  39. }
  40. });
  41. }
  42. window.onload = function() {
  43. var data = JSON.parse(document.getElementById('graphiql-data').innerText);
  44. entrypoint = data.entrypoint;
  45. var search = window.location.search;
  46. search.substr(1).split('&').forEach(function (entry) {
  47. var eq = entry.indexOf('=');
  48. if (eq >= 0) {
  49. initParameters[decodeURIComponent(entry.slice(0, eq))] = decodeURIComponent(entry.slice(eq + 1));
  50. }
  51. });
  52. if (initParameters.variables) {
  53. try {
  54. initParameters.variables = JSON.stringify(JSON.parse(initParameters.variables), null, 2);
  55. } catch (e) {
  56. // Do nothing, we want to display the invalid JSON as a string, rather than present an error.
  57. }
  58. }
  59. ReactDOM.render(
  60. React.createElement(GraphiQL, {
  61. fetcher: graphQLFetcher,
  62. query: initParameters.query,
  63. variables: initParameters.variables,
  64. operationName: initParameters.operationName,
  65. onEditQuery: onEditQuery,
  66. onEditVariables: onEditVariables,
  67. onEditOperationName: onEditOperationName
  68. }),
  69. document.getElementById('graphiql')
  70. );
  71. }