Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Commit c91f075

Browse files
authored
Use pageInfo for pagination (#355)
Instead of fetching every cursor, just fetch `pageInfo.endCursor`. Also, `pageInfo.hasNextPage` saves one final network request.
1 parent 96e951c commit c91f075

File tree

2 files changed

+37
-42
lines changed

2 files changed

+37
-42
lines changed

src/queries/all-open-prs-query.ts

+19-24
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import { gql, TypedDocumentNode } from "@apollo/client/core";
22
import { client } from "../graphql-client";
33
import { GetAllOpenPRsAndCardIDs, GetAllOpenPRsAndCardIDsVariables } from "./schema/GetAllOpenPRsAndCardIDs";
4+
import { noNullish } from "../util/util";
45

56
export const getAllOpenPRsAndCardIDsQuery: TypedDocumentNode<GetAllOpenPRsAndCardIDs, GetAllOpenPRsAndCardIDsVariables> = gql`
6-
query GetAllOpenPRsAndCardIDs($after: String) {
7+
query GetAllOpenPRsAndCardIDs($endCursor: String) {
78
repository(owner: "DefinitelyTyped", name: "DefinitelyTyped") {
89
id
9-
pullRequests(orderBy: { field: UPDATED_AT, direction: DESC }, states: [OPEN], first: 100, after: $after) {
10-
edges {
11-
cursor
12-
node {
13-
number
14-
updatedAt
15-
projectCards(first: 100) { nodes { id } }
16-
}
10+
pullRequests(states: OPEN, orderBy: { field: UPDATED_AT, direction: DESC }, first: 100, after: $endCursor) {
11+
nodes {
12+
number
13+
projectCards(first: 100) { nodes { id } }
14+
}
15+
pageInfo {
16+
hasNextPage
17+
endCursor
1718
}
1819
}
1920
}
@@ -22,26 +23,20 @@ query GetAllOpenPRsAndCardIDs($after: String) {
2223
export async function getAllOpenPRsAndCardIDs() {
2324
const prNumbers: number[] = [];
2425
const cardIDs: string[] = [];
25-
let after: string | undefined;
26+
let endCursor: string | undefined | null;
2627
while (true) {
27-
const results = await client.query({
28+
const result = await client.query({
2829
query: getAllOpenPRsAndCardIDsQuery,
2930
fetchPolicy: "no-cache",
30-
variables: { after }
31+
variables: { endCursor }
3132
});
32-
33-
if (!results.data.repository?.pullRequests.edges?.length) {
34-
return { prNumbers, cardIDs };
33+
prNumbers.push(...noNullish(result.data.repository?.pullRequests.nodes).map(pr => pr.number));
34+
for (const pr of noNullish(result.data.repository?.pullRequests.nodes)) {
35+
cardIDs.push(...noNullish(pr.projectCards.nodes).map(card => card.id));
3536
}
36-
37-
for (const edge of results.data.repository.pullRequests.edges) {
38-
if (!edge) continue;
39-
const { node, cursor } = edge;
40-
after = cursor;
41-
if (!node) continue;
42-
43-
prNumbers.push(node.number);
44-
node.projectCards.nodes?.forEach(n => n && cardIDs.push(n.id));
37+
if (!result.data.repository?.pullRequests.pageInfo.hasNextPage) {
38+
return { prNumbers, cardIDs };
4539
}
40+
endCursor = result.data.repository.pullRequests.pageInfo.endCursor;
4641
}
4742
}

src/queries/schema/GetAllOpenPRsAndCardIDs.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,53 @@
77
// GraphQL query operation: GetAllOpenPRsAndCardIDs
88
// ====================================================
99

10-
export interface GetAllOpenPRsAndCardIDs_repository_pullRequests_edges_node_projectCards_nodes {
10+
export interface GetAllOpenPRsAndCardIDs_repository_pullRequests_nodes_projectCards_nodes {
1111
__typename: "ProjectCard";
1212
id: string;
1313
}
1414

15-
export interface GetAllOpenPRsAndCardIDs_repository_pullRequests_edges_node_projectCards {
15+
export interface GetAllOpenPRsAndCardIDs_repository_pullRequests_nodes_projectCards {
1616
__typename: "ProjectCardConnection";
1717
/**
1818
* A list of nodes.
1919
*/
20-
nodes: (GetAllOpenPRsAndCardIDs_repository_pullRequests_edges_node_projectCards_nodes | null)[] | null;
20+
nodes: (GetAllOpenPRsAndCardIDs_repository_pullRequests_nodes_projectCards_nodes | null)[] | null;
2121
}
2222

23-
export interface GetAllOpenPRsAndCardIDs_repository_pullRequests_edges_node {
23+
export interface GetAllOpenPRsAndCardIDs_repository_pullRequests_nodes {
2424
__typename: "PullRequest";
2525
/**
2626
* Identifies the pull request number.
2727
*/
2828
number: number;
29-
/**
30-
* Identifies the date and time when the object was last updated.
31-
*/
32-
updatedAt: any;
3329
/**
3430
* List of project cards associated with this pull request.
3531
*/
36-
projectCards: GetAllOpenPRsAndCardIDs_repository_pullRequests_edges_node_projectCards;
32+
projectCards: GetAllOpenPRsAndCardIDs_repository_pullRequests_nodes_projectCards;
3733
}
3834

39-
export interface GetAllOpenPRsAndCardIDs_repository_pullRequests_edges {
40-
__typename: "PullRequestEdge";
35+
export interface GetAllOpenPRsAndCardIDs_repository_pullRequests_pageInfo {
36+
__typename: "PageInfo";
4137
/**
42-
* A cursor for use in pagination.
38+
* When paginating forwards, are there more items?
4339
*/
44-
cursor: string;
40+
hasNextPage: boolean;
4541
/**
46-
* The item at the end of the edge.
42+
* When paginating forwards, the cursor to continue.
4743
*/
48-
node: GetAllOpenPRsAndCardIDs_repository_pullRequests_edges_node | null;
44+
endCursor: string | null;
4945
}
5046

5147
export interface GetAllOpenPRsAndCardIDs_repository_pullRequests {
5248
__typename: "PullRequestConnection";
5349
/**
54-
* A list of edges.
50+
* A list of nodes.
51+
*/
52+
nodes: (GetAllOpenPRsAndCardIDs_repository_pullRequests_nodes | null)[] | null;
53+
/**
54+
* Information to aid in pagination.
5555
*/
56-
edges: (GetAllOpenPRsAndCardIDs_repository_pullRequests_edges | null)[] | null;
56+
pageInfo: GetAllOpenPRsAndCardIDs_repository_pullRequests_pageInfo;
5757
}
5858

5959
export interface GetAllOpenPRsAndCardIDs_repository {
@@ -73,5 +73,5 @@ export interface GetAllOpenPRsAndCardIDs {
7373
}
7474

7575
export interface GetAllOpenPRsAndCardIDsVariables {
76-
after?: string | null;
76+
endCursor?: string | null;
7777
}

0 commit comments

Comments
 (0)