diff --git a/src/fetcher.js b/src/fetcher.js index 4782c2f..8b2b858 100644 --- a/src/fetcher.js +++ b/src/fetcher.js @@ -120,10 +120,13 @@ const createPost = async (args) => { return rows[0]; }; -// TODO: TASK 3. crateComment -const createComment = async () => { - console.log('createComment'); - return {}; +const createComment = async (args) => { + const { content, author, post } = get(args, 'input', {}); + const { rows } = await db.query({ + text: 'INSERT INTO comments(id, content, author, post, timestamp) VALUES($1, $2, $3, $4, $5) RETURNING *', + values: [uuid(), content, author, post, new Date()], + }); + return rows[0]; }; diff --git a/src/schema.js b/src/schema.js index c65e7e8..b1b0689 100644 --- a/src/schema.js +++ b/src/schema.js @@ -23,7 +23,7 @@ const { PostFieldFilter, PostFieldOrder, Post, PostConnection, PostMutations, } = require('./schema/post'); const { - CommentFieldFilter, CommentFieldOrder, Comment, CommentConnection, + CommentFieldFilter, CommentFieldOrder, Comment, CommentConnection, CommentMutations, } = require('./schema/comment'); const queryType = new GraphQLObjectType({ @@ -94,7 +94,10 @@ const mutationType = new GraphQLObjectType({ type: PostMutations, resolve: () => PostMutations, }, - // TODO: TASK 3. createComment mutation + comment: { + type: CommentMutations, + resolve: () => CommentMutations, + }, }, }); diff --git a/src/schema/comment.js b/src/schema/comment.js index dac3f3b..4484edd 100644 --- a/src/schema/comment.js +++ b/src/schema/comment.js @@ -5,10 +5,11 @@ const { GraphQLEnumType, GraphQLList, GraphQLInputObjectType, + GraphQLNonNull, } = require('graphql'); const { get } = require('lodash'); -const { resolveQuery } = require('../fetcher'); +const { resolveQuery, createComment } = require('../fetcher'); const { Node, PageInfo, FilterOperation, OrderDirection, Datetime, } = require('./common'); @@ -91,8 +92,27 @@ const CommentConnection = new GraphQLObjectType({ }, }); -// TODO: TASK 3. CommentMutations and CreateCommentInput -const CommentMutations = {}; +const CreateCommentInput = new GraphQLInputObjectType({ + name: 'CreateCommentInput', + fields: { + content: { type: new GraphQLNonNull(GraphQLString) }, + author: { type: new GraphQLNonNull(GraphQLString) }, + post: { type: new GraphQLNonNull(GraphQLID) }, + }, +}); + +const CommentMutations = new GraphQLObjectType({ + name: 'CommentMutations', + fields: { + createComment: { + type: Comment, + args: { + input: { type: CreateCommentInput }, + }, + resolve: (_, args) => createComment(args), + }, + }, +}); module.exports = { CommentArgField,