wire auth to example, fix wire format, it's aliiiiiive!

This commit is contained in:
Ben Kraft
2019-12-25 21:33:12 -05:00
parent 4d880a701e
commit 7f6ab70ee3
2 changed files with 52 additions and 8 deletions
+36 -5
View File
@@ -3,17 +3,48 @@ package example
import (
"context"
"fmt"
"net/http"
"os"
"github.com/Khan/genql/graphql"
)
type authedTransport struct {
key string
wrapped http.RoundTripper
}
func (t *authedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", "bearer "+t.key)
return t.wrapped.RoundTrip(req)
}
func Main() {
client := graphql.NewClient("https://api.github.com/graphql", nil)
resp, err := getViewer(context.Background(), client)
var err error
defer func() {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}()
key := os.Getenv("KEY")
if key == "" {
err = fmt.Errorf("must set KEY=<github token>")
return
}
httpClient := http.Client{
Transport: &authedTransport{
key: key,
wrapped: http.DefaultTransport,
},
}
graphqlClient := graphql.NewClient("https://api.github.com/graphql", &httpClient)
resp, err := getViewer(context.Background(), graphqlClient)
if err != nil {
fmt.Println(err)
os.Exit(1)
return
}
fmt.Println("you are:", resp.Viewer.Name)
fmt.Println("you are:", *resp.Viewer.Name)
}