| 1 | import type { Type } from '@nestjs/common'; |
| 2 | import { Field, Int |
| 3 | |
| 4 | import { keysetCursor } |
| 5 | |
| 6 | @ObjectType('PageInfo') |
| 7 | export class PageInfo |
| 8 | @Field(() => |
| 9 | endCursor: string |
| 10 | |
| 11 | @Field() |
| 12 | hasNextPage: boolean; |
| 13 | |
| 14 | @Field() |
| 15 | hasPreviousPage: boolean; |
| 16 | |
| 17 | @Field(() => |
| 18 | startCursor: string |
| 19 | } |
| 20 | |
| 21 | export type ConnectionOf |
| 22 | nodes: T |
| 23 | edges?: { |
| 24 | pageInfo: PageInfo |
| 25 | totalCount: number |
| 26 | }; |
| 27 | |
| 28 | /** GitHub's `<Name>Edge`. */ |
| 29 | export function Edge<T |
| 30 | @ObjectType(`${name |
| 31 | class EdgeType |
| 32 | @Field() |
| 33 | cursor: string; |
| 34 | |
| 35 | @Field(() => |
| 36 | node: T |
| 37 | } |
| 38 | return EdgeType; |
| 39 | } |
| 40 | |
| 41 | /** GitHub's `<Name>Connection`. Edges are left out unless `edges` is set, for the connections gh reads them on; elsewhere gh reads `nodes`. */ |
| 42 | export function Connection<T |
| 43 | node: Type |
| 44 | name: string, |
| 45 | { edges }: |
| 46 | ) { |
| 47 | @ObjectType(`${name |
| 48 | class ConnectionType |
| 49 | @Field(() => |
| 50 | nodes: T |
| 51 | |
| 52 | @Field(() => |
| 53 | pageInfo: PageInfo |
| 54 | |
| 55 | @Field(() => |
| 56 | totalCount: number; |
| 57 | } |
| 58 | if (edges) { |
| 59 | const EdgeType = |
| 60 | Field(() => |
| 61 | ConnectionType.prototype, |
| 62 | 'edges', |
| 63 | ); |
| 64 | } |
| 65 | return ConnectionType; |
| 66 | } |
| 67 | |
| 68 | /** An offset as the opaque cursor GitHub's connections hand out. */ |
| 69 | export function offsetCursor(offset |
| 70 | return Buffer.from |
| 71 | } |
| 72 | |
| 73 | /** The offset an `offsetCursor` stands for; 0 for none or for one that is not a count. */ |
| 74 | export function cursorOffset(after |
| 75 | // The cursor comes from the client, and a negative one would slice from the end. |
| 76 | return Math.max |
| 77 | 0, |
| 78 | Math.trunc( |
| 79 | Number(after ? |
| 80 | ) || 0, |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | /** One page of a list that is already fully in memory, such as an issue's labels. `first`/`last` slice it as GitHub does; `after` is an offset cursor this function hands out as `endCursor`. */ |
| 85 | export function sliceConnection<T |
| 86 | items: T |
| 87 | { |
| 88 | first, |
| 89 | last, |
| 90 | after, |
| 91 | }: { first |
| 92 | ): ConnectionOf<T |
| 93 | const start = |
| 94 | const rest = |
| 95 | const nodes = |
| 96 | last != null |
| 97 | ? rest.slice( |
| 98 | : rest.slice( |
| 99 | const end = |
| 100 | return { |
| 101 | nodes, |
| 102 | edges: nodes.map |
| 103 | node, |
| 104 | cursor: offsetCursor(end |
| 105 | })), |
| 106 | totalCount: items.length, |
| 107 | pageInfo: { |
| 108 | hasNextPage: end < |
| 109 | hasPreviousPage: last != |
| 110 | startCursor: null, |
| 111 | endCursor: nodes.length |
| 112 | }, |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | /** The cursor that resumes an issue list after this issue, as IssuesService.getIssues pages it by `sort`. */ |
| 117 | export function issueCursor( |
| 118 | issue: { |
| 119 | sort: 'created |
| 120 | ) { |
| 121 | return keysetCursor([ |
| 122 | sort === 'updated' |
| 123 | issue.id, |
| 124 | ]); |
| 125 | } |