Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getExerciseComment and getChildComments GraphQL queries #2480

Merged
merged 3 commits into from
Nov 2, 2022
Merged
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
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
Copy link
Member

Choose a reason for hiding this comment

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

This will only include the exercise comment replies and not the replies nested replies like

// Main exercise comment
{
  replies: [
    {
      // ...props -> It won't have this, right?
      replies: []
    }
    // reply2...etc
  ]
}

Copy link
Member

Choose a reason for hiding this comment

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

I think the answer is no

image

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right it will retrieve all the 'main' comments and one nested layer of replies below that, then we use the getChildComments query to get the nested replies beyond that level.

}
})
}

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