-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2256 from HS-90/editComments
Edit Comments GraphQL query and resolver
- Loading branch information
Showing
6 changed files
with
250 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { gql } from '@apollo/client' | ||
|
||
const EDIT_COMMENT = gql` | ||
mutation editComment($id: Int!, $content: String!) { | ||
editComment(id: $id, content: $content) { | ||
id | ||
content | ||
} | ||
} | ||
` | ||
|
||
export default EDIT_COMMENT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import prismaMock from '../../__tests__/utils/prismaMock' | ||
import { editComment } from './editComment' | ||
|
||
const mockEditComment = { | ||
id: 2, | ||
content: 'i edited this comment' | ||
} | ||
|
||
describe('Edit comments resolver', () => { | ||
test('should invoke prismaMock update', () => { | ||
expect.assertions(1) | ||
prismaMock.comment.update.mockResolvedValue({ | ||
id: 2, | ||
authorId: 1 | ||
}) | ||
prismaMock.comment.findUnique.mockResolvedValue({ | ||
authorId: 1 | ||
}) | ||
expect( | ||
editComment({}, mockEditComment, { req: { user: { id: 1 } } }) | ||
).resolves.toEqual({ | ||
authorId: 1, | ||
id: 2 | ||
}) | ||
}) | ||
|
||
test('should throw error if no user.id in context', () => { | ||
expect.assertions(1) | ||
expect(editComment({}, mockEditComment, { req: {} })).rejects.toThrow( | ||
'No user' | ||
) | ||
}) | ||
|
||
test('should throw error if user.id does not match author.id', () => { | ||
expect.assertions(1) | ||
expect( | ||
editComment( | ||
{}, | ||
{ | ||
authorId: 2 | ||
}, | ||
{ req: { user: { id: 1 } } } | ||
) | ||
).rejects.toThrow('Comment is not by the user') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import prisma from '../../prisma' | ||
import { MutationEditCommentArgs } from '../index' | ||
import { Context } from '../../@types/helpers' | ||
import type { Comment } from '@prisma/client' | ||
import { withUserContainer } from '../../containers/withUserContainer' | ||
import _ from 'lodash' | ||
|
||
export const editComment = withUserContainer< | ||
Promise<Comment>, | ||
MutationEditCommentArgs | ||
>(async (_parent: void, args: MutationEditCommentArgs, ctx: Context) => { | ||
const { id, content } = args | ||
const { req } = ctx | ||
const authorId = _.get(req, 'user.id') | ||
|
||
const comment = await prisma.comment.findUnique({ | ||
where: { | ||
id | ||
} | ||
}) | ||
|
||
if (_.get(comment, 'authorId') !== authorId) | ||
throw new Error('Comment is not by the user') | ||
|
||
return prisma.comment.update({ | ||
where: { | ||
id | ||
}, | ||
data: { content } | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100107e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
c0d3-app – ./
c0d3-app-git-master-c0d3-prod.vercel.app
v2.c0d3.app
www.c0d3.com
c0d3-app-c0d3-prod.vercel.app
c0d3-app.vercel.app