Skip to content

Commit

Permalink
Merge pull request #26 from github/simplify_graphql_paging
Browse files Browse the repository at this point in the history
refactor(graphql): improve queries by returning a simple cursor
  • Loading branch information
nobe4 authored Dec 5, 2023
2 parents afbcaa4 + 3935b59 commit d39f418
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
entitlements-github-plugin (0.4.3)
entitlements-github-plugin (0.4.4)
contracts (~> 0.17.0)
faraday (~> 2.0)
faraday-retry (~> 2.0)
Expand Down
14 changes: 8 additions & 6 deletions lib/entitlements/service/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ def members_and_roles_from_graphql
login
}
role
cursor
}
pageInfo { endCursor }
}
}
}".gsub(/\n\s+/, "\n")
Expand All @@ -222,14 +222,15 @@ def members_and_roles_from_graphql
raise "GraphQL query failure"
end

edges = response[:data].fetch("data").fetch("organization").fetch("membersWithRole").fetch("edges")
membersWithRole = response[:data].fetch("data").fetch("organization").fetch("membersWithRole")
edges = membersWithRole.fetch("edges")
break unless edges.any?

edges.each do |edge|
result[edge.fetch("node").fetch("login").downcase] = edge.fetch("role")
end

cursor = edges.last.fetch("cursor")
cursor = membersWithRole.fetch("pageInfo").fetch("endCursor")
next if cursor && edges.size == max_graphql_results
break
end
Expand Down Expand Up @@ -276,8 +277,8 @@ def pending_members_from_graphql
node {
login
}
cursor
}
pageInfo { endCursor }
}
}
}".gsub(/\n\s+/, "\n")
Expand All @@ -288,14 +289,15 @@ def pending_members_from_graphql
raise "GraphQL query failure"
end

edges = response[:data].fetch("data").fetch("organization").fetch("pendingMembers").fetch("edges")
pendingMembers = response[:data].fetch("data").fetch("organization").fetch("pendingMembers")
edges = pendingMembers.fetch("edges")
break unless edges.any?

edges.each do |edge|
result.add(edge.fetch("node").fetch("login").downcase)
end

cursor = edges.last.fetch("cursor")
cursor = pendingMembers.fetch("pageInfo").fetch("endCursor")
next if cursor && edges.size == max_graphql_results
break
end
Expand Down
2 changes: 1 addition & 1 deletion lib/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Entitlements
module Version
VERSION = "0.4.3"
VERSION = "0.4.4"
end
end
22 changes: 16 additions & 6 deletions spec/acceptance/github-server/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,22 @@ def graphql_org_query(query)

edges = []
cursor_flag = cursor.nil?
end_cursor = nil
result.each do |user, role|
next if !cursor_flag && Base64.strict_encode64(user) != cursor
edges << { "node" => { "login" => user }, "role" => role, "cursor" => Base64.strict_encode64(user) } if cursor_flag
end_cursor = Base64.strict_encode64(user)
next if !cursor_flag && end_cursor != cursor
edges << { "node" => { "login" => user }, "role" => role} if cursor_flag
cursor_flag = true
break if edges.size >= first
end

{
"organization" => {
"membersWithRole" => {
"edges" => edges
"edges" => edges,
"pageInfo" => {
"endCursor" => end_cursor
}
}
}
}
Expand All @@ -144,17 +149,22 @@ def graphql_pending_query(query)

edges = []
cursor_flag = cursor.nil?
end_cursor = nil
result.each do |user|
next if !cursor_flag && Base64.strict_encode64(user) != cursor
edges << { "node" => { "login" => user }, "cursor" => Base64.strict_encode64(user) } if cursor_flag
end_cursor = Base64.strict_encode64(user)
next if !cursor_flag && end_cursor != cursor
edges << { "node" => { "login" => user } } if cursor_flag
cursor_flag = true
break if edges.size >= first
end

