laconicd/gql/server.go
Nabarun Gogoi 02e9080094
Changes to run laconic-console app with laconicd (#75)
* Add cors middleware for GQL requests (#65)

* Implement loading query from URL in GQL playground (#66)

* Implement loading GQL query from URL

* Fix variable name lint error

* Load record names in `getRecordsByIds` GQL query (#67)

* Fix variable rename

* Load names in getRecordsByIds GQL query

* CI Review fixes

* Make init script executable
2023-01-11 13:06:42 +05:30

57 lines
1.3 KiB
Go

package gql
import (
"fmt"
"net/http"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/cosmos/cosmos-sdk/client"
"github.com/ethereum/go-ethereum/log"
"github.com/go-chi/chi/v5"
"github.com/rs/cors"
"github.com/spf13/viper"
)
// Server configures and starts the GQL server.
func Server(ctx client.Context) {
if !viper.GetBool("gql-server") {
return
}
router := chi.NewRouter()
// Add CORS middleware around every request
// See https://github.com/rs/cors for full option listing
router.Use(cors.New(cors.Options{
AllowedOrigins: []string{"*"},
Debug: false,
}).Handler)
logFile := viper.GetString("log-file")
port := viper.GetString("gql-port")
srv := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: &Resolver{
ctx: ctx,
logFile: logFile,
}}))
router.Handle("/", PlaygroundHandler("/api"))
if viper.GetBool("gql-playground") {
apiBase := viper.GetString("gql-playground-api-base")
router.Handle("/webui", PlaygroundHandler(apiBase+"/api"))
router.Handle("/console", PlaygroundHandler(apiBase+"/graphql"))
}
router.Handle("/api", srv)
router.Handle("/graphql", srv)
log.Info("Connect to GraphQL playground", "url", fmt.Sprintf("http://localhost:%s", port))
err := http.ListenAndServe(":"+port, router) //nolint: all
if err != nil {
panic(err)
}
}