wire up example by way of testing

This commit is contained in:
Ben Kraft
2019-12-24 16:05:10 -05:00
parent 9a157470b3
commit 9d4b5559f4
5 changed files with 41 additions and 6 deletions
+16
View File
@@ -0,0 +1,16 @@
package example
import (
"context"
"fmt"
"os"
)
func Main() {
resp, err := getViewer(context.Background())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("you are:", resp.Viewer.Name)
}
+9
View File
@@ -0,0 +1,9 @@
package main
import (
example "github.com/Khan/genql/example"
)
func main() {
example.Main()
}
+9 -4
View File
@@ -3,6 +3,7 @@ package example
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
@@ -10,19 +11,19 @@ import (
"github.com/vektah/gqlparser/gqlerror"
)
type GetViewerResponse = struct {
type getViewerResponse = struct {
Viewer struct {
Name *string
}
}
// TODO
func GetViewer(ctx context.Context) (*GetViewerResponse, error) {
func getViewer(ctx context.Context) (*getViewerResponse, error) {
req, err := http.NewRequest(
http.MethodPost,
`https://api.github.com/graphql`,
strings.NewReader(`
query GetViewer {
query getViewer {
Viewer: viewer {
Name: name
}
@@ -44,8 +45,12 @@ query GetViewer {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("returned error %v: %v", resp.Status, string(body))
}
var retval struct {
Data GetViewerResponse `json:"data"`
Data getViewerResponse `json:"data"`
Errors gqlerror.List `json:"errors"`
}
err = json.Unmarshal(body, &retval)
+2 -2
View File
@@ -1,5 +1,5 @@
# GetViewer gets the current user's name.
query GetViewer {
# getViewer gets the current user's name.
query getViewer {
Viewer: viewer {
Name: name
}