I have this working now. Here’s what I did:
Step 1: Set up a Prisma relation on my Post and Comment tables. schema.prisma file changes:
Post table additions:
comments Comment[]
Comment table additions:
postId Int
post Post @relation(fields: [postId], references: [id])
I then ran yarn rw prisma migrate dev
Next, I changed my posts service (posts.ts) to
export const posts = () => {
const posts = db.post.findMany({
include: {
_count: {
select: { comments: true },
},
},
})
return posts
}
The Prisma docs for counting relations are actually pretty good
Next, I updated my graphql posts.sdl.ts by adding in a new type
type Counter {
comments: Int
}
and updating my posts type
type Post {
id: Int!
title: String!
body: String!
createdAt: DateTime!
_count: Counter
}
And finally I updated my PostsCell query
export const QUERY = gql`
query PostsQuery {
posts {
id
title
body
_count {
comments
}
}
}
`
Example output:
{
"id": 2,
"title": "qwerty",
"body": "qwerty",
"_count": {
"comments": 12
}
},
Thanks for putting me in the correct direction Rob!