Skip to content

Commit

Permalink
lab01s02 query implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ianbandrade committed Aug 22, 2021
1 parent 67e4a1c commit 3ea0b3b
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
65 changes: 65 additions & 0 deletions cmd/lab01s02/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"context"
"encoding/json"
"os"

"github.com/gamoch/popular_repos/internal/pkg/configuration"
"github.com/gamoch/popular_repos/pkg/graphql"
"github.com/gamoch/popular_repos/pkg/graphql/providers/github"
"github.com/gamoch/popular_repos/pkg/logs"
)

const query = `query PopularRepos($cursor: String) {
search(query: "stars:>1", type: REPOSITORY, first: 100, after: $cursor) {
pageInfo {
hasNextPage
endCursor
}
nodes {
... on Repository {
nameWithOwner
stargazerCount
createdAt
updatedAt
pullRequests(states: MERGED) {
totalCount
}
releases {
totalCount
}
primaryLanguage {
name
}
openIssues: issues(states: OPEN) {
totalCount
}
closedIssues: issues(states: CLOSED) {
totalCount
}
}
}
}
}`

func main() {
config := configuration.Get()
ctx := context.Background()

githubClient := github.NewClient(config.Token)
req := graphql.NewRequest(query)

graphQLData := new(GraphQLData)
if err := githubClient.Run(ctx, req, graphQLData); err != nil {
logs.Error.Fatal(err)
}

graphqlJSON, err := json.MarshalIndent(graphQLData, "", " ")
if err != nil {
logs.Error.Fatal(err)
}

graphqlJSON = append(graphqlJSON, '\n')
os.Stdout.Write(graphqlJSON)
}
33 changes: 33 additions & 0 deletions cmd/lab01s02/structures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import "time"

type GraphQLData struct {
Search struct {
PageInfo struct {
HasNextPage bool `json:"hasNextPage"`
EndCursor string `json:"endCursor"`
} `json:"pageInfo"`
Nodes []struct {
NameWithOwner string `json:"nameWithOwner"`
StargazerCount int `json:"stargazerCount"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
PullRequests struct {
TotalCount int `json:"totalCount"`
} `json:"pullRequests"`
Releases struct {
TotalCount int `json:"totalCount"`
} `json:"releases"`
PrimaryLanguage *struct {
Name string `json:"name"`
} `json:"primaryLanguage"`
OpenIssues struct {
TotalCount int `json:"totalCount"`
} `json:"openIssues"`
ClosedIssues struct {
TotalCount int `json:"totalCount"`
} `json:"closedIssues"`
} `json:"nodes"`
} `json:"search"`
}

0 comments on commit 3ea0b3b

Please sign in to comment.