forked from graphql/graphql-relay-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayconnection.js.flow
193 lines (175 loc) · 4.87 KB
/
arrayconnection.js.flow
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
184
185
186
187
188
189
190
191
192
193
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {
Connection,
ConnectionArguments,
ConnectionCursor,
} from './connectiontypes';
import {
base64,
unbase64,
} from '../utils/base64.js';
type ArraySliceMetaInfo = {
sliceStart: number;
arrayLength: number;
};
/**
* A simple function that accepts an array and connection arguments, and returns
* a connection object for use in GraphQL. It uses array offsets as pagination,
* so pagination will only work if the array is static.
*/
export function connectionFromArray<T>(
data: Array<T>,
args: ConnectionArguments
): Connection<T> {
return connectionFromArraySlice(
data,
args,
{
sliceStart: 0,
arrayLength: data.length,
}
);
}
/**
* A version of `connectionFromArray` that takes a promised array, and returns a
* promised connection.
*/
export function connectionFromPromisedArray<T>(
dataPromise: Promise<Array<T>>,
args: ConnectionArguments
): Promise<Connection<T>> {
return dataPromise.then(data => connectionFromArray(data, args));
}
/**
* Given a slice (subset) of an array, returns a connection object for use in
* GraphQL.
*
* This function is similar to `connectionFromArray`, but is intended for use
* cases where you know the cardinality of the connection, consider it too large
* to materialize the entire array, and instead wish pass in a slice of the
* total result large enough to cover the range specified in `args`.
*/
export function connectionFromArraySlice<T>(
arraySlice: Array<T>,
args: ConnectionArguments,
meta: ArraySliceMetaInfo
): Connection<T> {
const { after, before, first, last } = args;
const { sliceStart, arrayLength } = meta;
const sliceEnd = sliceStart + arraySlice.length;
const beforeOffset = getOffsetWithDefault(before, arrayLength);
const afterOffset = getOffsetWithDefault(after, -1);
let startOffset = Math.max(
sliceStart - 1,
afterOffset,
-1
) + 1;
let endOffset = Math.min(
sliceEnd,
beforeOffset,
arrayLength
);
if (typeof first === 'number') {
if (first < 0) {
throw new Error('Argument "first" must be a non-negative integer');
}
endOffset = Math.min(
endOffset,
startOffset + first
);
}
if (typeof last === 'number') {
if (last < 0) {
throw new Error('Argument "last" must be a non-negative integer');
}
startOffset = Math.max(
startOffset,
endOffset - last
);
}
// If supplied slice is too large, trim it down before mapping over it.
const slice = arraySlice.slice(
Math.max(startOffset - sliceStart, 0),
arraySlice.length - (sliceEnd - endOffset)
);
const edges = slice.map((value, index) => ({
cursor: offsetToCursor(startOffset + index),
node: value,
}));
const firstEdge = edges[0];
const lastEdge = edges[edges.length - 1];
const lowerBound = after ? (afterOffset + 1) : 0;
const upperBound = before ? beforeOffset : arrayLength;
return {
edges,
pageInfo: {
startCursor: firstEdge ? firstEdge.cursor : null,
endCursor: lastEdge ? lastEdge.cursor : null,
hasPreviousPage:
typeof last === 'number' ? startOffset > lowerBound : false,
hasNextPage:
typeof first === 'number' ? endOffset < upperBound : false,
},
};
}
/**
* A version of `connectionFromArraySlice` that takes a promised array slice,
* and returns a promised connection.
*/
export function connectionFromPromisedArraySlice<T>(
dataPromise: Promise<Array<T>>,
args: ConnectionArguments,
arrayInfo: ArraySliceMetaInfo
): Promise<Connection<T>> {
return dataPromise.then(
data => connectionFromArraySlice(data, args, arrayInfo)
);
}
const PREFIX = 'arrayconnection:';
/**
* Creates the cursor string from an offset.
*/
export function offsetToCursor(offset: number): ConnectionCursor {
return base64(PREFIX + offset);
}
/**
* Rederives the offset from the cursor string.
*/
export function cursorToOffset(cursor: ConnectionCursor): number {
return parseInt(unbase64(cursor).substring(PREFIX.length), 10);
}
/**
* Return the cursor associated with an object in an array.
*/
export function cursorForObjectInConnection<T>(
data: Array<T>,
object: T
): ?ConnectionCursor {
const offset = data.indexOf(object);
if (offset === -1) {
return null;
}
return offsetToCursor(offset);
}
/**
* Given an optional cursor and a default offset, returns the offset
* to use; if the cursor contains a valid offset, that will be used,
* otherwise it will be the default.
*/
export function getOffsetWithDefault(
cursor?: ?ConnectionCursor,
defaultOffset: number
): number {
if (typeof cursor !== 'string') {
return defaultOffset;
}
const offset = cursorToOffset(cursor);
return isNaN(offset) ? defaultOffset : offset;
}