github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/graphql/service.go (about)

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