basic working removeCommentTag mutation

This commit is contained in:
Benjamin Goering
2017-02-28 19:59:33 +08:00
parent 4ff6bce8ec
commit 30ee80b12c
5 changed files with 77 additions and 1 deletions
+17
View File
@@ -189,18 +189,31 @@ const setCommentStatus = ({loaders: {Comments}}, {id, status}) => {
/**
* Adds a tag to a Comment
* @param {String} id identifier of the comment (uuid)
* @param {String} tag name of the tag
*/
const addCommentTag = ({user, loaders: {Comments}}, {id, tag}) => {
return CommentsService.addTag(id, tag, user.id)
.then(() => CommentsService.findById( id ));
};
/**
* Removes a tag from a Comment
* @param {String} id identifier of the comment (uuid)
* @param {String} tag name of the tag
*/
const removeCommentTag = ({user, loaders: {Comments}}, {id, tag}) => {
return CommentsService.removeTag(id, tag)
.then(() => CommentsService.findById( id ));
};
module.exports = (context) => {
let mutators = {
Comment: {
create: () => Promise.reject(errors.ErrNotAuthorized),
setCommentStatus: () => Promise.reject(errors.ErrNotAuthorized),
addCommentTag: () => Promise.reject(errors.ErrNotAuthorized),
removeCommentTag: () => Promise.reject(errors.ErrNotAuthorized),
}
};
@@ -216,5 +229,9 @@ module.exports = (context) => {
mutators.Comment.addCommentTag = (action) => addCommentTag(context, action);
}
if (context.user && context.user.can('mutation:removeCommentTag')) {
mutators.Comment.removeCommentTag = (action) => removeCommentTag(context, action);
}
return mutators;
};
+4 -1
View File
@@ -51,7 +51,10 @@ const RootMutation = {
},
addCommentTag(_, {id, tag}, {mutators: {Comment}}) {
return wrapResponse('comment')(Comment.addCommentTag({id, tag}));
}
},
removeCommentTag(_, {id, tag}, {mutators: {Comment}}) {
return wrapResponse('comment')(Comment.removeCommentTag({id, tag}));
},
};
module.exports = RootMutation;
+10
View File
@@ -638,6 +638,13 @@ type AddCommentTagResponse implements Response {
errors: [UserError]
}
# Response to removeCommentTag mutation
type RemoveCommentTagResponse implements Response {
# An array of errors relating to the mutation that occured.
comment: Comment
errors: [UserError]
}
# All mutations for the application are defined on this object.
type RootMutation {
@@ -664,6 +671,9 @@ type RootMutation {
# Add tag to comment.
addCommentTag(id: ID!, tag: String!): AddCommentTagResponse
# Remove tag from comment.
removeCommentTag(id: ID!, tag: String!): RemoveCommentTagResponse
}
################################################################################
+1
View File
@@ -158,6 +158,7 @@ const USER_GRAPH_OPERATIONS = [
'mutation:setUserStatus',
'mutation:setCommentStatus',
'mutation:addCommentTag',
'mutation:removeCommentTag',
];
/**
+45
View File
@@ -0,0 +1,45 @@
const expect = require('chai').expect;
const {graphql} = require('graphql');
const schema = require('../../../graph/schema');
const Context = require('../../../graph/context');
const UserModel = require('../../../models/user');
const SettingsService = require('../../../services/settings');
const CommentsService = require('../../../services/comments');
describe('graph.mutations.removeCommentTag', () => {
let comment;
beforeEach(async () => {
SettingsService.init();
comment = await CommentsService.publicCreate({body: `hello there! ${ String(Math.random()).slice(2)}`});
});
const query = `
mutation RemoveCommentTag ($id: ID!, $tag: String!) {
removeCommentTag(id:$id, tag:$tag) {
comment {
tags {
name
}
}
errors {
translation_key
}
}
}
`;
it('can add remove tags from comments', async () => {
const user = new UserModel({});
const context = new Context({user});
// add a tag first
await CommentsService.addTag(comment.id, 'BEST');
const response = await graphql(schema, query, {}, context, {id: comment.id, tag: 'BEST'});
if (response.errors && response.errors.length) {
console.error(response.errors);
}
expect(response.errors).to.be.empty;
expect(response.data.removeCommentTag.comment.tags).to.deep.equal([]);
});
});