deno.land / x / pothos@release-1713397530 / packages / plugin-relay / utils / connections.ts

connections.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// @ts-nocheckimport { decodeBase64, encodeBase64, MaybePromise, PothosValidationError, SchemaTypes, } from '../../core/index.ts';import { ConnectionShape, DefaultConnectionArguments } from '../types.ts';// Since we know the return types of the connection helpers, we can// remove the MaybePromise wrappers on the ConnectionResult shapetype RemoveMaybePromiseProps<T> = { [K in keyof T]: Awaited<T[K]>;};interface ResolveOffsetConnectionOptions { args: DefaultConnectionArguments; defaultSize?: number; maxSize?: number;}export interface ResolveCursorConnectionOptions<T> { args: DefaultConnectionArguments; defaultSize?: number; maxSize?: number; toCursor: (value: T, nodes: T[]) => string;}export interface ResolveCursorConnectionArgs { before?: string; after?: string; limit: number; inverted: boolean;}interface ResolveArrayConnectionOptions { args: DefaultConnectionArguments; defaultSize?: number; maxSize?: number;}const OFFSET_CURSOR_PREFIX = "OffsetConnection:";const DEFAULT_MAX_SIZE = 100;const DEFAULT_SIZE = 20;export function offsetForArgs(options: ResolveOffsetConnectionOptions) { const { before, after, first, last } = options.args; const defaultSize = options.defaultSize ?? DEFAULT_SIZE; const maxSize = options.maxSize ?? DEFAULT_MAX_SIZE; const beforeOffset = before ? cursorToOffset(before) : Number.POSITIVE_INFINITY; const afterOffset = after ? cursorToOffset(after) : 0; if (first != null && first < 0) { throw new PothosValidationError("Argument \"first\" must be a non-negative integer"); } if (last != null && last < 0) { throw new PothosValidationError("Argument \"last\" must be a non-negative integer"); } let startOffset = after ? afterOffset + 1 : 0; let endOffset = before ? Math.max(beforeOffset, startOffset) : Number.POSITIVE_INFINITY; if (first != null) { endOffset = Math.min(endOffset, startOffset + first); } if (last != null) { if (endOffset === Number.POSITIVE_INFINITY) { throw new PothosValidationError("Argument \"last\" can only be used in combination with \"before\" or \"first\""); } startOffset = Math.max(startOffset, endOffset - last); } const size = first == null && last == null ? defaultSize : endOffset - startOffset; endOffset = Math.min(endOffset, startOffset + Math.min(size, maxSize)); const totalSize = endOffset - startOffset; return { offset: startOffset, limit: totalSize + 1, hasPreviousPage: startOffset > 0, expectedSize: totalSize, hasNextPage: (resultSize: number) => resultSize > totalSize, };}export async function resolveOffsetConnection<T, U extends Promise<readonly T[] | null> | readonly T[] | null>(options: ResolveOffsetConnectionOptions, resolve: (params: { offset: number; limit: number;}) => U & (MaybePromise<readonly T[] | null> | null)): Promise<RemoveMaybePromiseProps<ConnectionShape<SchemaTypes, NonNullable<T>, U extends NonNullable<U> ? (Promise<null> extends U ? true : false) : true, T extends NonNullable<T> ? false : { list: false; items: true;}, false>>> { const { limit, offset, expectedSize, hasPreviousPage, hasNextPage } = offsetForArgs(options); const nodes = (await resolve({ offset, limit })) as T[] | null; if (!nodes) { return nodes as never; } const edges = nodes.map((value, index) => value == null ? null : { cursor: offsetToCursor(offset + index), node: value, }); const trimmed = edges.slice(0, expectedSize); return { edges: trimmed as never, pageInfo: { startCursor: offsetToCursor(offset), endCursor: offsetToCursor(offset + trimmed.length - 1), hasPreviousPage, hasNextPage: hasNextPage(nodes.length), }, };}export function cursorToOffset(cursor: string): number { const string = decodeBase64(cursor); if (!string.startsWith(OFFSET_CURSOR_PREFIX)) { throw new PothosValidationError(`Invalid offset cursor ${OFFSET_CURSOR_PREFIX}`); } return Number.parseInt(string.slice(OFFSET_CURSOR_PREFIX.length), 10);}export function offsetToCursor(offset: number): string { return encodeBase64(`${OFFSET_CURSOR_PREFIX}${offset}`);}export function resolveArrayConnection<T>(options: ResolveArrayConnectionOptions, array: readonly T[]): RemoveMaybePromiseProps<ConnectionShape<SchemaTypes, NonNullable<T>, false, T extends NonNullable<T> ? false : { list: false; items: true;}, false>> { const { limit, offset, expectedSize, hasPreviousPage, hasNextPage } = offsetForArgs(options); const nodes = array.slice(offset, offset + limit); const edges = nodes.map((value, index) => value == null ? null : { cursor: offsetToCursor(offset + index), node: value, }); const trimmed = edges.slice(0, expectedSize); return { edges: trimmed as never, pageInfo: { startCursor: offsetToCursor(offset), endCursor: offsetToCursor(offset + trimmed.length - 1), hasPreviousPage, hasNextPage: hasNextPage(nodes.length), }, };}function parseCurserArgs(options: ResolveOffsetConnectionOptions) { const { before, after, first, last } = options.args; const defaultSize = options.defaultSize ?? DEFAULT_SIZE; const maxSize = options.maxSize ?? DEFAULT_MAX_SIZE; if (first != null && first < 0) { throw new PothosValidationError("Argument \"first\" must be a non-negative integer"); } if (last != null && last < 0) { throw new PothosValidationError("Argument \"last\" must be a non-negative integer"); } const limit = Math.min(first ?? last ?? defaultSize, maxSize) + 1; const inverted = after ? !!last && !first : (!!before && !first) || (!first && !!last); return { before: before ?? void 0, after: after ?? void 0, limit, expectedSize: limit - 1, inverted, hasPreviousPage: (resultSize: number) => (inverted ? resultSize >= limit : !!after), hasNextPage: (resultSize: number) => (inverted ? !!before : resultSize >= limit), };}type NodeType<T> = T extends (infer N)[] | Promise<(infer N)[] | null> | null ? N : never;export async function resolveCursorConnection<U extends Promise<readonly unknown[] | null> | readonly unknown[] | null>(options: ResolveCursorConnectionOptions<NodeType<U>>, resolve: (params: ResolveCursorConnectionArgs) => U): Promise<RemoveMaybePromiseProps<ConnectionShape<SchemaTypes, NodeType<U>, false, false, false>>> { const { before, after, limit, inverted, expectedSize, hasPreviousPage, hasNextPage } = parseCurserArgs(options); const nodes = (await resolve({ before, after, limit, inverted })) as NodeType<U>[] | null; if (!nodes) { return nodes as never; } const trimmed = nodes.slice(0, expectedSize); if (inverted) { trimmed.reverse(); } const edges = trimmed.map((value) => value == null ? null : { cursor: options.toCursor(value, trimmed), node: value, }); const startCursor = edges.length > 0 ? edges[0]?.cursor : options.args.after ?? options.args.before ?? ""; const endCursor = edges.length > 0 ? edges[edges.length - 1]?.cursor : options.args.after ?? options.args.before ?? ""; return { edges: edges as never, pageInfo: { startCursor, endCursor, hasPreviousPage: hasPreviousPage(nodes.length), hasNextPage: hasNextPage(nodes.length), }, };}
pothos

Version Info

Tagged at
a year ago