Setup and integrate GQL server #12

Merged
ashwin merged 3 commits from pm-setup-gql into main 2024-02-29 12:45:39 +00:00
4 changed files with 24 additions and 8 deletions
Showing only changes of commit 5155fbe533 - Show all commits

View File

@ -48,7 +48,7 @@ func initRootCmd(rootCmd *cobra.Command, txConfig client.TxConfig, basicManager
newStartCmd := server.StartCmdWithOptions(newApp, app.DefaultNodeHome, server.StartCmdOptions{ newStartCmd := server.StartCmdWithOptions(newApp, app.DefaultNodeHome, server.StartCmdOptions{
PostSetup: func(svrCtx *server.Context, clientCtx client.Context, ctx context.Context, g *errgroup.Group) error { PostSetup: func(svrCtx *server.Context, clientCtx client.Context, ctx context.Context, g *errgroup.Group) error {
g.Go(func() error { g.Go(func() error {
return gql.Server(clientCtx) return gql.Server(ctx, clientCtx, svrCtx.Logger.With("module", "gql-server"))
}) })
return nil return nil

View File

@ -107,7 +107,7 @@ func NewRootCmd() *cobra.Command {
// overwrite the block timeout // overwrite the block timeout
cmtCfg := cmtcfg.DefaultConfig() cmtCfg := cmtcfg.DefaultConfig()
cmtCfg.Consensus.TimeoutCommit = 3 * time.Second cmtCfg.Consensus.TimeoutCommit = 3 * time.Second
cmtCfg.LogLevel = "*:error,p2p:info,state:info,auction:info,bond:info,registry:info" // better default logging cmtCfg.LogLevel = "*:error,p2p:info,state:info,auction:info,bond:info,registry:info,gql-server:info" // better default logging
return server.InterceptConfigsPreRunHandler(cmd, serverconfig.DefaultConfigTemplate, srvCfg, cmtCfg) return server.InterceptConfigsPreRunHandler(cmd, serverconfig.DefaultConfigTemplate, srvCfg, cmtCfg)
}, },

View File

@ -1004,7 +1004,7 @@ func (ec *executionContext) introspectType(name string) (*introspection.Type, er
return introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name]), nil return introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name]), nil
} }
//go:embed "cerc-io/laconicd/schema.graphql" //go:embed "cerc-io/laconic2d/schema.graphql"
var sourcesFS embed.FS var sourcesFS embed.FS
func sourceData(filename string) string { func sourceData(filename string) string {
@ -1016,7 +1016,7 @@ func sourceData(filename string) string {
} }
var sources = []*ast.Source{ var sources = []*ast.Source{
{Name: "cerc-io/laconicd/schema.graphql", Input: sourceData("cerc-io/laconicd/schema.graphql"), BuiltIn: false}, {Name: "cerc-io/laconic2d/schema.graphql", Input: sourceData("cerc-io/laconic2d/schema.graphql"), BuiltIn: false},
} }
var parsedSchema = gqlparser.MustLoadSchema(sources...) var parsedSchema = gqlparser.MustLoadSchema(sources...)

View File

@ -1,9 +1,11 @@
package gql package gql
import ( import (
"context"
"fmt" "fmt"
"net/http" "net/http"
"cosmossdk.io/log"
"github.com/99designs/gqlgen/graphql/handler" "github.com/99designs/gqlgen/graphql/handler"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/rs/cors" "github.com/rs/cors"
@ -13,7 +15,7 @@ import (
) )
// Server configures and starts the GQL server. // Server configures and starts the GQL server.
func Server(ctx client.Context) error { func Server(ctx context.Context, clientCtx client.Context, logger log.Logger) error {
if !viper.GetBool("gql-server") { if !viper.GetBool("gql-server") {
return nil return nil
} }
@ -32,7 +34,7 @@ func Server(ctx client.Context) error {
port := viper.GetString("gql-port") port := viper.GetString("gql-port")
srv := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: &Resolver{ srv := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: &Resolver{
ctx: ctx, ctx: clientCtx,
logFile: logFile, logFile: logFile,
}})) }}))
@ -48,6 +50,20 @@ func Server(ctx client.Context) error {
router.Handle("/api", srv) router.Handle("/api", srv)
router.Handle("/graphql", srv) router.Handle("/graphql", srv)
fmt.Println("Connect to GraphQL playground", "url", fmt.Sprintf("http://localhost:%s", port)) errCh := make(chan error)
return http.ListenAndServe(":"+port, router) //nolint: all
go func() {
logger.Info(fmt.Sprintf("Connect to GraphQL playground url: http://localhost:%s", port))
errCh <- http.ListenAndServe(":"+port, router)
}()
select {
case <-ctx.Done():
// Gracefully stop the GQL server.
logger.Info("Stopping GQL server...")
return nil
case err := <-errCh:
logger.Error(fmt.Sprintf("Failed to start GQL server: %s", err))
return err
}
} }