11import { gql , TypedDocumentNode } from "@apollo/client/core" ;
22import { client } from "../graphql-client" ;
33import { GetAllOpenPRsAndCardIDs , GetAllOpenPRsAndCardIDsVariables } from "./schema/GetAllOpenPRsAndCardIDs" ;
4+ import { noNullish } from "../util/util" ;
45
56export 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) {
2223export 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}
0 commit comments