Add a Posts Schema just to test authentication
This commit is contained in:
@ -0,0 +1,8 @@
|
||||
-- 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,
|
||||
"updatedAt" DATETIME NOT NULL
|
||||
);
|
@ -52,3 +52,11 @@ model Identity {
|
||||
@@unique([provider, uid])
|
||||
@@index(userId)
|
||||
}
|
||||
|
||||
model Post {
|
||||
id Int @id @default(autoincrement())
|
||||
title String
|
||||
body String
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
|
30
api/src/graphql/posts.sdl.ts
Normal file
30
api/src/graphql/posts.sdl.ts
Normal file
@ -0,0 +1,30 @@
|
||||
export const schema = gql`
|
||||
type Post {
|
||||
id: Int!
|
||||
title: String!
|
||||
body: String!
|
||||
createdAt: DateTime!
|
||||
updatedAt: 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
|
||||
}
|
||||
`
|
23
api/src/services/posts/posts.scenarios.ts
Normal file
23
api/src/services/posts/posts.scenarios.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import type { Prisma, Post } from '@prisma/client'
|
||||
import type { ScenarioData } from '@redwoodjs/testing/api'
|
||||
|
||||
export const standard = defineScenario<Prisma.PostCreateArgs>({
|
||||
post: {
|
||||
one: {
|
||||
data: {
|
||||
title: 'String',
|
||||
body: 'String',
|
||||
updatedAt: '2024-10-04T07:38:59.006Z',
|
||||
},
|
||||
},
|
||||
two: {
|
||||
data: {
|
||||
title: 'String',
|
||||
body: 'String',
|
||||
updatedAt: '2024-10-04T07:38:59.006Z',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
export type StandardScenario = ScenarioData<Post, 'post'>
|
55
api/src/services/posts/posts.test.ts
Normal file
55
api/src/services/posts/posts.test.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import type { Post } from '@prisma/client'
|
||||
|
||||
import { posts, post, createPost, updatePost, deletePost } from './posts'
|
||||
import type { StandardScenario } from './posts.scenarios'
|
||||
|
||||
// Generated boilerplate tests do not account for all circumstances
|
||||
// and can fail without adjustments, e.g. Float.
|
||||
// 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: StandardScenario) => {
|
||||
const result = await posts()
|
||||
|
||||
expect(result.length).toEqual(Object.keys(scenario.post).length)
|
||||
})
|
||||
|
||||
scenario('returns a single post', async (scenario: StandardScenario) => {
|
||||
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',
|
||||
updatedAt: '2024-10-04T07:38:58.985Z',
|
||||
},
|
||||
})
|
||||
|
||||
expect(result.title).toEqual('String')
|
||||
expect(result.body).toEqual('String')
|
||||
expect(result.updatedAt).toEqual(new Date('2024-10-04T07:38:58.985Z'))
|
||||
})
|
||||
|
||||
scenario('updates a post', async (scenario: StandardScenario) => {
|
||||
const original = (await post({ id: scenario.post.one.id })) as Post
|
||||
const result = await updatePost({
|
||||
id: original.id,
|
||||
input: { title: 'String2' },
|
||||
})
|
||||
|
||||
expect(result.title).toEqual('String2')
|
||||
})
|
||||
|
||||
scenario('deletes a post', async (scenario: StandardScenario) => {
|
||||
const original = (await deletePost({ id: scenario.post.one.id })) as Post
|
||||
const result = await post({ id: original.id })
|
||||
|
||||
expect(result).toEqual(null)
|
||||
})
|
||||
})
|
32
api/src/services/posts/posts.ts
Normal file
32
api/src/services/posts/posts.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import type { QueryResolvers, MutationResolvers } from 'types/graphql'
|
||||
|
||||
import { db } from 'src/lib/db'
|
||||
|
||||
export const posts: QueryResolvers['posts'] = () => {
|
||||
return db.post.findMany()
|
||||
}
|
||||
|
||||
export const post: QueryResolvers['post'] = ({ id }) => {
|
||||
return db.post.findUnique({
|
||||
where: { id },
|
||||
})
|
||||
}
|
||||
|
||||
export const createPost: MutationResolvers['createPost'] = ({ input }) => {
|
||||
return db.post.create({
|
||||
data: input,
|
||||
})
|
||||
}
|
||||
|
||||
export const updatePost: MutationResolvers['updatePost'] = ({ id, input }) => {
|
||||
return db.post.update({
|
||||
data: input,
|
||||
where: { id },
|
||||
})
|
||||
}
|
||||
|
||||
export const deletePost: MutationResolvers['deletePost'] = ({ id }) => {
|
||||
return db.post.delete({
|
||||
where: { id },
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user