shurcooL / githubv4

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

How to query for commits in a given Repository #62

Open SashaCollins opened 4 years ago

SashaCollins commented 4 years ago

Hello,

I'm trying to convert this query into a struct:

{
  repository(owner: "google", name: "gson") {
    name
    refs(first: 100, refPrefix: "refs/heads/") {
      edges {
        node {
          name
          target {
            ... on Commit {
              id
              history(first: 2) {
                totalCount
                edges {
                  node {
                    ... on Commit {
                      committer {
                        date
                        email
                        name
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

I got this from https://stackoverflow.com/questions/15919539/github-api-how-to-efficiently-find-the-number-of-commits-for-a-repository

I tested it on the Github explorer just to be safe and its working as it should. What I'm trying to do is to get the "TotalCount" of commits per contributor to a specific Repository. I'm not really sure how I'm supposed to do it exactly. I already got this:

type repositoryInfo struct {
    Repository struct {
        Refs struct {
            TotalCount githubv4.Int //number of branches
            Nodes []ref
            PageInfo pageInfo
        }`graphql:"refs(refPrefix:$prefix,first:$refFirst,after:$refAfter,orderBy:$orderBy)"`
    } `graphql:"repository(owner:$login,name:$repositoryName)"`
}

type ref struct {
    Name githubv4.String
    Prefix githubv4.String

    Target struct {
        AbbreviatedOid githubv4.String
        ID githubv4.GitObjectID
        //History struct {
        //  TotalCount githubv4.Int
        //}`graphql:"history(first:0)"`
    }`graphql:"... on Commit"`
}

But I get this error message:

Fragment on Commit can't be spread inside Ref

However I can't seem to find any useful documentation. Can anybody tell me what I'm doing wrong please?

sshmaxime commented 3 years ago

@SashaCollins Did you figured out how to do it ?

Edwin-Luijten commented 3 years ago

Having the same message on a different query: Fragment on Repository can't be spread inside SearchResultItemEdge

chetan commented 2 years ago

I got this working with the following:

type LatestContributions struct {
    User struct {
        Repositories struct {
            TotalCount int
            Nodes      []struct {
                Name      string
                IsPrivate bool
                PushedAt  githubv4.DateTime

                DefaultBranchRef struct {
                    Name   string
                    Target struct {
                        SpreadCommits struct {
                            History struct {
                                TotalCount int
                            } `graphql:"history(since:"2021-09-19T23:05:23Z")"`
                        } `graphql:"... on Commit"`
                    }
                }
            }
        } `graphql:"repositories(first: 10, orderBy: {field: PUSHED_AT, direction: DESC})"`
    } `graphql:"user(login: $login)"`
}

The missing bit in the top post is the stub/placeholder SpreadCommits which gets overridden in the generated query by the tag value ... on Commit

jay-418 commented 1 year ago

@chetan 's example helped solve my issue with error Fragment on ProjectV2Field can't be spread inside ProjectV2 while trying to implement a struct for this query:

query {
    repository(owner:"orgName", name:"repoName") {
        projectV2(number:123){
            field(name:"Example") {
            on ProjectV2Field {
                    name
                    id
                }
            }
        }
    }
}

The solution was to create the Spread struct to be overwritten with ... on ProjectV2Field, much like @chetan used SpreadCommits and ... on Commit:

var q struct {
    Repository struct {
        ProjectV2 struct {
            Field struct {
                Spread struct {
                    Name string
                    Id   string
                } `graphql:"... on ProjectV2Field"`
            } `graphql:"field(name: $fieldName)"`
        } `graphql:"projectV2(number: $projectNumber)"`
    } `graphql:"repository(owner: $owner, name: $name)"`
}