Created
September 28, 2021 11:00
-
-
Save cowboyd/123ddcf3f2d13bc3ac79590b5a202dce to your computer and use it in GitHub Desktop.
Creating a Relay page from a graphgen edge type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Vertex } from '@frontside/graphgen'; | |
export interface PageArgs { | |
first?: number; | |
before?: string; | |
last?: number; | |
after: string; | |
} | |
export interface PageInfo { | |
hasNextPage: boolean; | |
hasPreviousPage: boolean; | |
startCursor?: string; | |
endCursor?: string; | |
} | |
export interface Page { | |
edges: { | |
vertex: Vertex; | |
cursor: string; | |
}[]; | |
pageInfo: PageInfo; | |
} | |
export function applyRelayPagination(vertices: Vertex[], args: PageArgs): Page { | |
const range = applyCursorsToEdges(vertices, args.before, args.after); | |
const edges = edgesToReturn(range, args.first, args.last); | |
const [first] = edges; | |
const last = edges.slice().pop(); | |
return { | |
edges, | |
pageInfo: { | |
get hasNextPage() { | |
const { first, before } = args; | |
if (first != null) { | |
return range.length > first; | |
} else if (before != null) { | |
return Number(before) < range.length - 1; | |
} | |
return false; | |
}, | |
get hasPreviousPage() { | |
const { last, after } = args; | |
if (last != null) { | |
return range.length > last; | |
} else if (after != null) { | |
return Number(after) > 0; | |
} | |
return false; | |
}, | |
startCursor: first?.cursor, | |
endCursor: last?.cursor, | |
} as PageInfo, | |
}; | |
} | |
function applyCursorsToEdges(vertices: Vertex[], before?: string, after?: string) { | |
const afterIdx = after != null ? Number(after) : -1; | |
const beforeIdx = before != null ? Number(before) : vertices.length; | |
const edges = vertices.slice(afterIdx + 1, beforeIdx).map((vertex, i) => ({ | |
vertex, | |
cursor: (afterIdx + 1 + i).toString(), | |
})); | |
return edges; | |
} | |
function edgesToReturn<T>(edges: T[], first?: number, last?: number) { | |
if (first != null) { | |
if (first < 0) { | |
throw new Error(`value of first must be greater than 0, was ${first}`); | |
} | |
edges = edges.slice(0, first); | |
} | |
if (last != null) { | |
if (last < 0) { | |
throw new Error(`value of 'after' must be greater than 0, was ${last}`); | |
} | |
edges = edges.slice(-last); | |
} | |
return edges; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment