erigon-pulse/cmd/rpcdaemon/graphql/graphql.go
Stéphane Loeuillet 195a72bf74
Initial GraphQL interface implementation (#6821)
That's an initial PR mostly for code review, not ready for production
use

Got basic GraphQL working when querying a single block

---------

Co-authored-by: Alex Sharov <AskAlexSharov@gmail.com>
2023-02-20 11:23:06 +00:00

55 lines
1.1 KiB
Go

package graphql
import (
"net/http"
"strings"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/commands"
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/graphql/graph"
"github.com/ledgerwatch/erigon/rpc"
)
const (
urlPath = "/graphql"
)
func CreateHandler(api []rpc.API) *handler.Server {
var graphqlAPI commands.GraphQLAPI
for _, rpc := range api {
if rpc.Service == nil {
continue
}
if graphqlCandidate, ok := rpc.Service.(commands.GraphQLAPI); ok {
graphqlAPI = graphqlCandidate
}
}
resolver := graph.Resolver{}
resolver.GraphQLAPI = graphqlAPI
return handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &resolver})) // TODO : init resolver.DB here !!!
}
func ProcessGraphQLcheckIfNeeded(
graphQLHandler http.Handler,
w http.ResponseWriter,
r *http.Request,
) bool {
if strings.EqualFold(r.URL.Path, urlPath) {
graphQLHandler.ServeHTTP(w, r)
return true
}
if strings.EqualFold(r.URL.Path, urlPath+"/ui") {
playground.Handler("GraphQL playground", "/graphql").ServeHTTP(w, r)
return true
}
return false
}