Skip to content

Commit fcbf7d4

Browse files
committed
refactor(graphql): improve queries by returning a simple cursor
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.
1 parent afbcaa4 commit fcbf7d4

File tree

9 files changed

+60
-54
lines changed

9 files changed

+60
-54
lines changed

lib/entitlements/service/github.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ def members_and_roles_from_graphql
210210
login
211211
}
212212
role
213-
cursor
214213
}
214+
pageInfo { endCursor }
215215
}
216216
}
217217
}".gsub(/\n\s+/, "\n")
@@ -222,14 +222,15 @@ def members_and_roles_from_graphql
222222
raise "GraphQL query failure"
223223
end
224224

225-
edges = response[:data].fetch("data").fetch("organization").fetch("membersWithRole").fetch("edges")
225+
membersWithRole = response[:data].fetch("data").fetch("organization").fetch("membersWithRole")
226+
edges = membersWithRole.fetch("edges")
226227
break unless edges.any?
227228

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

232-
cursor = edges.last.fetch("cursor")
233+
cursor = membersWithRole.fetch("pageInfo").fetch("endCursor")
233234
next if cursor && edges.size == max_graphql_results
234235
break
235236
end
@@ -276,8 +277,8 @@ def pending_members_from_graphql
276277
node {
277278
login
278279
}
279-
cursor
280280
}
281+
pageInfo { endCursor }
281282
}
282283
}
283284
}".gsub(/\n\s+/, "\n")
@@ -288,14 +289,15 @@ def pending_members_from_graphql
288289
raise "GraphQL query failure"
289290
end
290291

291-
edges = response[:data].fetch("data").fetch("organization").fetch("pendingMembers").fetch("edges")
292+
pendingMembers = response[:data].fetch("data").fetch("organization").fetch("pendingMembers")
293+
edges = pendingMembers.fetch("edges")
292294
break unless edges.any?
293295

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

298-
cursor = edges.last.fetch("cursor")
300+
cursor = pendingMembers.fetch("pageInfo").fetch("endCursor")
299301
next if cursor && edges.size == max_graphql_results
300302
break
301303
end

spec/unit/fixtures/graphql-output/organization-members-page1.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@
77
"node": {
88
"login": "monalisa"
99
},
10-
"role": "ADMIN",
11-
"cursor": "Y3Vyc29yOnYyOpEB"
10+
"role": "ADMIN"
1211
},
1312
{
1413
"node": {
1514
"login": "ocicat"
1615
},
17-
"role": "MEMBER",
18-
"cursor": "Y3Vyc29yOnYyOpEF"
16+
"role": "MEMBER"
1917
},
2018
{
2119
"node": {
2220
"login": "blackmanx"
2321
},
24-
"role": "MEMBER",
25-
"cursor": "Y3Vyc29yOnYyOpEG"
22+
"role": "MEMBER"
2623
}
27-
]
24+
],
25+
"pageInfo": {
26+
"endCursor": "Y3Vyc29yOnYyOpEG"
27+
}
2828
}
2929
}
3030
}

spec/unit/fixtures/graphql-output/organization-members-page2.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@
77
"node": {
88
"login": "toyger"
99
},
10-
"role": "MEMBER",
11-
"cursor": "Y3Vyc29yOnYyOpEH"
10+
"role": "MEMBER"
1211
},
1312
{
1413
"node": {
1514
"login": "highlander"
1615
},
17-
"role": "MEMBER",
18-
"cursor": "Y3Vyc29yOnYyOpEI"
16+
"role": "MEMBER"
1917
},
2018
{
2119
"node": {
2220
"login": "RussianBlue"
2321
},
24-
"role": "MEMBER",
25-
"cursor": "Y3Vyc29yOnYyOpEJ"
22+
"role": "MEMBER"
2623
}
27-
]
24+
],
25+
"pageInfo": {
26+
"endCursor": "Y3Vyc29yOnYyOpEJ"
27+
}
2828
}
2929
}
3030
}

