From 83b41cc7d680811685889f0da7a571121783dc1f Mon Sep 17 00:00:00 2001 From: nabarun Date: Mon, 12 Dec 2022 13:31:19 +0530 Subject: [PATCH 1/2] Implement loading GQL query from URL --- 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..59b08161 --- /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 e06686e0ba0594b8babf105bed1e82a4f64a27d2 Mon Sep 17 00:00:00 2001 From: nabarun Date: Mon, 12 Dec 2022 13:45:02 +0530 Subject: [PATCH 2/2] Fix variable name lint error --- gql/graphiql.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gql/graphiql.go b/gql/graphiql.go index 59b08161..75c6a3b8 100644 --- a/gql/graphiql.go +++ b/gql/graphiql.go @@ -25,7 +25,7 @@ func errorJSON(msg string) []byte { return buf.Bytes() } -func PlaygroundHandler(apiUrl string) http.HandlerFunc { +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) @@ -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) @@ -159,7 +159,7 @@ var page = template.Must(template.New("graphiql").Parse(` React.createElement(GraphiQL, { fetcher: GraphiQL.createFetcher({ // subscriptionUrl: 'ws://localhost:8081/subscriptions', - url: {{.apiUrl}} + url: {{.apiURL}} }), query: parameters.query, variables: parameters.variables, -- 2.45.2