From 0a57eee5df64d5b7492e75071b86ccdb281b6b97 Mon Sep 17 00:00:00 2001 From: Loan Laux Date: Sun, 30 May 2021 21:04:06 +0400 Subject: [PATCH] fix: don't include if value is 0 Signed-off-by: Loan Laux --- lib/graphql/applyOffsetPaginationToMongoAggregate.js | 12 +++++++----- lib/graphql/applyPaginationToMongoAggregate.js | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/graphql/applyOffsetPaginationToMongoAggregate.js b/lib/graphql/applyOffsetPaginationToMongoAggregate.js index 01941127929..b22fa8c83ee 100644 --- a/lib/graphql/applyOffsetPaginationToMongoAggregate.js +++ b/lib/graphql/applyOffsetPaginationToMongoAggregate.js @@ -23,10 +23,14 @@ export default async function applyOffsetPaginationToMongoAggregate(collection, const limit = first || DEFAULT_LIMIT; const newPipeline = [...pipeline]; + const hasPreviousPage = offset > 0; + // Apply limit + skip to the provided pipeline - newPipeline.push({ - "$skip": offset - }); + if (hasPreviousPage) { + newPipeline.push({ + "$skip": offset + }); + } newPipeline.push({ "$limit": limit @@ -34,8 +38,6 @@ export default async function applyOffsetPaginationToMongoAggregate(collection, let hasNextPage = null; - const hasPreviousPage = offset > 0; - if (includeHasNextPage) { const nextCursor = collection.aggregate(pipeline); const nextDoc = await nextCursor.hasNext(); diff --git a/lib/graphql/applyPaginationToMongoAggregate.js b/lib/graphql/applyPaginationToMongoAggregate.js index eebd0abb481..8ce9217a34e 100644 --- a/lib/graphql/applyPaginationToMongoAggregate.js +++ b/lib/graphql/applyPaginationToMongoAggregate.js @@ -76,7 +76,7 @@ export default async function applyPaginationToMongoAggregate(collection, pipeli "$limit": limit }); - if (skip) { + if (skip > 0) { pipeline.push({ "$skip": skip });