From 03dcc0d00cf4aa5565911a1e00a09beca4901d39 Mon Sep 17 00:00:00 2001 From: productdevbook Date: Sat, 4 Nov 2023 08:59:02 +0300 Subject: [PATCH] feat: connection add context type --- playground/index.ts | 4 ++-- src/relay.ts | 10 ++++++---- src/types.ts | 3 ++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/playground/index.ts b/playground/index.ts index 9d4b5ba..aea6447 100644 --- a/playground/index.ts +++ b/playground/index.ts @@ -41,14 +41,14 @@ export const schema = createSchema({ edges: [LibraryEdge!]! pageInfo: PageInfo! } - + type Query { libraries( first: Int after: Cursor last: Int before: Cursor - ): LibraryConnection + ): LibraryConnection } `, resolvers: { diff --git a/src/relay.ts b/src/relay.ts index 6428449..acdde71 100644 --- a/src/relay.ts +++ b/src/relay.ts @@ -46,13 +46,14 @@ export function offsetForArgs(options: ResolveOffsetConnectionOptions) { export async function resolveOffsetConnection< T, U extends Promise | readonly T[] | null, + I, >( options: ResolveOffsetConnectionOptions, resolve: (params: { offset: number limit: number }) => U & (MaybePromise | null), -): Promise> { +): Promise> { const { limit, offset, expectedSize, hasPreviousPage, hasNextPage } = offsetForArgs(options) const nodes = (await resolve({ offset, limit })) as T[] | null @@ -90,10 +91,10 @@ export function offsetToCursor(offset: number): string { return Buffer.from(`${OFFSET_CURSOR_PREFIX}${offset}`).toString('base64') } -export function resolveArrayConnection( +export function resolveArrayConnection( options: ResolveArrayConnectionOptions, array: readonly T[], -): Connection { +): Connection { const { limit, offset, expectedSize, hasPreviousPage, hasNextPage } = offsetForArgs(options) const nodes = array.slice(offset, offset + limit) @@ -149,10 +150,11 @@ type NodeType = T extends Promise<(infer N)[] | null> | (infer N)[] | null ? export async function resolveCursorConnection< U extends Promise | readonly unknown[] | null, + I, >( options: ResolveCursorConnectionOptions>, resolve: (params: ResolveCursorConnectionArgs) => U, -): Promise>> { +): Promise, I>> { const { before, after, limit, inverted, expectedSize, hasPreviousPage, hasNextPage } = parseCurserArgs(options) diff --git a/src/types.ts b/src/types.ts index 1dc325e..13c1441 100644 --- a/src/types.ts +++ b/src/types.ts @@ -65,9 +65,10 @@ export interface ResolveArrayConnectionOptions { /** * A type designed to be exposed as a `Connection` over GraphQL. */ -export interface Connection { +export interface Connection { edges: Array> pageInfo: PageInfo + context?: I } /**