diff --git a/api/src/graphql/comments.sdl.ts b/api/src/graphql/comments.sdl.ts index 272a67092..da485f814 100644 --- a/api/src/graphql/comments.sdl.ts +++ b/api/src/graphql/comments.sdl.ts @@ -23,4 +23,9 @@ export const schema = gql` body: String postId: Int } + + type Mutation { + createComment(input: CreateCommentInput!): Comment! @skipAuth + deleteComment(id: Int!): Comment! @requireAuth + } ` diff --git a/api/src/services/comments/comments.scenarios.ts b/api/src/services/comments/comments.scenarios.ts index 1e4da48d6..e4162a348 100644 --- a/api/src/services/comments/comments.scenarios.ts +++ b/api/src/services/comments/comments.scenarios.ts @@ -1,23 +1,44 @@ -import type { Prisma, Comment } from '@prisma/client' -import type { ScenarioData } from '@redwoodjs/testing/api' +import type { Prisma } from '@prisma/client' export const standard = defineScenario({ comment: { - one: { + jane: { data: { - name: 'String', - body: 'String', - post: { create: { title: 'String', body: 'String' } }, + name: 'Jane Doe', + body: 'I like trees', + post: { + create: { + title: 'Redwood Leaves', + body: 'The quick brown fox jumped over the lazy dog.', + }, + }, }, }, - two: { + john: { data: { - name: 'String', - body: 'String', - post: { create: { title: 'String', body: 'String' } }, + name: 'John Doe', + body: 'Hug a tree today', + post: { + create: { + title: 'Root Systems', + body: 'The five boxing wizards jump quickly.', + }, + }, }, }, }, }) -export type StandardScenario = ScenarioData +export const postOnly = defineScenario({ + post: { + bark: { + data: { + title: 'Bark', + body: "A tree's bark is worse than its bite", + }, + }, + }, +}) + +export type StandardScenario = typeof standard +export type PostOnlyScenario = typeof postOnly diff --git a/api/src/services/comments/comments.test.ts b/api/src/services/comments/comments.test.ts index 836dfe54d..048d4c564 100644 --- a/api/src/services/comments/comments.test.ts +++ b/api/src/services/comments/comments.test.ts @@ -1,7 +1,5 @@ -import type { Comment } from '@prisma/client' - -import { comments } from './comments' -import type { StandardScenario } from './comments.scenarios' +import { comments, createComment } from './comments' +import type { StandardScenario, PostOnlyScenario } from './comments.scenarios' // Generated boilerplate tests do not account for all circumstances // and can fail without adjustments, e.g. Float and DateTime types. @@ -15,4 +13,25 @@ describe('comments', () => { expect(result.length).toEqual(Object.keys(scenario.comment).length) }) + + scenario( + 'postOnly', + 'creates a new comment', + async (scenario: PostOnlyScenario) => { + const comment = await createComment({ + input: { + name: 'Billy Bob', + body: 'What is your favorite tree bark?', + post: { + connect: { id: scenario.post.bark.id }, + }, + }, + }) + + expect(comment.name).toEqual('Billy Bob') + expect(comment.body).toEqual('What is your favorite tree bark?') + expect(comment.postId).toEqual(scenario.post.bark.id) + expect(comment.createdAt).not.toEqual(null) + } + ) }) diff --git a/api/src/services/comments/comments.ts b/api/src/services/comments/comments.ts index 86f542294..b02111333 100644 --- a/api/src/services/comments/comments.ts +++ b/api/src/services/comments/comments.ts @@ -1,3 +1,4 @@ +import type { Prisma } from '@prisma/client' import type { QueryResolvers, CommentRelationResolvers } from 'types/graphql' import { db } from 'src/lib/db' @@ -17,3 +18,13 @@ export const Comment: CommentRelationResolvers = { return db.comment.findUnique({ where: { id: root?.id } }).post() }, } + +interface CreateCommentArgs { + input: Prisma.CommentCreateInput +} + +export const createComment = ({ input }: CreateCommentArgs) => { + return db.comment.create({ + data: input, + }) +}