From c656d88c76de087f3aec618a36278d8dcb71b6b5 Mon Sep 17 00:00:00 2001 From: nikugogoi Date: Fri, 9 Dec 2022 13:22:38 +0530 Subject: [PATCH 1/5] Add cors middleware for GQL requests (#65) --- go.mod | 1 + go.sum | 2 ++ gql/server.go | 24 ++++++++++++++++++------ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 2e6912e9..7e368854 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( github.com/deckarep/golang-set v1.8.0 github.com/ethereum/go-ethereum v1.10.26 github.com/gibson042/canonicaljson-go v1.0.3 + github.com/go-chi/chi/v5 v5.0.7 github.com/gogo/protobuf v1.3.3 github.com/golang/protobuf v1.5.2 github.com/gorilla/mux v1.8.0 diff --git a/go.sum b/go.sum index 26ee95c6..de7d249d 100644 --- a/go.sum +++ b/go.sum @@ -367,6 +367,8 @@ github.com/gin-gonic/gin v1.7.0/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjX github.com/glycerine/go-unsnap-stream v0.0.0-20180323001048-9f0cb55181dd/go.mod h1:/20jfyN9Y5QPEAprSgKAUr+glWDY39ZiUEAYOEv5dsE= github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24= github.com/go-chi/chi/v5 v5.0.0/go.mod h1:BBug9lr0cqtdAhsu6R4AAdvufI0/XBzAQSsUqJpoZOs= +github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8= +github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= diff --git a/gql/server.go b/gql/server.go index dd5fe5a7..7eb82c7e 100644 --- a/gql/server.go +++ b/gql/server.go @@ -8,6 +8,8 @@ import ( "github.com/99designs/gqlgen/graphql/playground" "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" ) @@ -16,6 +18,16 @@ 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") @@ -25,20 +37,20 @@ func Server(ctx client.Context) { logFile: logFile, }})) - http.Handle("/", playground.Handler("GraphQL playground", "/api")) + router.Handle("/", playground.Handler("GraphQL playground", "/api")) if viper.GetBool("gql-playground") { apiBase := viper.GetString("gql-playground-api-base") - http.Handle("/webui", playground.Handler("GraphQL playground", apiBase+"/api")) - http.Handle("/console", playground.Handler("GraphQL playground", apiBase+"/graphql")) + router.Handle("/webui", playground.Handler("GraphQL playground", apiBase+"/api")) + router.Handle("/console", playground.Handler("GraphQL playground", apiBase+"/graphql")) } - http.Handle("/api", srv) - http.Handle("/graphql", srv) + 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, nil) //nolint: all + err := http.ListenAndServe(":"+port, router) //nolint: all if err != nil { panic(err) } -- 2.45.2 From d145cc2e2507a6c98ae17651450b65157ddca277 Mon Sep 17 00:00:00 2001 From: nikugogoi Date: Mon, 12 Dec 2022 13:51:53 +0530 Subject: [PATCH 2/5] Implement loading query from URL in GQL playground (#66) * Implement loading GQL query from URL * Fix variable name lint error --- gql/graphiql.go | 181 ++++++++++++++++++++++++++++++++++++++++++++++++ gql/server.go | 7 +- 2 files changed, 184 insertions(+), 4 deletions(-) create mode 100644 gql/graphiql.go diff --git a/gql/graphiql.go b/gql/graphiql.go new file mode 100644 index 00000000..75c6a3b8 --- /dev/null +++ b/gql/graphiql.go @@ -0,0 +1,181 @@ +package gql + +import ( + "bytes" + "fmt" + "html/template" + "net/http" +) + +// GraphiQL is an in-browser IDE for exploring GraphiQL APIs. +// This handler returns GraphiQL when requested. +// +// For more information, see https://github.com/graphql/graphiql. + +func respond(w http.ResponseWriter, body []byte, code int) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.Header().Set("X-Content-Type-Options", "nosniff") + w.WriteHeader(code) + _, _ = w.Write(body) +} + +func errorJSON(msg string) []byte { + buf := bytes.Buffer{} + fmt.Fprintf(&buf, `{"error": "%s"}`, msg) + return buf.Bytes() +} + +func PlaygroundHandler(apiURL string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + respond(w, errorJSON("only GET requests are supported"), http.StatusMethodNotAllowed) + return + } + w.Header().Set("Content-Type", "text/html") + err := page.Execute(w, map[string]interface{}{ + "apiUrl": apiURL, + }) + if err != nil { + panic(err) + } + } +} + +// https://github.com/graphql/graphiql/blob/main/examples/graphiql-cdn/index.html +var page = template.Must(template.New("graphiql").Parse(` + + + + GraphiQL + + + + + + + + + + + +
Loading...
+ + + + +`)) diff --git a/gql/server.go b/gql/server.go index 7eb82c7e..32337856 100644 --- a/gql/server.go +++ b/gql/server.go @@ -5,7 +5,6 @@ import ( "net/http" "github.com/99designs/gqlgen/graphql/handler" - "github.com/99designs/gqlgen/graphql/playground" "github.com/cosmos/cosmos-sdk/client" "github.com/ethereum/go-ethereum/log" "github.com/go-chi/chi/v5" @@ -37,13 +36,13 @@ func Server(ctx client.Context) { logFile: logFile, }})) - router.Handle("/", playground.Handler("GraphQL playground", "/api")) + router.Handle("/", PlaygroundHandler("/api")) if viper.GetBool("gql-playground") { apiBase := viper.GetString("gql-playground-api-base") - router.Handle("/webui", playground.Handler("GraphQL playground", apiBase+"/api")) - router.Handle("/console", playground.Handler("GraphQL playground", apiBase+"/graphql")) + router.Handle("/webui", PlaygroundHandler(apiBase+"/api")) + router.Handle("/console", PlaygroundHandler(apiBase+"/graphql")) } router.Handle("/api", srv) -- 2.45.2 From 3dd433cbef3c6b05313a663b533ca517a4e14b48 Mon Sep 17 00:00:00 2001 From: nikugogoi Date: Mon, 12 Dec 2022 15:48:03 +0530 Subject: [PATCH 3/5] Load record names in `getRecordsByIds` GQL query (#67) * Fix variable rename * Load names in getRecordsByIds GQL query --- gql/graphiql.go | 2 +- x/registry/keeper/keeper.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gql/graphiql.go b/gql/graphiql.go index 75c6a3b8..92bf2eb8 100644 --- a/gql/graphiql.go +++ b/gql/graphiql.go @@ -33,7 +33,7 @@ func PlaygroundHandler(apiURL string) http.HandlerFunc { } w.Header().Set("Content-Type", "text/html") err := page.Execute(w, map[string]interface{}{ - "apiUrl": apiURL, + "apiURL": apiURL, }) if err != nil { panic(err) diff --git a/x/registry/keeper/keeper.go b/x/registry/keeper/keeper.go index eabfe20e..e7891658 100644 --- a/x/registry/keeper/keeper.go +++ b/x/registry/keeper/keeper.go @@ -111,7 +111,7 @@ func (k Keeper) GetRecord(ctx sdk.Context, id string) (record types.Record) { store := ctx.KVStore(k.storeKey) result := store.Get(GetRecordIndexKey(id)) k.cdc.MustUnmarshal(result, &record) - return record + return recordObjToRecord(store, record) } // ListRecords - get all records. -- 2.45.2 From ea76ad75a58b7e868df40323dc5d3c35aa0e1359 Mon Sep 17 00:00:00 2001 From: nabarun Date: Wed, 11 Jan 2023 12:05:41 +0530 Subject: [PATCH 4/5] CI Review fixes --- gql/graphiql.go | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/gql/graphiql.go b/gql/graphiql.go index 92bf2eb8..32f4b85b 100644 --- a/gql/graphiql.go +++ b/gql/graphiql.go @@ -1,8 +1,6 @@ package gql import ( - "bytes" - "fmt" "html/template" "net/http" ) @@ -12,23 +10,10 @@ import ( // // For more information, see https://github.com/graphql/graphiql. -func respond(w http.ResponseWriter, body []byte, code int) { - w.Header().Set("Content-Type", "application/json; charset=utf-8") - w.Header().Set("X-Content-Type-Options", "nosniff") - w.WriteHeader(code) - _, _ = w.Write(body) -} - -func errorJSON(msg string) []byte { - buf := bytes.Buffer{} - fmt.Fprintf(&buf, `{"error": "%s"}`, msg) - return buf.Bytes() -} - func PlaygroundHandler(apiURL string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { - respond(w, errorJSON("only GET requests are supported"), http.StatusMethodNotAllowed) + http.Error(w, "only GET requests are supported", http.StatusMethodNotAllowed) return } w.Header().Set("Content-Type", "text/html") @@ -158,7 +143,6 @@ var page = template.Must(template.New("graphiql").Parse(` ReactDOM.render( React.createElement(GraphiQL, { fetcher: GraphiQL.createFetcher({ - // subscriptionUrl: 'ws://localhost:8081/subscriptions', url: {{.apiURL}} }), query: parameters.query, -- 2.45.2 From abd82dcb1e1e64945c61d3b9adc9a9707dfb1a53 Mon Sep 17 00:00:00 2001 From: nabarun Date: Wed, 11 Jan 2023 12:42:35 +0530 Subject: [PATCH 5/5] Make init script executable --- init.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 init.sh diff --git a/init.sh b/init.sh old mode 100644 new mode 100755 -- 2.45.2