Skip to content

Commit 8f0fc3b

Browse files
committed
Export getOffset, and give it a more descriptive name
In building a schema using `connectionFromArraySlice`, I've found it useful to have access to functionality like `getOffset`. This commit exposes it as an export, after changing the name to be more descriptive, do further distinguish it from the existing `cursorToOffset`. Here's an example of the function in use: resolve: async (_, args) => { const offset = getOffsetWithDefault(args.after, -1) + 1; const [articles, totalCount] = await articleLoader.readIndex( args.first, offset ); return connectionFromPromisedArraySlice( articleLoader.loadMany(articles), args, { sliceStart: offset, arrayLength: totalCount, }, ); }, Without the function, I end up reimplementing equivalent logic, such as: const after = args.after && cursorToOffset(args.after); const offset = isNaN(after) ? 0 : after + 1;
1 parent 70f20a3 commit 8f0fc3b

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/connection/arrayconnection.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ export function connectionFromArraySlice<T>(
7171
var {after, before, first, last} = args;
7272
var {sliceStart, arrayLength} = meta;
7373
var sliceEnd = sliceStart + arraySlice.length;
74-
var beforeOffset = getOffset(before, arrayLength);
75-
var afterOffset = getOffset(after, -1);
74+
var beforeOffset = getOffsetWithDefault(before, arrayLength);
75+
var afterOffset = getOffsetWithDefault(after, -1);
7676

7777
var startOffset = Math.max(
7878
sliceStart - 1,
@@ -172,7 +172,10 @@ export function cursorForObjectInConnection<T>(
172172
* to use; if the cursor contains a valid offset, that will be used,
173173
* otherwise it will be the default.
174174
*/
175-
function getOffset(cursor?: ?ConnectionCursor, defaultOffset: number): number {
175+
export function getOffsetWithDefault(
176+
cursor?: ?ConnectionCursor,
177+
defaultOffset: number
178+
): number {
176179
if (cursor == null) {
177180
return defaultOffset;
178181
}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export {
2121
connectionFromPromisedArraySlice,
2222
cursorForObjectInConnection,
2323
cursorToOffset,
24+
getOffsetWithDefault,
2425
} from './connection/arrayconnection.js';
2526

2627
// Helper for creating mutations with client mutation IDs

0 commit comments

Comments
 (0)