UNPKG

relay-cursor-paging

Version:

Relay Cursor-Based Pagination Support for Sequelize

34 lines (33 loc) 1.38 kB
var _a = require("graphql-relay"), fromGlobalId = _a.fromGlobalId, toGlobalId = _a.toGlobalId; var checkPagingSanity_1 = require("./checkPagingSanity"); /** * Create a 'paging parameters' object with 'limit' and 'offset' fields based on the incoming * cursor-paging arguments. * * TODO: Handle the case when a user uses 'last' alone. */ function getPagingParameters(args) { var _a = checkPagingSanity_1.default(args), isForwardPaging = _a.isForwardPaging, isBackwardPaging = _a.isBackwardPaging; var first = args.first, last = args.last, after = args.after, before = args.before; var getId = function (cursor) { return parseInt(fromGlobalId(cursor).id, 10); }; var nextId = function (cursor) { return getId(cursor) + 1; }; if (isForwardPaging) { return { limit: first, offset: after ? nextId(after) : 0 }; } else if (isBackwardPaging) { var limit = last; var offset = getId(before) - last; // Check to see if our before-page is underflowing past the 0th item if (offset < 0) { // Adjust the limit with the underflow value limit = Math.max(last + offset, 0); offset = 0; } return { offset: offset, limit: limit }; } } Object.defineProperty(exports, "__esModule", { value: true }); exports.default = getPagingParameters;