wire auth to example, fix wire format, it's aliiiiiive!
This commit is contained in:
+36
-5
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user