sketches of generated graphql client

This commit is contained in:
Ben Kraft
2019-12-21 00:03:19 -05:00
commit a23b0e3756
7 changed files with 137 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
package example
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
)
type GetViewerResponse = struct {
Viewer struct {
Name string `json:"name"`
} `json:"viewer"`
}
// GetViewer gets the current user's name.
func GetViewer(ctx context.Context) (*GetViewerResponse, error) {
req, err := http.NewRequestWithContext(
ctx, http.MethodPost,
`https://api.github.com/graphql`,
strings.NewReader(`
"GetViewer gets the current user's name."
query GetViewer {
Viewer: viewer {
Name: name
}
}
`))
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
retval := GetViewerResponse{}
err = json.Unmarshal(body, &retval)
if err != nil {
return nil, err
}
return &retval, nil
}
+6
View File
@@ -0,0 +1,6 @@
"GetViewer gets the current user's name."
query GetUser($login: String!) {
User: user(login: $login) {
Name: name
}
}