Fix graphql endpoint setup order.

This commit is contained in:
2021-05-12 10:34:32 +01:00
parent 3e2dd803f1
commit abff330394
2 changed files with 38 additions and 14 deletions
+7 -3
View File
@@ -27,9 +27,13 @@ type requestBase struct {
func prepare() { func prepare() {
value, present := os.LookupEnv("GRAPHQL_ENDPOINT") value, present := os.LookupEnv("GRAPHQL_ENDPOINT")
if present { if GraphQLUrl == "" && present {
GraphQLUrl = value if present {
} else { GraphQLUrl = value
} else {
GraphQLUrl = "http://127.0.0.1:8080/v1/graphql"
}
} else if GraphQLUrl == "" && !present {
GraphQLUrl = "http://127.0.0.1:9090/v1/graphql" GraphQLUrl = "http://127.0.0.1:9090/v1/graphql"
} }
} }
+31 -11
View File
@@ -38,6 +38,7 @@ func Test_queryBuilder(t *testing.T) {
want: `{"query":"query checkifUserIsAdmin($UserID: bigint, $GroupID: bigint) { tbl_user_group_admins(where: {is_admin: {_eq: true}, user_id: {_eq: $UserID}, group_id: {_eq: $GroupID}}) { id is_admin } }","variables":{"GroupID":11007,"UserID":37}}`, want: `{"query":"query checkifUserIsAdmin($UserID: bigint, $GroupID: bigint) { tbl_user_group_admins(where: {is_admin: {_eq: true}, user_id: {_eq: $UserID}, group_id: {_eq: $GroupID}}) { id is_admin } }","variables":{"GroupID":11007,"UserID":37}}`,
}, },
} }
GraphQLUrl = "http://hasura.local/v1/graphql"
assert := assert.New(t) assert := assert.New(t)
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@@ -69,6 +70,7 @@ func Test_queryAgainstDatabaseExecution(t *testing.T) {
is_admin is_admin
} }
}` }`
GraphQLUrl = "http://hasura.local/v1/graphql"
result, err := Query(query, variables, headers) result, err := Query(query, variables, headers)
if err != nil { if err != nil {
t.Log("Query execution errored. Is GQL server up?") t.Log("Query execution errored. Is GQL server up?")
@@ -99,34 +101,52 @@ func ExampleQuery() {
func Test_initialize(t *testing.T) { func Test_initialize(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
expected string expected string
env_endpoint string env_endpoint string
local_endpoint string
}{ }{
{ {
name: "Setting env variable for endpoint", name: "Setting env variable for endpoint (ENV)",
env_endpoint: "hasura.local/v1/graphql", env_endpoint: "new-hasura.local/v1/graphql",
expected: "hasura.local/v1/graphql", expected: "new-hasura.local/v1/graphql",
}, },
{ {
name: "Setting default endpoint", name: "Setting default endpoint (ENV)",
env_endpoint: "", env_endpoint: "",
expected: "http://127.0.0.1:9090/v1/graphql", local_endpoint: "",
expected: "http://127.0.0.1:9090/v1/graphql",
}, },
{ {
name: "Setting custom endpoint", name: "Setting custom endpoint (ENV)",
env_endpoint: "http://127.0.0.1:8080/v1/graphql", env_endpoint: "http://127.0.0.1:8080/v1/graphql",
expected: "http://127.0.0.1:8080/v1/graphql", expected: "http://127.0.0.1:8080/v1/graphql",
}, },
{
name: "Setting custom endpoint (VAR)",
local_endpoint: "http://127.0.0.1:8080/v1/graphql",
expected: "http://127.0.0.1:8080/v1/graphql",
},
{
name: "Setting custom endpoint (VAR)",
local_endpoint: "hasura-def.local/v1/graphql",
expected: "hasura-def.local/v1/graphql",
},
} }
assert := assert.New(t) assert := assert.New(t)
backupEnv, present := os.LookupEnv("GRAPHQL_ENDPOINT") backupEnv, present := os.LookupEnv("GRAPHQL_ENDPOINT")
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
os.Unsetenv("GRAPHQL_ENDPOINT")
GraphQLUrl = ""
if tt.env_endpoint != "" { if tt.env_endpoint != "" {
os.Setenv("GRAPHQL_ENDPOINT", tt.env_endpoint) os.Setenv("GRAPHQL_ENDPOINT", tt.env_endpoint)
t.Log(tt.env_endpoint, os.Getenv("GRAPHQL_ENDPOINT"))
}
if tt.local_endpoint != "" {
os.Unsetenv("GRAPHQL_ENDPOINT")
GraphQLUrl = tt.local_endpoint
} }
t.Log(tt.env_endpoint, os.Getenv("GRAPHQL_ENDPOINT"))
prepare() prepare()
assert.Equal(tt.expected, GraphQLUrl, "Unexpected value of env variable: "+tt.name) assert.Equal(tt.expected, GraphQLUrl, "Unexpected value of env variable: "+tt.name)
os.Unsetenv("GRAPHQL_ENDPOINT") os.Unsetenv("GRAPHQL_ENDPOINT")