Skip to content

Commit

Permalink
rw g sdl Comment --no-crud
Browse files Browse the repository at this point in the history
  • Loading branch information
tktcorporation committed Oct 10, 2022
1 parent e0b09f2 commit 398010e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
26 changes: 26 additions & 0 deletions api/src/graphql/comments.sdl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const schema = gql`
type Comment {
id: Int!
name: String!
body: String!
post: Post!
postId: Int!
createdAt: DateTime!
}
type Query {
comments: [Comment!]! @requireAuth
}
input CreateCommentInput {
name: String!
body: String!
postId: Int!
}
input UpdateCommentInput {
name: String
body: String
postId: Int
}
`
23 changes: 23 additions & 0 deletions api/src/services/comments/comments.scenarios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Prisma, Comment } from '@prisma/client'
import type { ScenarioData } from '@redwoodjs/testing/api'

export const standard = defineScenario<Prisma.CommentCreateArgs>({
comment: {
one: {
data: {
name: 'String',
body: 'String',
post: { create: { title: 'String', body: 'String' } },
},
},
two: {
data: {
name: 'String',
body: 'String',
post: { create: { title: 'String', body: 'String' } },
},
},
},
})

export type StandardScenario = ScenarioData<Comment, 'comment'>
18 changes: 18 additions & 0 deletions api/src/services/comments/comments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Comment } from '@prisma/client'

import { comments } from './comments'
import type { StandardScenario } from './comments.scenarios'

// 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: StandardScenario) => {
const result = await comments()

expect(result.length).toEqual(Object.keys(scenario.comment).length)
})
})
19 changes: 19 additions & 0 deletions api/src/services/comments/comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { QueryResolvers, CommentRelationResolvers } from 'types/graphql'

import { db } from 'src/lib/db'

export const comments: QueryResolvers['comments'] = () => {
return db.comment.findMany()
}

export const comment: QueryResolvers['comment'] = ({ id }) => {
return db.comment.findUnique({
where: { id },
})
}

export const Comment: CommentRelationResolvers = {
post: (_obj, { root }) => {
return db.comment.findUnique({ where: { id: root?.id } }).post()
},
}

0 comments on commit 398010e

Please sign in to comment.