Skip to content

Commit

Permalink
refactor(graphql): improve queries by returning a simple cursor
Browse files Browse the repository at this point in the history
The `membersWithRole` connection has a `pageInfo` containing the
`endCursor`. It is equivalent to look at the last edge's cursor but
creates a smaller return object.

This update the queries, code and fixtures to reflect the new way to
paginate.
  • Loading branch information
nobe4 committed Nov 30, 2023
1 parent afbcaa4 commit fcbf7d4
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 54 deletions.
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
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 fcbf7d4

Please sign in to comment.