service.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package graphql
  17. import (
  18. "encoding/json"
  19. "net/http"
  20. "github.com/ethereum/go-ethereum/internal/ethapi"
  21. "github.com/ethereum/go-ethereum/node"
  22. "github.com/graph-gophers/graphql-go"
  23. )
  24. type handler struct {
  25. Schema *graphql.Schema
  26. }
  27. func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. var params struct {
  29. Query string `json:"query"`
  30. OperationName string `json:"operationName"`
  31. Variables map[string]interface{} `json:"variables"`
  32. }
  33. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  34. http.Error(w, err.Error(), http.StatusBadRequest)
  35. return
  36. }
  37. response := h.Schema.Exec(r.Context(), params.Query, params.OperationName, params.Variables)
  38. responseJSON, err := json.Marshal(response)
  39. if err != nil {
  40. http.Error(w, err.Error(), http.StatusInternalServerError)
  41. return
  42. }
  43. if len(response.Errors) > 0 {
  44. w.WriteHeader(http.StatusBadRequest)
  45. }
  46. w.Header().Set("Content-Type", "application/json")
  47. w.Write(responseJSON)
  48. }
  49. // New constructs a new GraphQL service instance.
  50. func New(stack *node.Node, backend ethapi.Backend, cors, vhosts []string) error {
  51. if backend == nil {
  52. panic("missing backend")
  53. }
  54. // check if http server with given endpoint exists and enable graphQL on it
  55. return newHandler(stack, backend, cors, vhosts)
  56. }
  57. // newHandler returns a new `http.Handler` that will answer GraphQL queries.
  58. // It additionally exports an interactive query browser on the / endpoint.
  59. func newHandler(stack *node.Node, backend ethapi.Backend, cors, vhosts []string) error {
  60. q := Resolver{backend}
  61. s, err := graphql.ParseSchema(schema, &q)
  62. if err != nil {
  63. return err
  64. }
  65. h := handler{Schema: s}
  66. handler := node.NewHTTPHandlerStack(h, cors, vhosts)
  67. stack.RegisterHandler("GraphQL UI", "/graphql/ui", GraphiQL{})
  68. stack.RegisterHandler("GraphQL", "/graphql", handler)
  69. stack.RegisterHandler("GraphQL", "/graphql/", handler)
  70. return nil
  71. }