Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tktcorporation committed Oct 10, 2022
1 parent 3d74401 commit a13f566
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
5 changes: 5 additions & 0 deletions api/src/graphql/comments.sdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ export const schema = gql`
body: String
postId: Int
}
type Mutation {
createComment(input: CreateCommentInput!): Comment! @skipAuth
deleteComment(id: Int!): Comment! @requireAuth
}
`
43 changes: 32 additions & 11 deletions api/src/services/comments/comments.scenarios.ts
Original file line number Diff line number Diff line change
@@ -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<Prisma.CommentCreateArgs>({
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<Comment, 'comment'>
export const postOnly = defineScenario<Prisma.PostCreateArgs>({
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
27 changes: 23 additions & 4 deletions api/src/services/comments/comments.test.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)
}
)
})
11 changes: 11 additions & 0 deletions api/src/services/comments/comments.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Prisma } from '@prisma/client'
import type { QueryResolvers, CommentRelationResolvers } from 'types/graphql'

import { db } from 'src/lib/db'
Expand All @@ -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,
})
}

0 comments on commit a13f566

Please sign in to comment.