Skip to content

Commit e8ba04c

Browse files
committed
implementing pagination for GithubProjectItemCollection
1 parent fe292cb commit e8ba04c

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

github-project-to-csv.rb

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,18 @@ def to_csv
7979
end
8080

8181
class GithubProjectItemCollection < GithubQuery
82+
PAGINATION_BATCH_SIZE = 100
83+
8284
def self.find_by(project_id:)
83-
execute("
85+
execute_paginated("
8486
query{
8587
node(id: \"#{project_id}\") {
8688
... on ProjectV2 {
87-
items(first: 100) {
89+
items(first: ALL) {
90+
pageInfo {
91+
endCursor
92+
hasNextPage
93+
}
8894
nodes {
8995
id
9096
content{
@@ -174,6 +180,27 @@ def self.find_by(project_id:)
174180
")
175181
end
176182

183+
def self.execute_paginated(query)
184+
github_project_item_collection = nil
185+
all_nodes = []
186+
pagination_string = "first: #{PAGINATION_BATCH_SIZE}"
187+
188+
loop do
189+
github_project_item_collection = page = execute(query.gsub("first: ALL", pagination_string))
190+
all_nodes += page.result.dig("data", "node", "items", "nodes")
191+
192+
if page.result.dig("data", "node", "items", "pageInfo", "hasNextPage")
193+
last_cursor_id = page.result.dig("data", "node", "items", "pageInfo", "endCursor")
194+
pagination_string = "first: #{PAGINATION_BATCH_SIZE}, after: \"#{last_cursor_id}\""
195+
else
196+
break
197+
end
198+
end
199+
200+
github_project_item_collection.result["data"]["node"]["items"]["nodes"] = all_nodes
201+
github_project_item_collection
202+
end
203+
177204
def items
178205
result.dig("data", "node", "items", "nodes").collect do |node_data|
179206
github_project_item = GithubProjectItem.new

0 commit comments

Comments
 (0)