spec/unit/fixtures/graphql-output/organization-members-page3.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@
77
"node": {
88
"login": "ragamuffin"
99
},
10-
"role": "MEMBER",
11-
"cursor": "Y3Vyc29yOnYyOpEK"
10+
"role": "MEMBER"
1211
},
1312
{
1413
"node": {
1514
"login": "mainecoon"
1615
},
17-
"role": "MEMBER",
18-
"cursor": "Y3Vyc29yOnYyOpEL"
16+
"role": "MEMBER"
1917
},
2018
{
2119
"node": {
2220
"login": "laperm"
2321
},
24-
"role": "MEMBER",
25-
"cursor": "Y3Vyc29yOnYyOpEM"
22+
"role": "MEMBER"
2623
}
27-
]
24+
],
25+
"pageInfo": {
26+
"endCursor": "Y3Vyc29yOnYyOpEM"
27+
}
2828
}
2929
}
3030
}

spec/unit/fixtures/graphql-output/organization-members-page4.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
"node": {
88
"login": "peterbald"
99
},
10-
"role": "MEMBER",
11-
"cursor": "Y3Vyc29yOnYyOpEN"
10+
"role": "MEMBER"
1211
}
13-
]
12+
],
13+
"pageInfo": {
14+
"endCursor": "Y3Vyc29yOnYyOpEN"
15+
}
1416
}
1517
}
1618
}

spec/unit/fixtures/graphql-output/pending-members-page1.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66
{
77
"node": {
88
"login": "alice"
9-
},
10-
"cursor": "Y3Vyc29yOnYyOpEB"
9+
}
1110
},
1211
{
1312
"node": {
1413
"login": "bob"
15-
},
16-
"cursor": "Y3Vyc29yOnYyOpEF"
14+
}
1715
},
1816
{
1917
"node": {
2018
"login": "charles"
21-
},
22-
"cursor": "Y3Vyc29yOnYyOpEG"
2319
}
24-
]
20+
}
21+
],
22+
"pageInfo": {
23+
"endCursor": "Y3Vyc29yOnYyOpEG"
24+
}
2525
}
2626
}
2727
}

spec/unit/fixtures/graphql-output/pending-members-page2.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66
{
77
"node": {
88
"login": "DAVID"
9-
},
10-
"cursor": "Y3Vyc29yOnYyOpEH"
9+
}
1110
},
1211
{
1312
"node": {
1413
"login": "edward"
15-
},
16-
"cursor": "Y3Vyc29yOnYyOpEI"
14+
}
1715
},
1816
{
1917
"node": {
2018
"login": "frank"
21-
},
22-
"cursor": "Y3Vyc29yOnYyOpEJ"
19+
}
2320
}
24-
]
21+
],
22+
"pageInfo": {
23+
"endCursor": "Y3Vyc29yOnYyOpEJ"
24+
}
2525
}
2626
}
2727
}

spec/unit/fixtures/graphql-output/pending-members-page3.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66
{
77
"node": {
88
"login": "george"
9-
},
10-
"cursor": "Y3Vyc29yOnYyOpEK"
9+
}
1110
},
1211
{
1312
"node": {
1413
"login": "harriet"
15-
},
16-
"cursor": "Y3Vyc29yOnYyOpEL"
14+
}
1715
},
1816
{
1917
"node": {
2018
"login": "ingrid"
21-
},
22-
"cursor": "Y3Vyc29yOnYyOpEM"
19+
}
2320
}
24-
]
21+
],
22+
"pageInfo": {
23+
"endCursor": "Y3Vyc29yOnYyOpEM"
24+
}
2525
}
2626
}
2727
}

spec/unit/fixtures/graphql-output/pending-members-page4.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
{
77
"node": {
88
"login": "blackmanx"
9-
},
10-
"cursor": "Y3Vyc29yOnYyOpEN"
9+
}
1110
}
12-
]
11+
],
12+
"pageInfo": {
13+
"endCursor": "Y3Vyc29yOnYyOpEN"
14+
}
1315
}
1416
}
1517
}

0 commit comments

Comments
 (0)