From 90c08fbc390ad2686a1afa54e8b6d26a080ccf9d Mon Sep 17 00:00:00 2001 From: tkt <37575408+tktcorporation@users.noreply.github.com> Date: Mon, 10 Oct 2022 11:50:00 +0000 Subject: [PATCH] :sparkles: finish https://redwoodjs.com/docs/tutorial/chapter6/comment-form#updating-the-service --- api/src/services/comments/comments.test.ts | 17 +++++++++++++---- api/src/services/comments/comments.ts | 8 ++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/api/src/services/comments/comments.test.ts b/api/src/services/comments/comments.test.ts index 048d4c564..076f061a5 100644 --- a/api/src/services/comments/comments.test.ts +++ b/api/src/services/comments/comments.test.ts @@ -1,3 +1,5 @@ +import { db } from 'src/lib/db' + import { comments, createComment } from './comments' import type { StandardScenario, PostOnlyScenario } from './comments.scenarios' @@ -8,11 +10,18 @@ import type { StandardScenario, PostOnlyScenario } from './comments.scenarios' // https://redwoodjs.com/docs/testing#jest-expect-type-considerations describe('comments', () => { - scenario('returns all comments', async (scenario: StandardScenario) => { - const result = await comments() + scenario( + 'returns all comments for a single post from the database', + async (scenario: StandardScenario) => { + const result = await comments({ postId: scenario.comment.jane.postId }) - expect(result.length).toEqual(Object.keys(scenario.comment).length) - }) + const post = await db.post.findUnique({ + where: { id: scenario.comment.jane.postId }, + include: { comments: true }, + }) + expect(result.length).toEqual(post.comments.length) + } + ) scenario( 'postOnly', diff --git a/api/src/services/comments/comments.ts b/api/src/services/comments/comments.ts index b02111333..ffd14fdd8 100644 --- a/api/src/services/comments/comments.ts +++ b/api/src/services/comments/comments.ts @@ -3,8 +3,12 @@ import type { QueryResolvers, CommentRelationResolvers } from 'types/graphql' import { db } from 'src/lib/db' -export const comments: QueryResolvers['comments'] = () => { - return db.comment.findMany() +export const comments: QueryResolvers['comments'] = ({ + postId, +}: Required>) => { + return db.comment.findMany({ + where: { postId }, + }) } export const comment: QueryResolvers['comment'] = ({ id }) => {