Skip to content

Commit 03dcc0d

Browse files
feat: connection add context type
1 parent ec1922a commit 03dcc0d

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

playground/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ export const schema = createSchema({
4141
edges: [LibraryEdge!]!
4242
pageInfo: PageInfo!
4343
}
44-
44+
4545
type Query {
4646
libraries(
4747
first: Int
4848
after: Cursor
4949
last: Int
5050
before: Cursor
51-
): LibraryConnection
51+
): LibraryConnection
5252
}
5353
`,
5454
resolvers: {

src/relay.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ export function offsetForArgs(options: ResolveOffsetConnectionOptions) {
4646
export async function resolveOffsetConnection<
4747
T,
4848
U extends Promise<readonly T[] | null> | readonly T[] | null,
49+
I,
4950
>(
5051
options: ResolveOffsetConnectionOptions,
5152
resolve: (params: {
5253
offset: number
5354
limit: number
5455
}) => U & (MaybePromise<readonly T[] | null> | null),
55-
): Promise<Connection<T>> {
56+
): Promise<Connection<T, I>> {
5657
const { limit, offset, expectedSize, hasPreviousPage, hasNextPage } = offsetForArgs(options)
5758

5859
const nodes = (await resolve({ offset, limit })) as T[] | null
@@ -90,10 +91,10 @@ export function offsetToCursor(offset: number): string {
9091
return Buffer.from(`${OFFSET_CURSOR_PREFIX}${offset}`).toString('base64')
9192
}
9293

93-
export function resolveArrayConnection<T>(
94+
export function resolveArrayConnection<T, I>(
9495
options: ResolveArrayConnectionOptions,
9596
array: readonly T[],
96-
): Connection<T> {
97+
): Connection<T, I> {
9798
const { limit, offset, expectedSize, hasPreviousPage, hasNextPage } = offsetForArgs(options)
9899

99100
const nodes = array.slice(offset, offset + limit)
@@ -149,10 +150,11 @@ type NodeType<T> = T extends Promise<(infer N)[] | null> | (infer N)[] | null ?
149150

150151
export async function resolveCursorConnection<
151152
U extends Promise<readonly unknown[] | null> | readonly unknown[] | null,
153+
I,
152154
>(
153155
options: ResolveCursorConnectionOptions<NodeType<U>>,
154156
resolve: (params: ResolveCursorConnectionArgs) => U,
155-
): Promise<Connection<NodeType<U>>> {
157+
): Promise<Connection<NodeType<U>, I>> {
156158
const { before, after, limit, inverted, expectedSize, hasPreviousPage, hasNextPage }
157159
= parseCurserArgs(options)
158160

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ export interface ResolveArrayConnectionOptions {
6565
/**
6666
* A type designed to be exposed as a `Connection` over GraphQL.
6767
*/
68-
export interface Connection<T> {
68+
export interface Connection<T, I> {
6969
edges: Array<Edge<T>>
7070
pageInfo: PageInfo
71+
context?: I
7172
}
7273

7374
/**

0 commit comments

Comments
 (0)