Skip to content

Commit

Permalink
Merge pull request #76 from outgrow/trunk
Browse files Browse the repository at this point in the history
Don't include `$skip` if value is 0
  • Loading branch information
Akarshit committed Jun 1, 2021
2 parents a7fbc6b + 0a57eee commit a6e0cf9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions lib/graphql/applyOffsetPaginationToMongoAggregate.js
Expand Up @@ -23,19 +23,21 @@ 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
});

let hasNextPage = null;

const hasPreviousPage = offset > 0;

if (includeHasNextPage) {
const nextCursor = collection.aggregate(pipeline);
const nextDoc = await nextCursor.hasNext();
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/applyPaginationToMongoAggregate.js
Expand Up @@ -76,7 +76,7 @@ export default async function applyPaginationToMongoAggregate(collection, pipeli
"$limit": limit
});

if (skip) {
if (skip > 0) {
pipeline.push({
"$skip": skip
});
Expand Down

0 comments on commit a6e0cf9

Please sign in to comment.