Skip to content

Commit

Permalink
Merge pull request #2480 from HS-90/discussionsGraphQL
Browse files Browse the repository at this point in the history
getExerciseComment and getChildComments GraphQL queries
  • Loading branch information
HS-90 authored Nov 2, 2022
2 parents 55940b9 + a96ed23 commit 9fbb102
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
10 changes: 8 additions & 2 deletions graphql/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -60,7 +64,9 @@ export default {
session,
alerts,
getPreviousSubmissions,
challenges
challenges,
getExerciseComments,
getChildComments
},

Mutation: {
Expand Down
78 changes: 77 additions & 1 deletion graphql/resolvers/exerciseCommentCrud.test.js
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -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
}
})
})
})
26 changes: 26 additions & 0 deletions graphql/resolvers/exerciseCommentCrud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions graphql/typeDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

1 comment on commit 9fbb102

@vercel
Copy link

@vercel vercel bot commented on 9fbb102 Nov 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.