mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-17 01:17:43 +00:00
Add setting graphql server endpoint via environment variable.
This commit is contained in:
@@ -4,6 +4,7 @@ go 1.16
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/lukaszraczylo/zero v0.0.0-20160322155811-4a4535dd56e7 // indirect
|
||||||
github.com/stretchr/testify v1.7.0
|
github.com/stretchr/testify v1.7.0
|
||||||
github.com/tidwall/gjson v1.7.3
|
github.com/tidwall/gjson v1.7.3
|
||||||
github.com/valyala/fasthttp v1.22.0
|
github.com/valyala/fasthttp v1.22.0
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
|||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/klauspost/compress v1.11.8 h1:difgzQsp5mdAz9v8lm3P/I+EpDKMU/6uTMw1y1FObuo=
|
github.com/klauspost/compress v1.11.8 h1:difgzQsp5mdAz9v8lm3P/I+EpDKMU/6uTMw1y1FObuo=
|
||||||
github.com/klauspost/compress v1.11.8/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
|
github.com/klauspost/compress v1.11.8/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
|
||||||
|
github.com/lukaszraczylo/zero v0.0.0-20160322155811-4a4535dd56e7 h1:9l31ZdbJuSK5sQH4hldeHB/ulflee5srCFzT4Oxm65Q=
|
||||||
|
github.com/lukaszraczylo/zero v0.0.0-20160322155811-4a4535dd56e7/go.mod h1:UOwgylrs4V/Vzo1KfbjhzU+iB/5Pu4HnSpgE8WI14gs=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
|
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
|
||||||
|
|||||||
@@ -9,12 +9,16 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/lukaszraczylo/zero"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Endpoint of your GraphQL server to query
|
// Endpoint of your GraphQL server to query
|
||||||
|
// this variable can be overwritten by setting env variable, for example:
|
||||||
|
// GRAPHQL_ENDPOINT=http://hasura.local/v1/graphql
|
||||||
var GraphQLUrl = "http://127.0.0.1:9090/v1/graphql"
|
var GraphQLUrl = "http://127.0.0.1:9090/v1/graphql"
|
||||||
|
|
||||||
type requestBase struct {
|
type requestBase struct {
|
||||||
@@ -22,6 +26,12 @@ type requestBase struct {
|
|||||||
Variables interface{} `json:"variables"`
|
Variables interface{} `json:"variables"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if !zero.IsZero(os.Getenv("GRAPHQL_ENDPOINT")) {
|
||||||
|
GraphQLUrl = os.Getenv("GRAPHQL_ENDPOINT")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// queryBuilder takes query data (string) and variables (interface) as a parameter and assembles
|
// queryBuilder takes query data (string) and variables (interface) as a parameter and assembles
|
||||||
// graphQL query. It also compacts the JSON result. Function returns query as []byte and error if anything went wrong.
|
// graphQL query. It also compacts the JSON result. Function returns query as []byte and error if anything went wrong.
|
||||||
func queryBuilder(data string, variables interface{}) ([]byte, error) {
|
func queryBuilder(data string, variables interface{}) ([]byte, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user