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

Is there a way to use a null type in the after graphql tag? #58

Closed palash25 closed 4 years ago

palash25 commented 4 years ago

I am trying to fetch all the PRs in a repo, I start with this struct to make my first request

type firstBatchRequest struct {
        Repository struct {
            PullRequests struct {
                Nodes []struct {
                    Author  githubV4Actor
                    Participants struct {
                        Nodes []struct {
                            Login githubv4.String
                        }
                    } `graphql:"participants(first:$nodes)"`
                }
                PageInfo struct {
                    EndCursor   githubv4.String
                    HasNextPage githubv4.Boolean
                }
            } `graphql:"pullRequests(baseRefName:$branch,first:$prNumber)"`
        } `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
    }

with these options

opts := map[string]interface{}{
            "repositoryOwner": githubv4.String("someone"),
            "repositoryName":  githubv4.String("something"),
            "prNumber":        githubv4.Int(100),
            "branch":          githubv4.String("master"),
            "nodes":           githubv4.Int(100),
        }

and once I make the first request I make subsequent requests using the endCursor obtained from the first one to get the PRs after that like this

type subsequentBatchRequest struct {
        Repository struct {
            PullRequests struct {
                Nodes []struct {
                    Number  githubv4.Int
                    Author  githubV4Actor
                    Participants struct {
                        Nodes []struct {
                            Login githubv4.String
                        }
                    } `graphql:"participants(first:$nodes)"`
                }
                PageInfo struct {
                    EndCursor   githubv4.String
                    HasNextPage githubv4.Boolean
                }
            } `graphql:"pullRequests(baseRefName:$branch,first:$prNumber,after:$endCursor)"`
        } `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
    }

opts := map[string]interface{}{
                "repositoryOwner": githubv4.String("someone"),
                "repositoryName":  githubv4.String("something"),
                "prNumber":        githubv4.Int(100),
                "branch":          githubv4.String("master"),
                "endCursor":       githubv4.String(cursor),
                "nodes":           githubv4.Int(100),
            }

The only difference between these two is that one uses an endcursor and one doesn't, if there was a way to pass a null for the after tag I would be able to reduce the two structs to just one and reuse it, is there anyway to do that?

ichn-hu commented 4 years ago

I am facing the same issue, and would be glad if someone could help as well.

ichn-hu commented 4 years ago

@palash25 I immediately found the solution by carefully reading the documentation on pagination

see here, and it worked like a charm.

@palash25 you may want to close this issue.

variables := map[string]interface{}{
    "repositoryOwner": githubv4.String(owner),
    "repositoryName":  githubv4.String(name),
    "issueNumber":     githubv4.Int(issue),
    "commentsCursor":  (*githubv4.String)(nil), // Null after argument to get first page.
}
palash25 commented 4 years ago

thanks @ichn-hu