bsc/vendor/github.com/graph-gophers/graphql-go/relay/relay.go

71 lines
1.6 KiB
Go
Raw Normal View History

GraphQL master FF for review (#18445) * Initial work on a graphql API * Added receipts, and more transaction fields. * Finish receipts, add logs * Add transactionCount to block * Add types and . * Update Block type to be compatible with ethql * Rename nonce to transactionCount in Account, to be compatible with ethql * Update transaction, receipt and log to match ethql * Add query operator, for a range of blocks * Added ommerCount to Block * Add transactionAt and ommerAt to Block * Added sendRawTransaction mutation * Add Call and EstimateGas to graphQL API * Refactored to use hexutil.Bytes instead of HexBytes * Replace BigNum with hexutil.Big * Refactor call and estimateGas to use ethapi struct type * Replace ethgraphql.Address with common.Address * Replace ethgraphql.Hash with common.Hash * Converted most quantities to Long instead of Int * Add support for logs * Fix bug in runFilter * Restructured Transaction to work primarily with headers, so uncle data is reported properly * Add gasPrice API * Add protocolVersion API * Add syncing API * Moved schema into its own source file * Move some single use args types into anonymous structs * Add doc-comments * Fixed backend fetching to use context * Added (very) basic tests * Add documentation to the graphql schema * Fix reversion for formatting of big numbers * Correct spelling error * s/BigInt/Long/ * Update common/types.go * Fixes in response to review * Fix lint error * Updated calls on private functions * Fix typo in graphql.go * Rollback ethapi breaking changes for graphql support Co-Authored-By: Arachnid <arachnid@notdot.net>
2019-01-21 17:38:13 +03:00
package relay
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
graphql "github.com/graph-gophers/graphql-go"
)
func MarshalID(kind string, spec interface{}) graphql.ID {
d, err := json.Marshal(spec)
if err != nil {
panic(fmt.Errorf("relay.MarshalID: %s", err))
}
return graphql.ID(base64.URLEncoding.EncodeToString(append([]byte(kind+":"), d...)))
}
func UnmarshalKind(id graphql.ID) string {
s, err := base64.URLEncoding.DecodeString(string(id))
if err != nil {
return ""
}
i := strings.IndexByte(string(s), ':')
if i == -1 {
return ""
}
return string(s[:i])
}
func UnmarshalSpec(id graphql.ID, v interface{}) error {
s, err := base64.URLEncoding.DecodeString(string(id))
if err != nil {
return err
}
i := strings.IndexByte(string(s), ':')
if i == -1 {
return errors.New("invalid graphql.ID")
}
return json.Unmarshal([]byte(s[i+1:]), v)
}
type Handler struct {
Schema *graphql.Schema
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var params struct {
Query string `json:"query"`
OperationName string `json:"operationName"`
Variables map[string]interface{} `json:"variables"`
}
if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
response := h.Schema.Exec(r.Context(), params.Query, params.OperationName, params.Variables)
responseJSON, err := json.Marshal(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(responseJSON)
}