-
Notifications
You must be signed in to change notification settings - Fork 64
/
main.go
55 lines (47 loc) · 1.58 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"context"
"fmt"
"net/http"
"os"
"github.com/Yamashou/gqlgenc/clientv2"
"github.com/Yamashou/gqlgenc/example/github/gen"
)
func main() {
// This example only read public repository. You don't need to select scopes.
token := os.Getenv("GITHUB_TOKEN")
ctx := context.Background()
githubClient := &gen.Client{
Client: clientv2.NewClient(http.DefaultClient, "https://api.github.com/graphql", nil,
func(ctx context.Context, req *http.Request, gqlInfo *clientv2.GQLRequestInfo, res interface{}, next clientv2.RequestInterceptorFunc) error {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
return next(ctx, req, gqlInfo, res)
}),
}
getUser, err := githubClient.GetUser(ctx, 10, 10)
if err != nil {
if handledError, ok := err.(*clientv2.ErrorResponse); ok {
fmt.Fprintf(os.Stderr, "handled error: %s\n", handledError.Error())
} else {
fmt.Fprintf(os.Stderr, "unhandled error: %s\n", err.Error())
}
os.Exit(1)
}
fmt.Println(*getUser.Viewer.Name, getUser.Viewer.Repositories.Nodes[0].Name)
for _, repository := range getUser.Viewer.Repositories.Nodes {
fmt.Println(repository.Name)
for _, language := range repository.Languages.Nodes {
fmt.Println(language.Name)
}
res, err := githubClient.GetNode(ctx, repository.ID)
if err != nil {
if handledError, ok := err.(*clientv2.ErrorResponse); ok {
fmt.Fprintf(os.Stderr, "handled error: %s\n", handledError.Error())
} else {
fmt.Fprintf(os.Stderr, "unhandled error: %s\n", err.Error())
}
os.Exit(1)
}
fmt.Println(res.Node.ID, res.Node.Repository.Name)
}
}