shurcooL / githubv4

Package githubv4 is a client library for accessing GitHub GraphQL API v4 (https://docs.github.com/en/graphql).
MIT License
1.1k stars 89 forks source link

Star gazers not part of a Repository #73

Closed g14a closed 4 years ago

g14a commented 4 years ago

Hello! I was exploring the GraphQL explorer and I'm able to list down the total count of stars on a repo with this following query

{
  user(login: "g14a") {
    repositories(first: 100) {
      nodes {
        name
        stargazers {
          totalCount
        }
      }
    }
  }
}

But when I use the library to list it down it gives the error Field 'starGazers' doesn't exist on type 'Repository'

I'm declaring the structs as follows:

var ForksStarsQuery struct {
    User struct {
        Repositories struct {
            Nodes []Nodes
        } `graphql:"repositories(first: $repoCount)"`
    } `graphql:"user(login: $user)"`
}

type Nodes struct {
    StarGazers struct {
        TotalCount githubv4.Int
    }
    Name githubv4.String
}

Is there anything simple I'm missing here? I'm able to get values of the ForkCount and WatcherCount but not the StarGazers. Thank you in advance.

dmitshur commented 4 years ago

Either rename the StarGazers field to Stargazers, or add an explicit name override via field tag:

StarGazers struct {
    TotalCount githubv4.Int
} `graphql:"stargazers"`

There's an implicit conversion happening if you don't set an explicit GraphQL field name via the graphql tag, and the Go field "StarGazers" is converted to "starGazers" (capital 'G'), which doesn't match the field "stargazers" (lower case 'g') in the GraphQL schema. It is case sensitive and fields must match exactly.

g14a commented 4 years ago

@dmitshur Thank you my dude. You saved my day. Will remember the implicit conversion in the future. :grin: