1
1
import { gql , TypedDocumentNode } from "@apollo/client/core" ;
2
2
import { client } from "../graphql-client" ;
3
3
import { GetAllOpenPRsAndCardIDs , GetAllOpenPRsAndCardIDsVariables } from "./schema/GetAllOpenPRsAndCardIDs" ;
4
+ import { noNullish } from "../util/util" ;
4
5
5
6
export const getAllOpenPRsAndCardIDsQuery : TypedDocumentNode < GetAllOpenPRsAndCardIDs , GetAllOpenPRsAndCardIDsVariables > = gql `
6
- query GetAllOpenPRsAndCardIDs($after : String) {
7
+ query GetAllOpenPRsAndCardIDs($endCursor : String) {
7
8
repository(owner: "DefinitelyTyped", name: "DefinitelyTyped") {
8
9
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
17
18
}
18
19
}
19
20
}
@@ -22,26 +23,20 @@ query GetAllOpenPRsAndCardIDs($after: String) {
22
23
export async function getAllOpenPRsAndCardIDs ( ) {
23
24
const prNumbers : number [ ] = [ ] ;
24
25
const cardIDs : string [ ] = [ ] ;
25
- let after : string | undefined ;
26
+ let endCursor : string | undefined | null ;
26
27
while ( true ) {
27
- const results = await client . query ( {
28
+ const result = await client . query ( {
28
29
query : getAllOpenPRsAndCardIDsQuery ,
29
30
fetchPolicy : "no-cache" ,
30
- variables : { after }
31
+ variables : { endCursor }
31
32
} ) ;
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 ) ) ;
35
36
}
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 } ;
45
39
}
40
+ endCursor = result . data . repository . pullRequests . pageInfo . endCursor ;
46
41
}
47
42
}
0 commit comments