Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions blog/lib/dynamo.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ export function createPost(post) {
});
}

export function createComment(comment) {
return new Promise(function(resolve, reject) {
var params = {
TableName: commentsTable,
Item: comment
};

docClient.get({
TableName: postsTable,
Key: {
id: comment.postId
},
AttributesToGet: [ "id" ]
}, function(err, data) {
if (err) return reject(err);
if(!data.Item) return reject(new Error(`Post with id: ${comment.postId} does not exist`));

docClient.put(params, function(err, data) {
if (err) return reject(err);
return resolve(comment);
});
})
});
}

export function getPosts() {
return new Promise(function(resolve, reject) {
var params = {
Expand Down Expand Up @@ -85,15 +110,14 @@ export function getAuthors() {
});
}

export function getComments() {
export function getComments(postId) {
return new Promise(function(resolve, reject) {
var params = {
TableName: commentsTable,
AttributesToGet: [
'id',
'content',
'author'
]
FilterExpression : 'postId = :postId',
ExpressionAttributeValues : {
':postId' : postId
}
};

docClient.scan(params, function(err, data) {
Expand Down
27 changes: 24 additions & 3 deletions blog/lib/schema.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import uuid from 'uuid';

import {
GraphQLObjectType,
GraphQLSchema,
Expand All @@ -10,7 +12,7 @@ import {
GraphQLLimitedString
} from 'graphql-custom-types';

import { getPosts, getAuthor, getAuthors, getComments, createPost } from './dynamo';
import { getPosts, getAuthor, getAuthors, getComments, createPost, createComment } from './dynamo';

const Author = new GraphQLObjectType({
name: "Author",
Expand Down Expand Up @@ -52,7 +54,7 @@ const Post = new GraphQLObjectType({
comments: {
type: new GraphQLList(Comment),
resolve: function(post) {
return getComments();
return getComments(post.id);
}
}
})
Expand Down Expand Up @@ -92,11 +94,30 @@ const Query = new GraphQLObjectType({
const Mutuation = new GraphQLObjectType({
name: 'BlogMutations',
fields: {
createComment: {
type: Comment,
description: "Create blog comment",
args: {
id: {
type: GraphQLString,
defaultValue: uuid.v4()
},
postId: {type: new GraphQLNonNull(GraphQLString)},
content: {type: new GraphQLNonNull(GraphQLString)},
author: {type: new GraphQLNonNull(GraphQLString), description: "Id of the author"}
},
resolve: function(source, args) {
return createComment(args);
}
},
createPost: {
type: Post,
description: "Create blog post",
args: {
id: {type: new GraphQLNonNull(GraphQLString)},
id: {
type: GraphQLString,
defaultValue: uuid.v4()
},
title: {type: new GraphQLLimitedString(10, 30)},
bodyContent: {type: new GraphQLNonNull(GraphQLString)},
author: {type: new GraphQLNonNull(GraphQLString), description: "Id of the author"}
Expand Down
3 changes: 2 additions & 1 deletion blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"bluebird": "^3.1.1",
"graphql": "^0.4.14",
"graphql-custom-types": "^0.2.0",
"serverless-helpers-js": "~0.0.3"
"serverless-helpers-js": "~0.0.3",
"uuid": "2.0.2"
}
}