diff --git a/graphql/resolvers.ts b/graphql/resolvers.ts index ca734d73b..2a0c95e7e 100644 --- a/graphql/resolvers.ts +++ b/graphql/resolvers.ts @@ -44,7 +44,11 @@ import { exerciseSubmissions, addExerciseSubmission } from './resolvers/exerciseSubmissionCrud' -import { addExerciseComment } from './resolvers/exerciseCommentCrud' +import { + getExerciseComments, + addExerciseComment, + getChildComments +} from './resolvers/exerciseCommentCrud' export default { Query: { @@ -60,7 +64,9 @@ export default { session, alerts, getPreviousSubmissions, - challenges + challenges, + getExerciseComments, + getChildComments }, Mutation: { diff --git a/graphql/resolvers/exerciseCommentCrud.test.js b/graphql/resolvers/exerciseCommentCrud.test.js index fe40075a2..5d37de105 100644 --- a/graphql/resolvers/exerciseCommentCrud.test.js +++ b/graphql/resolvers/exerciseCommentCrud.test.js @@ -1,5 +1,9 @@ import prismaMock from '../../__tests__/utils/prismaMock' -import { addExerciseComment } from './exerciseCommentCrud' +import { + addExerciseComment, + getExerciseComments, + getChildComments +} from './exerciseCommentCrud' describe('addExerciseComment resolver tests', () => { test('Should throw error if user is invalid or not loggedin', async () => { @@ -48,3 +52,75 @@ describe('addExerciseComment resolver tests', () => { }) }) }) + +describe('getExerciseComment resolver test', () => { + test('should return exerciseComment with id 1', async () => { + const mockContext = { req: { user: { id: 1 } } } + const mockArgs = { exerciseId: 1 } + const mockExerciseComments = [ + { + id: 1, + exerciseId: 1, + authorId: 1, + content: 'there is user', + userPic: null + }, + { + id: 2, + exerciseId: 1, + authorId: 2, + content: 'there is user 2', + userPic: null + } + ] + + prismaMock.exerciseComment.findMany.mockResolvedValue(mockExerciseComments) + await expect( + getExerciseComments(undefined, mockArgs, mockContext) + ).resolves.toEqual(mockExerciseComments) + + expect(prismaMock.exerciseComment.findMany).toBeCalledWith({ + where: { parentId: null, exerciseId: 1 }, + include: { + replies: true + } + }) + }) +}) + +describe('getChildComments resolver tests', () => { + test('should return all comments with parent id 1', async () => { + const mockContext = { req: { user: { id: 1 } } } + const mockArgs = { parentId: 1 } + const mockExerciseComments = [ + { + id: 2, + exerciseId: 1, + authorId: 1, + parentId: 1, + content: 'there is user', + userPic: null + }, + { + id: 3, + exerciseId: 1, + authorId: 2, + parentId: 1, + content: 'there is user 2', + userPic: null + } + ] + + prismaMock.exerciseComment.findMany.mockResolvedValue(mockExerciseComments) + await expect( + getChildComments(undefined, mockArgs, mockContext) + ).resolves.toEqual(mockExerciseComments) + + expect(prismaMock.exerciseComment.findMany).toBeCalledWith({ + where: { parentId: 1 }, + include: { + replies: true + } + }) + }) +}) diff --git a/graphql/resolvers/exerciseCommentCrud.ts b/graphql/resolvers/exerciseCommentCrud.ts index 616625410..710903bea 100644 --- a/graphql/resolvers/exerciseCommentCrud.ts +++ b/graphql/resolvers/exerciseCommentCrud.ts @@ -2,6 +2,32 @@ import { MutationAddExerciseCommentArgs } from '..' import { Context } from '../../@types/helpers' import prisma from '../../prisma' +export const getExerciseComments = async ( + _parent: void, + _args: { exerciseId: number } +) => { + const exerciseId = _args.exerciseId + return prisma.exerciseComment.findMany({ + where: { parentId: null, exerciseId }, + include: { + replies: true + } + }) +} + +export const getChildComments = async ( + _parent: void, + _args: { parentId: number } +) => { + const parentId = _args.parentId + return prisma.exerciseComment.findMany({ + where: { parentId }, + include: { + replies: true + } + }) +} + export const addExerciseComment = async ( _parent: void, { content, exerciseId, parentId, userPic }: MutationAddExerciseCommentArgs, diff --git a/graphql/typeDefs.ts b/graphql/typeDefs.ts index 1796213f0..3eb66c47f 100644 --- a/graphql/typeDefs.ts +++ b/graphql/typeDefs.ts @@ -15,6 +15,8 @@ export default gql` alerts: [Alert!]! getPreviousSubmissions(challengeId: Int!, userId: Int!): [Submission!] exerciseSubmissions: [ExerciseSubmission!]! + getExerciseComments(exerciseId: Int!): [ExerciseComment!]! + getChildComments(parentId: Int!): [ExerciseComment!]! } type TokenResponse {