From 398010efdded57c359b16699ef955e807b057460 Mon Sep 17 00:00:00 2001 From: tkt <37575408+tktcorporation@users.noreply.github.com> Date: Mon, 10 Oct 2022 10:07:51 +0000 Subject: [PATCH] :sparkles: `rw g sdl Comment --no-crud` --- api/src/graphql/comments.sdl.ts | 26 +++++++++++++++++++ .../services/comments/comments.scenarios.ts | 23 ++++++++++++++++ api/src/services/comments/comments.test.ts | 18 +++++++++++++ api/src/services/comments/comments.ts | 19 ++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 api/src/graphql/comments.sdl.ts create mode 100644 api/src/services/comments/comments.scenarios.ts create mode 100644 api/src/services/comments/comments.test.ts create mode 100644 api/src/services/comments/comments.ts diff --git a/api/src/graphql/comments.sdl.ts b/api/src/graphql/comments.sdl.ts new file mode 100644 index 000000000..cb6f63acb --- /dev/null +++ b/api/src/graphql/comments.sdl.ts @@ -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 + } +` diff --git a/api/src/services/comments/comments.scenarios.ts b/api/src/services/comments/comments.scenarios.ts new file mode 100644 index 000000000..1e4da48d6 --- /dev/null +++ b/api/src/services/comments/comments.scenarios.ts @@ -0,0 +1,23 @@ +import type { Prisma, Comment } from '@prisma/client' +import type { ScenarioData } from '@redwoodjs/testing/api' + +export const standard = defineScenario({ + 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 diff --git a/api/src/services/comments/comments.test.ts b/api/src/services/comments/comments.test.ts new file mode 100644 index 000000000..836dfe54d --- /dev/null +++ b/api/src/services/comments/comments.test.ts @@ -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) + }) +}) diff --git a/api/src/services/comments/comments.ts b/api/src/services/comments/comments.ts new file mode 100644 index 000000000..86f542294 --- /dev/null +++ b/api/src/services/comments/comments.ts @@ -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() + }, +}