diff --git a/graph/loaders/comments.js b/graph/loaders/comments.js index ad78a1e92..47a8095dc 100644 --- a/graph/loaders/comments.js +++ b/graph/loaders/comments.js @@ -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} 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)), diff --git a/graph/loaders/metrics.js b/graph/loaders/metrics.js index 7cdecbc8e..8591ecefe 100644 --- a/graph/loaders/metrics.js +++ b/graph/loaders/metrics.js @@ -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) => { diff --git a/graph/typeDefs.graphql b/graph/typeDefs.graphql index 0eec34b54..c61312255 100644 --- a/graph/typeDefs.graphql +++ b/graph/typeDefs.graphql @@ -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!] } ################################################################################