keystonejs / keystone

The most powerful headless CMS for Node.js — built with GraphQL and React
https://keystonejs.com
MIT License
8.98k stars 1.13k forks source link

Prisma `relationJoins` not being honored #9173

Open mariomnts opened 2 weeks ago

mariomnts commented 2 weeks ago

I'm using the Prisma preview feature relationJoins that as the docs say

Uses a database-level LATERAL JOIN (PostgreSQL) or correlated subqueries (MySQL) and fetches all data with a single query to the database. and I was expecting that when requesting an entity and one of its relation we would be reducing the number of queries dramatically to even just one but that's not being the case.

For example, for the following graphql request:

query keystone($where: SubscriptionContractWhereInput!) {
  author(take: 10, skip: 0) {
    id
    posts {
      id
    }
  }
}

I can see in the console 10 + 1 queries, first getting the Author table, and then querying 10 times the Posts table. I'm also seeing that we include ALL the fields of both tables despite I'm just asking for one field.

If in the same server I use the following Prisma function:

await context.prisma.Author.findMany({
  select: { id: true, posts: { select: { id: true } } },
  take: 10,
  skip: 0,
});

I can see in the logs just one query that uses the LATERAL JOIN.