Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Develop #108

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/graphql/applyPaginationToMongoAggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export default async function applyPaginationToMongoAggregate(collection, pipeli

if (last) {
// Get the new count after applying before/after
const totalCount = await collection.aggregate(pipeline).count();
const lastPipeline = [...pipeline];
lastPipeline.push({
"$count": "count"
});
const totalCount = await collection.aggregate(lastPipeline).count;
if (totalCount > last) {
skip = totalCount - last;
}
Expand All @@ -54,7 +58,10 @@ export default async function applyPaginationToMongoAggregate(collection, pipeli
prevPipeline.push({
"$skip": skip - 1
});
const prevCursorCount = await collection.aggregate(prevPipeline).count();
prevPipeline.push({
"$count": "count"
});
const prevCursorCount = await collection.aggregate(prevPipeline).count;
hasPreviousPage = prevCursorCount > limit;
}
}
Expand All @@ -67,7 +74,10 @@ export default async function applyPaginationToMongoAggregate(collection, pipeli
nextPipeline.push({
"$limit": limit + 1
});
const nextCursorCount = await collection.aggregate(nextPipeline).count();
nextPipeline.push({
"$count": "count"
});
const nextCursorCount = await collection.aggregate(nextPipeline).count;
hasNextPage = nextCursorCount > limit;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/getPaginatedResponseFromAggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function getPaginatedResponseFromAggregate(collection, pipeline, args, {
* Set `totalCount` inside of our objects, since we'll replace the root in the next stage.
*/
$set: {
"$objects.totalCount": "$totalCount"
"objects.totalCount": "$totalCount"
}
}, {
/**
Expand Down