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
This commit is contained in:
parent
ab2ea51aac
commit
02e9080094
1
go.mod
1
go.mod
@ -18,6 +18,7 @@ require (
|
|||||||
github.com/deckarep/golang-set v1.8.0
|
github.com/deckarep/golang-set v1.8.0
|
||||||
github.com/ethereum/go-ethereum v1.10.26
|
github.com/ethereum/go-ethereum v1.10.26
|
||||||
github.com/gibson042/canonicaljson-go v1.0.3
|
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/gogo/protobuf v1.3.3
|
||||||
github.com/golang/protobuf v1.5.2
|
github.com/golang/protobuf v1.5.2
|
||||||
github.com/gorilla/mux v1.8.0
|
github.com/gorilla/mux v1.8.0
|
||||||
|
2
go.sum
2
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/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/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.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 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-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||||
|
165
gql/graphiql.go
Normal file
165
gql/graphiql.go
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
package gql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"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 PlaygroundHandler(apiURL string) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != "GET" {
|
||||||
|
http.Error(w, "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(`
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<title>GraphiQL</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
height: 100%;
|
||||||
|
margin: 0;
|
||||||
|
width: 100%;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
#graphiql {
|
||||||
|
height: 100vh;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
This GraphiQL example depends on Promise and fetch, which are available in
|
||||||
|
modern browsers, but can be "polyfilled" for older browsers.
|
||||||
|
GraphiQL itself depends on React DOM.
|
||||||
|
If you do not want to rely on a CDN, you can host these files locally or
|
||||||
|
include them directly in your favored resource bundler.
|
||||||
|
-->
|
||||||
|
<script
|
||||||
|
src="https://unpkg.com/react@17/umd/react.development.js"
|
||||||
|
integrity="sha512-Vf2xGDzpqUOEIKO+X2rgTLWPY+65++WPwCHkX2nFMu9IcstumPsf/uKKRd5prX3wOu8Q0GBylRpsDB26R6ExOg=="
|
||||||
|
crossorigin="anonymous"
|
||||||
|
></script>
|
||||||
|
<script
|
||||||
|
src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"
|
||||||
|
integrity="sha512-Wr9OKCTtq1anK0hq5bY3X/AvDI5EflDSAh0mE9gma+4hl+kXdTJPKZ3TwLMBcrgUeoY0s3dq9JjhCQc7vddtFg=="
|
||||||
|
crossorigin="anonymous"
|
||||||
|
></script>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
These two files can be found in the npm module, however you may wish to
|
||||||
|
copy them directly into your environment, or perhaps include them in your
|
||||||
|
favored resource bundler.
|
||||||
|
-->
|
||||||
|
<link rel="stylesheet" href="https://unpkg.com/graphiql/graphiql.min.css" />
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div id="graphiql">Loading...</div>
|
||||||
|
<script
|
||||||
|
src="https://unpkg.com/graphiql/graphiql.min.js"
|
||||||
|
type="application/javascript"
|
||||||
|
></script>
|
||||||
|
<script>
|
||||||
|
// https://github.com/graphql/graphiql/blob/main/packages/graphiql/resources/renderExample.js
|
||||||
|
|
||||||
|
// Parse the search string to get url parameters.
|
||||||
|
var search = window.location.search;
|
||||||
|
var parameters = {};
|
||||||
|
search
|
||||||
|
.substr(1)
|
||||||
|
.split('&')
|
||||||
|
.forEach(function (entry) {
|
||||||
|
var eq = entry.indexOf('=');
|
||||||
|
if (eq >= 0) {
|
||||||
|
parameters[decodeURIComponent(entry.slice(0, eq))] = decodeURIComponent(
|
||||||
|
entry.slice(eq + 1),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// When the query and variables string is edited, update the URL bar so
|
||||||
|
// that it can be easily shared.
|
||||||
|
function onEditQuery(newQuery) {
|
||||||
|
parameters.query = newQuery;
|
||||||
|
updateURL();
|
||||||
|
}
|
||||||
|
|
||||||
|
function onEditVariables(newVariables) {
|
||||||
|
parameters.variables = newVariables;
|
||||||
|
updateURL();
|
||||||
|
}
|
||||||
|
|
||||||
|
function onEditHeaders(newHeaders) {
|
||||||
|
parameters.headers = newHeaders;
|
||||||
|
updateURL();
|
||||||
|
}
|
||||||
|
|
||||||
|
function onTabChange(tabsState) {
|
||||||
|
const activeTab = tabsState.tabs[tabsState.activeTabIndex];
|
||||||
|
parameters.query = activeTab.query;
|
||||||
|
parameters.variables = activeTab.variables;
|
||||||
|
parameters.headers = activeTab.headers;
|
||||||
|
updateURL();
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateURL() {
|
||||||
|
var newSearch =
|
||||||
|
'?' +
|
||||||
|
Object.keys(parameters)
|
||||||
|
.filter(function (key) {
|
||||||
|
return Boolean(parameters[key]);
|
||||||
|
})
|
||||||
|
.map(function (key) {
|
||||||
|
return (
|
||||||
|
encodeURIComponent(key) + '=' + encodeURIComponent(parameters[key])
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.join('&');
|
||||||
|
history.replaceState(null, null, newSearch);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render <GraphiQL /> into the body.
|
||||||
|
// See the README in the top level of this module to learn more about
|
||||||
|
// how you can customize GraphiQL by providing different values or
|
||||||
|
// additional child elements.
|
||||||
|
ReactDOM.render(
|
||||||
|
React.createElement(GraphiQL, {
|
||||||
|
fetcher: GraphiQL.createFetcher({
|
||||||
|
url: {{.apiURL}}
|
||||||
|
}),
|
||||||
|
query: parameters.query,
|
||||||
|
variables: parameters.variables,
|
||||||
|
headers: parameters.headers,
|
||||||
|
defaultHeaders: parameters.defaultHeaders,
|
||||||
|
onEditQuery: onEditQuery,
|
||||||
|
onEditVariables: onEditVariables,
|
||||||
|
onEditHeaders: onEditHeaders,
|
||||||
|
defaultEditorToolsVisibility: true,
|
||||||
|
isHeadersEditorEnabled: true,
|
||||||
|
shouldPersistHeaders: true,
|
||||||
|
onTabChange,
|
||||||
|
}),
|
||||||
|
document.getElementById('graphiql'),
|
||||||
|
);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`))
|
@ -5,9 +5,10 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/99designs/gqlgen/graphql/handler"
|
"github.com/99designs/gqlgen/graphql/handler"
|
||||||
"github.com/99designs/gqlgen/graphql/playground"
|
|
||||||
"github.com/cosmos/cosmos-sdk/client"
|
"github.com/cosmos/cosmos-sdk/client"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
"github.com/rs/cors"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,6 +17,16 @@ func Server(ctx client.Context) {
|
|||||||
if !viper.GetBool("gql-server") {
|
if !viper.GetBool("gql-server") {
|
||||||
return
|
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")
|
logFile := viper.GetString("log-file")
|
||||||
|
|
||||||
port := viper.GetString("gql-port")
|
port := viper.GetString("gql-port")
|
||||||
@ -25,20 +36,20 @@ func Server(ctx client.Context) {
|
|||||||
logFile: logFile,
|
logFile: logFile,
|
||||||
}}))
|
}}))
|
||||||
|
|
||||||
http.Handle("/", playground.Handler("GraphQL playground", "/api"))
|
router.Handle("/", PlaygroundHandler("/api"))
|
||||||
|
|
||||||
if viper.GetBool("gql-playground") {
|
if viper.GetBool("gql-playground") {
|
||||||
apiBase := viper.GetString("gql-playground-api-base")
|
apiBase := viper.GetString("gql-playground-api-base")
|
||||||
|
|
||||||
http.Handle("/webui", playground.Handler("GraphQL playground", apiBase+"/api"))
|
router.Handle("/webui", PlaygroundHandler(apiBase+"/api"))
|
||||||
http.Handle("/console", playground.Handler("GraphQL playground", apiBase+"/graphql"))
|
router.Handle("/console", PlaygroundHandler(apiBase+"/graphql"))
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Handle("/api", srv)
|
router.Handle("/api", srv)
|
||||||
http.Handle("/graphql", srv)
|
router.Handle("/graphql", srv)
|
||||||
|
|
||||||
log.Info("Connect to GraphQL playground", "url", fmt.Sprintf("http://localhost:%s", port))
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -111,7 +111,7 @@ func (k Keeper) GetRecord(ctx sdk.Context, id string) (record types.Record) {
|
|||||||
store := ctx.KVStore(k.storeKey)
|
store := ctx.KVStore(k.storeKey)
|
||||||
result := store.Get(GetRecordIndexKey(id))
|
result := store.Get(GetRecordIndexKey(id))
|
||||||
k.cdc.MustUnmarshal(result, &record)
|
k.cdc.MustUnmarshal(result, &record)
|
||||||
return record
|
return recordObjToRecord(store, record)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListRecords - get all records.
|
// ListRecords - get all records.
|
||||||
|
Loading…
Reference in New Issue
Block a user