mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-23 12:07:17 +00:00
195a72bf74
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>
55 lines
1.1 KiB
Go
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
|
|
}
|