Prisma `relationJoins` not being honored

Open
#9,173 0 comments 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
graphql, nodejs, typescript
Domain
api, backend, databases

Research direction

Start with the GraphQL request and compare its behavior with the direct Prisma Author.findMany example shown in the issue. Trace how the request resolves the posts relation and inspect the generated queries and selected fields. Done means relationJoins is honored for the GraphQL request, with the expected single query and field selection.

Written by the indexing model from the issue text.

Description

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.

Dominant language
TypeScript
Stars
10k
Forks
1.3k
Avg merge
1d 12h
Merged PRs (30d)
38

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from keystonejs/keystone

All issues in keystonejs/keystone

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.