Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination examples for RESTClient and GQLClient #102

Merged
merged 2 commits into from
Feb 17, 2023

Conversation

samcoe
Copy link
Contributor

@samcoe samcoe commented Feb 13, 2023

Fixes #100

Additionally I removed our one reference to github.com/shurcooL/githubv4, it seemed like a large dependency to bring in just for an example.

@samcoe samcoe self-assigned this Feb 13, 2023
@samcoe samcoe force-pushed the pagination-examples branch from 425886f to e8e82da Compare February 13, 2023 08:23
@samcoe samcoe requested a review from mislav February 13, 2023 08:24
@samcoe samcoe marked this pull request as ready for review February 13, 2023 08:24
@samcoe samcoe requested a review from vilmibm February 13, 2023 08:25
if err != nil {
log.Fatal(err)
}
body, err := io.ReadAll(response.Body)
Copy link
Contributor

@mislav mislav Feb 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: More recently I started to prefer json.Decoder to decode a Reader right directly into the destination struct without having to first save it to an intermediate buffer. Maybe you can try it here to simplify the example?

requestPath := "repos/cli/cli/releases"
page := 1
hasNextPage := true
for hasNextPage {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: this could be an endless loop, with an explicit break happening if findNextPage() returned false. Logic would be lightly simpler and you wouldn't have to keep a hasNextPage variable outside of the loop

Comment on lines +96 to +98
var linkRE = regexp.MustCompile(`<([^>]+)>;\s*rel="([^"]+)"`)
findNextPage := func(response *http.Response) (string, bool) {
for _, m := range linkRE.FindAllStringSubmatch(response.Header.Get("Link"), -1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought for the future: having to do this as an extension author feels a bit low-level and requires understanding of certain details in the GitHub API. I wonder if we could provide facilities to encapsulate pagination from go-gh in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed #23

variables := map[string]interface{}{
"input": githubv4.AddStarInput{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit torn: on one hand I do appreciate that the new example has fewer dependencies, but I also did find value in this example demonstrating the use of the shurcooL library that ensures typed inputs for each GraphQL mutation. I would actually recommend the use of that library to API clients such as extensions. @mntlty: thoughts? #99

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's two use cases of what the examples here are for.

  • To show how go-gh can be used
  • To show how go-gh should be used

If it's the first, having another package when a struct will do the job isn't necessary. If it's the second, having githubv4 here introduces it to users who will likely benefit from the structure and safety it provides. They don't have to use it, but it's a big help to have the compile time checks.

Personally I learned about githubv4 when contributing to the CLI, which not everyone is fortunate enough to be able to do ❤️

Copy link
Contributor Author

@samcoe samcoe Feb 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thoughts on this are that as go-gh is meant to be used in other projects and all of our dependencies come with it so we should try hard to minimize them. If go had a way to mark a dependency for test or examples only then this wouldn't matter. This was a case I felt that having the dependency was not worth it. What I think would be a good compromise would be to add a comment to this example where the struct is defined saying that the user could alternatively use the githubv4 package where these are already defined. What do people think about that idea?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samcoe I like that compromise!

Copy link
Contributor

@mntlty mntlty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wish this existed a few weeks ago when I was figuring this out on my own, glad to see it's here now!

@samcoe samcoe merged commit a92c93a into trunk Feb 17, 2023
@samcoe samcoe deleted the pagination-examples branch February 17, 2023 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add pagination examples
3 participants