Chapter 6 - Adding Comments to Schema - Complete
This commit is contained in:
31
api/src/graphql/comments.sdl.js
Normal file
31
api/src/graphql/comments.sdl.js
Normal file
@@ -0,0 +1,31 @@
|
||||
export const schema = gql`
|
||||
type Comment {
|
||||
id: Int!
|
||||
name: String!
|
||||
body: String!
|
||||
post: Post!
|
||||
postId: Int!
|
||||
createdAt: DateTime!
|
||||
}
|
||||
|
||||
type Query {
|
||||
comments: [Comment!]! @skipAuth
|
||||
}
|
||||
|
||||
input CreateCommentInput {
|
||||
name: String!
|
||||
body: String!
|
||||
postId: Int!
|
||||
}
|
||||
|
||||
input UpdateCommentInput {
|
||||
name: String
|
||||
body: String
|
||||
postId: Int
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
createComment(input: CreateCommentInput!): Comment! @skipAuth
|
||||
deleteComment(id: Int!): Comment! @requireAuth
|
||||
}
|
||||
`
|
||||
28
api/src/services/comments/comments.js
Normal file
28
api/src/services/comments/comments.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import { db } from 'src/lib/db'
|
||||
|
||||
export const comments = () => {
|
||||
return db.comment.findMany()
|
||||
}
|
||||
|
||||
export const comment = ({ id }) => {
|
||||
return db.comment.findUnique({
|
||||
where: { id },
|
||||
})
|
||||
}
|
||||
|
||||
export const Comment = {
|
||||
post: (_obj, { root }) =>
|
||||
db.comment.findUnique({ where: { id: root.id } }).post(),
|
||||
}
|
||||
|
||||
export const createComment = ({ input }) => {
|
||||
return db.comment.create({
|
||||
data: input,
|
||||
})
|
||||
}
|
||||
|
||||
export const deleteComment = ({ id }) => {
|
||||
return db.comment.delete({
|
||||
where: { id },
|
||||
})
|
||||
}
|
||||
42
api/src/services/comments/comments.scenarios.js
Normal file
42
api/src/services/comments/comments.scenarios.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import { defineScenario } from '@redwoodjs/testing/dist/api'
|
||||
|
||||
export const standard = defineScenario({
|
||||
comment: {
|
||||
one: {
|
||||
data: {
|
||||
name: 'Jane Doe',
|
||||
body: 'I like trees.',
|
||||
post: {
|
||||
create: {
|
||||
title: 'Redwood Leaves',
|
||||
body: 'The quick brown fox jumped over the lazy dog.',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
two: {
|
||||
data: {
|
||||
name: 'John Doe',
|
||||
body: 'Hug a tree today',
|
||||
post: {
|
||||
create: {
|
||||
title: 'Root Systems',
|
||||
body: 'The five boxing wizards jump quickly',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
export const postOnly = defineScenario({
|
||||
post: {
|
||||
bark: {
|
||||
data: {
|
||||
title: 'Bark',
|
||||
body: "A tree's bark is worse than its bite",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
30
api/src/services/comments/comments.test.js
Normal file
30
api/src/services/comments/comments.test.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import { comments, createComment } from './comments'
|
||||
|
||||
// Generated boilerplate tests do not account for all circumstances
|
||||
// and can fail without adjustments, e.g. Float and DateTime types.
|
||||
// Please refer to the RedwoodJS Testing Docs:
|
||||
// https://redwoodjs.com/docs/testing#testing-services
|
||||
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
|
||||
|
||||
describe('comments', () => {
|
||||
scenario('returns all comments', async (scenario) => {
|
||||
const result = await comments()
|
||||
|
||||
expect(result.length).toEqual(Object.keys(scenario.comment).length)
|
||||
})
|
||||
|
||||
scenario('postOnly', 'creates a new comment', async (scenario) => {
|
||||
const comment = await createComment({
|
||||
input: {
|
||||
name: 'Billy Bob',
|
||||
body: 'What is your favorite tree bark?',
|
||||
postId: 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)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user