From abff330394d1da0e20de2a965732ee3746cbbb78 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Wed, 12 May 2021 10:34:32 +0100 Subject: [PATCH] Fix graphql endpoint setup order. --- gql.go | 10 +++++++--- gql_test.go | 42 +++++++++++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/gql.go b/gql.go index f1f9c5c..3709f90 100644 --- a/gql.go +++ b/gql.go @@ -27,9 +27,13 @@ type requestBase struct { func prepare() { value, present := os.LookupEnv("GRAPHQL_ENDPOINT") - if present { - GraphQLUrl = value - } else { + if GraphQLUrl == "" && present { + if present { + 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" } } diff --git a/gql_test.go b/gql_test.go index 2d9b7a3..99a39d8 100644 --- a/gql_test.go +++ b/gql_test.go @@ -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}}`, }, } + GraphQLUrl = "http://hasura.local/v1/graphql" assert := assert.New(t) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -69,6 +70,7 @@ func Test_queryAgainstDatabaseExecution(t *testing.T) { is_admin } }` + GraphQLUrl = "http://hasura.local/v1/graphql" result, err := Query(query, variables, headers) if err != nil { t.Log("Query execution errored. Is GQL server up?") @@ -99,34 +101,52 @@ func ExampleQuery() { func Test_initialize(t *testing.T) { tests := []struct { - name string - expected string - env_endpoint string + name string + expected string + env_endpoint string + local_endpoint string }{ { - name: "Setting env variable for endpoint", - env_endpoint: "hasura.local/v1/graphql", - expected: "hasura.local/v1/graphql", + name: "Setting env variable for endpoint (ENV)", + env_endpoint: "new-hasura.local/v1/graphql", + expected: "new-hasura.local/v1/graphql", }, { - name: "Setting default endpoint", - env_endpoint: "", - expected: "http://127.0.0.1:9090/v1/graphql", + name: "Setting default endpoint (ENV)", + env_endpoint: "", + 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", 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) backupEnv, present := os.LookupEnv("GRAPHQL_ENDPOINT") for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + os.Unsetenv("GRAPHQL_ENDPOINT") + GraphQLUrl = "" if 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() assert.Equal(tt.expected, GraphQLUrl, "Unexpected value of env variable: "+tt.name) os.Unsetenv("GRAPHQL_ENDPOINT")