graphiql.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2016 Muhammed Thanish
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. package graphql
  23. import (
  24. "bytes"
  25. "fmt"
  26. "net/http"
  27. )
  28. // GraphiQL is an in-browser IDE for exploring GraphiQL APIs.
  29. // This handler returns GraphiQL when requested.
  30. //
  31. // For more information, see https://github.com/graphql/graphiql.
  32. type GraphiQL struct{}
  33. func respond(w http.ResponseWriter, body []byte, code int) {
  34. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  35. w.Header().Set("X-Content-Type-Options", "nosniff")
  36. w.WriteHeader(code)
  37. _, _ = w.Write(body)
  38. }
  39. func errorJSON(msg string) []byte {
  40. buf := bytes.Buffer{}
  41. fmt.Fprintf(&buf, `{"error": "%s"}`, msg)
  42. return buf.Bytes()
  43. }
  44. func (h GraphiQL) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  45. if r.Method != "GET" {
  46. respond(w, errorJSON("only GET requests are supported"), http.StatusMethodNotAllowed)
  47. return
  48. }
  49. w.Write(graphiql)
  50. }
  51. var graphiql = []byte(`
  52. <!DOCTYPE html>
  53. <html>
  54. <head>
  55. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.11.11/graphiql.css"/>
  56. <script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.3/fetch.min.js"></script>
  57. <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.2.0/umd/react.production.min.js"></script>
  58. <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/umd/react-dom.production.min.js"></script>
  59. <script src="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.11.11/graphiql.min.js"></script>
  60. </head>
  61. <body style="width: 100%; height: 100%; margin: 0; overflow: hidden;">
  62. <div id="graphiql" style="height: 100vh;">Loading...</div>
  63. <script>
  64. function fetchGQL(params) {
  65. return fetch("/graphql", {
  66. method: "post",
  67. body: JSON.stringify(params),
  68. credentials: "include",
  69. }).then(function (resp) {
  70. return resp.text();
  71. }).then(function (body) {
  72. try {
  73. return JSON.parse(body);
  74. } catch (error) {
  75. return body;
  76. }
  77. });
  78. }
  79. ReactDOM.render(
  80. React.createElement(GraphiQL, {fetcher: fetchGQL}),
  81. document.getElementById("graphiql")
  82. )
  83. </script>
  84. </body>
  85. </html>
  86. `)