chapter 2

This commit is contained in:
Colin Diem
2022-04-14 21:57:50 -04:00
parent 6131eda9f6
commit 0c47db9001
36 changed files with 1294 additions and 9 deletions

View File

@@ -0,0 +1,7 @@
-- CreateTable
CREATE TABLE "Post" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"title" TEXT NOT NULL,
"body" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

View File

@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"

View File

@@ -8,11 +8,9 @@ generator client {
binaryTargets = "native"
}
// Define your own datamodels here and run `yarn redwood prisma migrate dev`
// to create migrations for them and apply to your dev DB.
// TODO: Please remove the following example:
model UserExample {
id Int @id @default(autoincrement())
email String @unique
name String?
model Post {
id Int @id @default(autoincrement())
title String
body String
createdAt DateTime @default(now())
}

View File

@@ -0,0 +1,29 @@
export const schema = gql`
type Post {
id: Int!
title: String!
body: String!
createdAt: DateTime!
}
type Query {
posts: [Post!]! @requireAuth
post(id: Int!): Post @requireAuth
}
input CreatePostInput {
title: String!
body: String!
}
input UpdatePostInput {
title: String
body: String
}
type Mutation {
createPost(input: CreatePostInput!): Post! @requireAuth
updatePost(id: Int!, input: UpdatePostInput!): Post! @requireAuth
deletePost(id: Int!): Post! @requireAuth
}
`

View File

@@ -0,0 +1,30 @@
import { db } from 'src/lib/db'
export const posts = () => {
return db.post.findMany()
}
export const post = ({ id }) => {
return db.post.findUnique({
where: { id },
})
}
export const createPost = ({ input }) => {
return db.post.create({
data: input,
})
}
export const updatePost = ({ id, input }) => {
return db.post.update({
data: input,
where: { id },
})
}
export const deletePost = ({ id }) => {
return db.post.delete({
where: { id },
})
}

View File

@@ -0,0 +1,6 @@
export const standard = defineScenario({
post: {
one: { data: { title: 'String', body: 'String' } },
two: { data: { title: 'String', body: 'String' } },
},
})

View File

@@ -0,0 +1,47 @@
import { posts, post, createPost, updatePost, deletePost } from './posts'
// 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('posts', () => {
scenario('returns all posts', async (scenario) => {
const result = await posts()
expect(result.length).toEqual(Object.keys(scenario.post).length)
})
scenario('returns a single post', async (scenario) => {
const result = await post({ id: scenario.post.one.id })
expect(result).toEqual(scenario.post.one)
})
scenario('creates a post', async () => {
const result = await createPost({
input: { title: 'String', body: 'String' },
})
expect(result.title).toEqual('String')
expect(result.body).toEqual('String')
})
scenario('updates a post', async (scenario) => {
const original = await post({ id: scenario.post.one.id })
const result = await updatePost({
id: original.id,
input: { title: 'String2' },
})
expect(result.title).toEqual('String2')
})
scenario('deletes a post', async (scenario) => {
const original = await deletePost({ id: scenario.post.one.id })
const result = await post({ id: original.id })
expect(result).toEqual(null)
})
})