mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
1.8 KiB
93 lines
1.8 KiB
package main |
|
|
|
import ( |
|
"bytes" |
|
"encoding/json" |
|
"errors" |
|
"fmt" |
|
"io/ioutil" |
|
"net/http" |
|
"os" |
|
) |
|
|
|
func jsonPrettyPrint(in []byte) string { |
|
var out bytes.Buffer |
|
err := json.Indent(&out, in, "", " ") |
|
if err != nil { |
|
return string(in) |
|
} |
|
return out.String() |
|
} |
|
|
|
var HASURA_GRAPHQL_ADDRESS = os.Getenv("HASURA_GRAPHQL_ADDRESS") |
|
var HASURA_GRAPHQL_ADMIN_SECRET = os.Getenv("HASURA_GRAPHQL_ADMIN_SECRET") |
|
|
|
type GraphqlQuery struct { |
|
Data interface{} |
|
} |
|
|
|
var client = &http.Client{Transport: http.DefaultTransport} |
|
|
|
func hasura(query string, variables interface{}, data interface{}) (err error) { |
|
variablesBytes, err := json.Marshal(variables) |
|
if err != nil { |
|
return |
|
} |
|
|
|
v := string(variablesBytes) |
|
requestBody := []byte(`{"query":"` + query + `","variables":` + v + `}`) |
|
requestBytes := bytes.NewBuffer(requestBody) |
|
req, err := http.NewRequest("POST", HASURA_GRAPHQL_ADDRESS, requestBytes) |
|
if err != nil { |
|
return |
|
} |
|
|
|
req.Header.Add("X-Hasura-Admin-Secret", HASURA_GRAPHQL_ADMIN_SECRET) |
|
|
|
resp, err := client.Do(req) |
|
if err != nil { |
|
return |
|
} |
|
|
|
b, err := ioutil.ReadAll(resp.Body) |
|
if err != nil { |
|
return |
|
} |
|
|
|
// loggin the answer for debugging purposes |
|
fmt.Println(jsonPrettyPrint(b)) |
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 { |
|
return errors.New(http.StatusText(resp.StatusCode)) |
|
} |
|
|
|
gqlQuery := GraphqlQuery{Data: data} |
|
err = json.Unmarshal(b, &gqlQuery) |
|
if err != nil { |
|
return |
|
} |
|
return |
|
} |
|
|
|
type User struct { |
|
GithubLogin string |
|
} |
|
|
|
const userQuery = ` |
|
query { |
|
user { |
|
githubLogin |
|
} |
|
}` |
|
|
|
func getUsers() (users []User, err error) { |
|
var data map[string][]User |
|
err = hasura(userQuery, nil, &data) |
|
return data["user"], err |
|
} |
|
|
|
func main() { |
|
fmt.Println(getUsers()) |
|
} |
|
|
|
// HASURA_GRAPHQL_ADMIN_SECRET=VERYVERYSECRET HASURA_GRAPHQL_ADDRESS=http://localhost/graphql-engine/v1alpha1/graphql go run call-graphql.go
|
|
|