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
11 changes: 7 additions & 4 deletions src/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};


Expand Down
7 changes: 5 additions & 2 deletions src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -94,7 +94,10 @@ const mutationType = new GraphQLObjectType({
type: PostMutations,
resolve: () => PostMutations,
},
// TODO: TASK 3. createComment mutation
comment: {
type: CommentMutations,
resolve: () => CommentMutations,
},
},
});

Expand Down
26 changes: 23 additions & 3 deletions src/schema/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand Down