{
"organization" => {
"pendingMembers" => {
"edges" => edges
"edges" => edges,
"pageInfo" => {
"endCursor" => end_cursor
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
"node": {
"login": "monalisa"
},
"role": "ADMIN",
"cursor": "Y3Vyc29yOnYyOpEB"
"role": "ADMIN"
},
{
"node": {
"login": "ocicat"
},
"role": "MEMBER",
"cursor": "Y3Vyc29yOnYyOpEF"
"role": "MEMBER"
},
{
"node": {
"login": "blackmanx"
},
"role": "MEMBER",
"cursor": "Y3Vyc29yOnYyOpEG"
"role": "MEMBER"
}
]
],
"pageInfo": {
"endCursor": "Y3Vyc29yOnYyOpEG"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
"node": {
"login": "toyger"
},
"role": "MEMBER",
"cursor": "Y3Vyc29yOnYyOpEH"
"role": "MEMBER"
},
{
"node": {
"login": "highlander"
},
"role": "MEMBER",
"cursor": "Y3Vyc29yOnYyOpEI"
"role": "MEMBER"
},
{
"node": {
"login": "RussianBlue"
},
"role": "MEMBER",
"cursor": "Y3Vyc29yOnYyOpEJ"
"role": "MEMBER"
}
]
],
"pageInfo": {
"endCursor": "Y3Vyc29yOnYyOpEJ"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
"node": {
"login": "ragamuffin"
},
"role": "MEMBER",
"cursor": "Y3Vyc29yOnYyOpEK"
"role": "MEMBER"
},
{
"node": {
"login": "mainecoon"
},
"role": "MEMBER",
"cursor": "Y3Vyc29yOnYyOpEL"
"role": "MEMBER"
},
{
"node": {
"login": "laperm"
},
"role": "MEMBER",
"cursor": "Y3Vyc29yOnYyOpEM"
"role": "MEMBER"
}
]
],
"pageInfo": {
"endCursor": "Y3Vyc29yOnYyOpEM"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
"node": {
"login": "peterbald"
},
"role": "MEMBER",
"cursor": "Y3Vyc29yOnYyOpEN"
"role": "MEMBER"
}
]
],
"pageInfo": {
"endCursor": "Y3Vyc29yOnYyOpEN"
}
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions spec/unit/fixtures/graphql-output/pending-members-page1.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
{
"node": {
"login": "alice"
},
"cursor": "Y3Vyc29yOnYyOpEB"
}
},
{
"node": {
"login": "bob"
},
"cursor": "Y3Vyc29yOnYyOpEF"
}
},
{
"node": {
"login": "charles"
},
"cursor": "Y3Vyc29yOnYyOpEG"
}
]
}
],
"pageInfo": {
"endCursor": "Y3Vyc29yOnYyOpEG"
}
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions spec/unit/fixtures/graphql-output/pending-members-page2.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
{
"node": {
"login": "DAVID"
},
"cursor": "Y3Vyc29yOnYyOpEH"
}
},
{
"node": {
"login": "edward"
},
"cursor": "Y3Vyc29yOnYyOpEI"
}
},
{
"node": {
"login": "frank"
},
"cursor": "Y3Vyc29yOnYyOpEJ"
}
}
]
],
"pageInfo": {
"endCursor": "Y3Vyc29yOnYyOpEJ"
}
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions spec/unit/fixtures/graphql-output/pending-members-page3.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
{
"node": {
"login": "george"
},
"cursor": "Y3Vyc29yOnYyOpEK"
}
},
{
"node": {
"login": "harriet"
},
"cursor": "Y3Vyc29yOnYyOpEL"
}
},
{
"node": {
"login": "ingrid"
},
"cursor": "Y3Vyc29yOnYyOpEM"
}
}
]
],
"pageInfo": {
"endCursor": "Y3Vyc29yOnYyOpEM"
}
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions spec/unit/fixtures/graphql-output/pending-members-page4.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
{
"node": {
"login": "blackmanx"
},
"cursor": "Y3Vyc29yOnYyOpEN"
}
}
]
],
"pageInfo": {
"endCursor": "Y3Vyc29yOnYyOpEN"
}
}
}
}
Expand Down

0 comments on commit d39f418

Please sign in to comment.