Chapter 6 - Adding Comments to Schema - Complete

This commit is contained in:
Colin Diem
2022-04-26 01:24:39 -04:00
parent e180c93ada
commit ac15c86840
15 changed files with 307 additions and 11 deletions

View File

@@ -0,0 +1,9 @@
-- CreateTable
CREATE TABLE "Comment" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"body" TEXT NOT NULL,
"postId" INTEGER NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "Comment_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Post" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

View File

@@ -9,10 +9,11 @@ generator client {
}
model Post {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
title String
body String
createdAt DateTime @default(now())
comments Comment[]
createdAt DateTime @default(now())
}
model Contact {
@@ -24,11 +25,20 @@ model Contact {
}
model User {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
name String?
email String @unique
email String @unique
hashedPassword String
salt String
resetToken String?
resetTokenExpiresAt DateTime?
}
model Comment {
id Int @id @default(autoincrement())
name String
body String
post Post @relation(fields: [postId], references: [id])
postId Int
createdAt DateTime @default(now())
}

View 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
}
`

View 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 },
})
}

View 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",
},
},
},
})

View 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)
})
})