service.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/eth/filters"
  21. "github.com/ethereum/go-ethereum/internal/ethapi"
  22. "github.com/ethereum/go-ethereum/node"
  23. "github.com/graph-gophers/graphql-go"
  24. )
  25. type handler struct {
  26. Schema *graphql.Schema
  27. }
  28. func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  29. var params struct {
  30. Query string `json:"query"`
  31. OperationName string `json:"operationName"`
  32. Variables map[string]interface{} `json:"variables"`
  33. }
  34. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  35. http.Error(w, err.Error(), http.StatusBadRequest)
  36. return
  37. }
  38. response := h.Schema.Exec(r.Context(), params.Query, params.OperationName, params.Variables)
  39. responseJSON, err := json.Marshal(response)
  40. if err != nil {
  41. http.Error(w, err.Error(), http.StatusInternalServerError)
  42. return
  43. }
  44. if len(response.Errors) > 0 {
  45. w.WriteHeader(http.StatusBadRequest)
  46. }
  47. w.Header().Set("Content-Type", "application/json")
  48. w.Write(responseJSON)
  49. }
  50. // New constructs a new GraphQL service instance.
  51. func New(stack *node.Node, backend ethapi.Backend, filterSystem *filters.FilterSystem, cors, vhosts []string) error {
  52. return newHandler(stack, backend, filterSystem, cors, vhosts)
  53. }
  54. // newHandler returns a new `http.Handler` that will answer GraphQL queries.
  55. // It additionally exports an interactive query browser on the / endpoint.
  56. func newHandler(stack *node.Node, backend ethapi.Backend, filterSystem *filters.FilterSystem, cors, vhosts []string) error {
  57. q := Resolver{backend, filterSystem}
  58. s, err := graphql.ParseSchema(schema, &q)
  59. if err != nil {
  60. return err
  61. }
  62. h := handler{Schema: s}
  63. handler := node.NewHTTPHandlerStack(h, cors, vhosts, nil)
  64. stack.RegisterHandler("GraphQL UI", "/graphql/ui", GraphiQL{})
  65. stack.RegisterHandler("GraphQL", "/graphql", handler)
  66. stack.RegisterHandler("GraphQL", "/graphql/", handler)
  67. return nil
  68. }