Skip to content

Commit

Permalink
fix: "listPublishedPages" GraphQL field (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j authored Jan 31, 2019
1 parent 8c35352 commit 372ab1e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,25 @@ export const listPublishedPages = async ({ args, Page, Category }: Object) => {
});
}

pipeline.push({
$facet: {
results: [{ $skip: (page - 1) * perPage }, { $limit: perPage }],
totalCount: [
{
$count: "count"
}
]
}
});

return Page.find({
aggregation: async ({ aggregate, QueryResult }) => {
const results = await aggregate(pipeline);
const pipelines = {
results: pipeline.concat({ $skip: (page - 1) * perPage }, { $limit: perPage }),
totalCount: pipeline.concat({
$count: "count"
})
};

const results = (await aggregate(pipelines.results)) || [];
const totalCount = get(await aggregate(pipelines.totalCount), "0.count") || 0;

const meta = createPaginationMeta({
totalCount: get(results, "totalCount.0.count") || 0,
totalCount,
page,
perPage
});
return new QueryResult(get(results, "results") || [], meta);

return new QueryResult(results, meta);
}
});
};
Expand Down
8 changes: 2 additions & 6 deletions packages/webiny-entity-mongodb/src/MongoDbDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ class MongoDbDriver extends Driver {
* @param pipeline
*/
async aggregate(collection, pipeline) {
const arrayResults = await this.getDatabase()
return await this.getDatabase()
.collection(collection)
.aggregate(pipeline)
.toArray();

return arrayResults;
}

// eslint-disable-next-line
Expand Down Expand Up @@ -93,12 +91,10 @@ class MongoDbDriver extends Driver {
if (typeof options.aggregation === "function") {
return options.aggregation({
aggregate: async pipeline => {
const arrayResults = await this.getDatabase()
return await this.getDatabase()
.collection(this.getCollectionName(entity))
.aggregate(pipeline)
.toArray();

return arrayResults[0];
},
QueryResult
});
Expand Down

0 comments on commit 372ab1e

Please sign in to comment.