Query to get data ordered by the number of items in a relation

prisma

#1

Hi again!

Let’s say I have this typical datamodel, the one used in many tutorials:

type User {
  id: ID! @unique
  name: String!
  posts: [Post!]!
}

type Post {
  id: ID! @unique
  title: String!
  content: String!
  published: Boolean! @default(value: "false")
  author: User!
}

Is there a query I can build to get a list of, let’s say, the 10 Users with more Posts??
Basically I need to query ordering by “count” of Posts… but I haven’t found a way to do it :frowning_face:

Any help will be highly appreciated

Cheers :slight_smile:


#2

Beats me!!! I need solution on this too.


#3

Hey @BenjaminKelly, it’s a pain I know.

I didn’t find any information or help so I ended up adding a field to the User table called numberOfPosts and I’m running a mutation once a day to update this field with the number of posts in the relation…


#4

I would also love to see a solution to this!


#5

This feature is currently not implement in Prisma. Please leave your :+1: on this feature request if you’d like to see it in the Prisma API.