Fixed issue with comment hydration

This commit is contained in:
Wyatt Johnson
2017-02-22 09:32:51 -07:00
parent 2360f280d2
commit 6398e2faad
3 changed files with 28 additions and 5 deletions
+16
View File
@@ -270,6 +270,21 @@ const genRecentComments = (_, ids) => {
.then(util.arrayJoinBy(ids, 'asset_id'));
};
/**
* Returns the comment's by their id.
* @param {Object} context graph context
* @param {Array<String>} ids the comment id's to fetch
* @return {Promise} resolves to the comments
*/
const genCommentsByID = (context, ids) => {
return CommentModel.find({
id: {
$in: ids
}
})
.then(util.singleJoinBy(ids, 'id'));
};
/**
* Creates a set of loaders based on a GraphQL context.
* @param {Object} context the context of the GraphQL request
@@ -277,6 +292,7 @@ const genRecentComments = (_, ids) => {
*/
module.exports = (context) => ({
Comments: {
get: new DataLoader((ids) => genCommentsByID(context, ids)),
getByQuery: (query) => getCommentsByQuery(context, query),
getCountByQuery: (query) => getCommentCountByQuery(context, query),
countByAssetID: new util.SharedCacheDataLoader('Comments.countByAssetID', 3600, (ids) => getCountsByAssetID(context, ids)),
+10 -3
View File
@@ -106,7 +106,7 @@ const getAssetMetrics = ({loaders: {Metrics, Assets}}, {from, to, sort, limit})
* Returns a list of comments that are retrieved based on most activity within
* the indicated time range.
*/
const getCommentMetrics = ({loaders: {Metrics, Assets}}, {from, to, sort, limit}) => {
const getCommentMetrics = ({loaders: {Metrics, Comments}}, {from, to, sort, limit}) => {
let commentActionSummaries = {};
@@ -143,8 +143,15 @@ const getCommentMetrics = ({loaders: {Metrics, Assets}}, {from, to, sort, limit}
// Only keep the top `limit`.
commentIDs = commentIDs.slice(0, limit);
// Find those comments.
return Metrics.getSpecificComments.loadMany(commentIDs);
// If there are no comment's to get, then just continue with an empty
// array.
if (commentIDs.length === 0) {
return [];
}
// Find those comments, this is the final stage, so let's get all the
// fields.
return Comments.get.loadMany(commentIDs);
})
.then((comments) => comments.map((comment) => {
+2 -2
View File
@@ -458,11 +458,11 @@ type RootQuery {
# Asset metrics related to user actions are saturated into the assets
# returned.
assetMetrics(from: Date!, to: Date!, sort: ACTION_TYPE!, limit: Int = 10): [Asset]
assetMetrics(from: Date!, to: Date!, sort: ACTION_TYPE!, limit: Int = 10): [Asset!]
# Comment metrics related to user actions are saturated into the comments
# returned.
commentMetrics(from: Date!, to: Date!, sort: ACTION_TYPE!, limit: Int = 10): [Comment]
commentMetrics(from: Date!, to: Date!, sort: ACTION_TYPE!, limit: Int = 10): [Comment!]
}
################################################################################