Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Add Mutation for posts
Browse files Browse the repository at this point in the history
  • Loading branch information
telco2011 committed Aug 11, 2017
1 parent e52f8bb commit 61b0e07
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
30 changes: 16 additions & 14 deletions src/resolvers/blog.resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,25 @@ const blogResolver = {
Mutation: {
createPost(_, { post }) {
let errors = [];
console.log(post);
Author.findById(post.authorId).then((author) => {
return Author.findById(post.authorId).then((author) => {
if (!author) {
console.log('Author with id', post.authorId, 'not founded.');
console.log('ERRORS BEFORE:', errors.length);
errors.push({ key: 'password', message: 'The password filed must not be empty.' });
console.log('ERRORS AFTER:', errors.length);
} else {
console.log('Author', author, 'founded.');
}
}).then(() => {
if (errors.length > 0) {
console.log('THROW:', errors);
errors.push({ key: 'authorId', message: `Author with id ${post.authorId} not founded.` });
throw new ValidationError(errors);
} else {
console.log(`Author ${JSON.stringify(author)} founded.`);
return author.createPost({
title: post.title,
text: post.text,
}).then((mongoDBpost) => {
return View.update(
{ postId: mongoDBpost.id },
{ views: 0 },
{ upsert: true },
).then(() => {
return mongoDBpost;
});
});
}
console.log('END');
return post;
});
},
},
Expand Down
2 changes: 0 additions & 2 deletions src/schemas/PostInput.graphql
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
input PostInput {
id: Int
title: String
text: String
views: Int
authorId: Int
}

0 comments on commit 61b0e07

Please sign in to comment.