diff --git a/blog/lib/dynamo.js b/blog/lib/dynamo.js index ace75e9..8a384c8 100644 --- a/blog/lib/dynamo.js +++ b/blog/lib/dynamo.js @@ -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 = { @@ -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) { diff --git a/blog/lib/schema.js b/blog/lib/schema.js index e6b5e51..faf274f 100644 --- a/blog/lib/schema.js +++ b/blog/lib/schema.js @@ -1,3 +1,5 @@ +import uuid from 'uuid'; + import { GraphQLObjectType, GraphQLSchema, @@ -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", @@ -52,7 +54,7 @@ const Post = new GraphQLObjectType({ comments: { type: new GraphQLList(Comment), resolve: function(post) { - return getComments(); + return getComments(post.id); } } }) @@ -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"} diff --git a/blog/package.json b/blog/package.json index f7dccb1..0d0c3c8 100644 --- a/blog/package.json +++ b/blog/package.json @@ -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" } }