Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial 6.3: Fix prisma type error on comment creation #6045

Merged
merged 3 commits into from
Aug 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions docs/docs/tutorial/chapter6/comments-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,9 @@ describe('comments', () => {
input: {
name: 'Billy Bob',
body: 'What is your favorite tree bark?',
postId: scenario.post.bark.id,
post: {
connect: { id: scenario.post.bark.id },
},
},
})

Expand Down Expand Up @@ -844,7 +846,9 @@ describe('comments', () => {
input: {
name: 'Billy Bob',
body: 'What is your favorite tree bark?',
postId: scenario.post.bark.id,
post: {
connect: { id: scenario.post.bark.id },
},
},
})

Expand All @@ -865,6 +869,34 @@ We pass an optional first argument to `scenario()` which is the named scenario t

We were able to use the `id` of the post that we created in our scenario because the scenarios contain the actual database data after being inserted, not just the few fields we defined in the scenario itself. In addition to `id` we could access `createdAt` which is defaulted to `now()` in the database.

:::info What's that post…connect…id-Voodoo?! Can't we simply pass the Post's ID directly here?

What you're looking at is the [connect syntax](https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#connect-an-existing-record), which is a Prisma
core concept. And yes, we could simply pass `postId: scenario.post.bark.id` instead – as a so-called "unchecked" input. But as the name implies, the connect syntax is king
in Prisma-land.

<ShowForTs>
Note that if you try to use `postId` that would give you red squiggles, because that input would violate the `CreateCommentArgs` interface definition in
`api/src/services/comments/comments.ts`. In order to use the `postId` input, that'd need to be changed to

```ts
interface CreateCommentArgs {
input: Prisma.CommentUncheckedCreateInput
}
```

or

```ts
interface CreateCommentArgs {
input: Prisma.CommentCreateInput | Prisma.CommentUncheckedCreateInput
}
```
in case we wanted to allow both ways – which Prisma generally allows, however [it doesn't allow to pick and mix](https://stackoverflow.com/a/69169106/1246547) within the same input.
</ShowForTs>

:::

We'll test that all the fields we give to the `createComment()` function are actually created in the database, and for good measure just make sure that `createdAt` is set to a non-null value. We could test that the actual timestamp is correct, but that involves freezing the Javascript Date object so that no matter how long the test takes, you can still compare the value to `new Date` which is right *now*, down to the millisecond. While possible, it's beyond the scope of our easy, breezy tutorial since it gets [very gnarly](https://codewithhugo.com/mocking-the-current-date-in-jest-tests/)!

:::info What's up with the names for scenario data? posts.bark? Really?
Expand Down