Retrieving comment y id from backend

This commit is contained in:
David Jay
2017-02-27 14:26:20 -05:00
parent a47e2378e9
commit ab16e5bfe7
4 changed files with 33 additions and 1 deletions
+17
View File
@@ -270,6 +270,22 @@ const genRecentComments = (_, ids) => {
.then(util.arrayJoinBy(ids, 'asset_id'));
};
/**
* genComments returns the comments by the id's specified that are considered
* public comments (i.e., accepted or not rejected).
*/
const genComments = (context, ids) => {
return CommentModel.find({
id: {
$in: ids
},
status: {
$in: ['NONE', 'ACCEPTED']
}
})
.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 +293,7 @@ const genRecentComments = (_, ids) => {
*/
module.exports = (context) => ({
Comments: {
get: new DataLoader((ids) => genComments(context, ids)),
getByQuery: (query) => getCommentsByQuery(context, query),
getCountByQuery: (query) => getCommentCountByQuery(context, query),
countByAssetID: new util.SharedCacheDataLoader('Comments.countByAssetID', 3600, (ids) => getCountsByAssetID(context, ids)),
+7
View File
@@ -1,4 +1,11 @@
const Comment = {
parent({parent_id}, _, {loaders: {Comments}}) {
if (parent_id == null) {
return null;
}
return Comments.get.load(parent_id);
},
user({author_id}, _, {loaders: {Users}}) {
return Users.getByID.load(author_id);
},
+3 -1
View File
@@ -38,7 +38,9 @@ const RootQuery = {
return Comments.getByQuery(query);
},
comment(_, {id}, {loaders: {Comments}}) {
return Comments.get.load(id);
},
commentCount(_, {query: {action_type, statuses, asset_id, parent_id}}, {user, loaders: {Actions, Comments}}) {
if (user == null || !user.hasRoles('ADMIN')) {
return null;
+6
View File
@@ -149,6 +149,9 @@ input CommentCountQuery {
# Comment is the base representation of user interaction in Talk.
type Comment {
# The parent of the comment (if there is one).
parent: Comment
# The ID of the comment.
id: ID!
@@ -477,6 +480,9 @@ type RootQuery {
# Site wide settings and defaults.
settings: Settings
# Finds a specific comment based on it's id.
comment(id: ID!): Comment
# All assets. Requires the `ADMIN` role.
assets: [Asset]