Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eliasnorrby/prisma-dataloader-issue-reproduction
https://github.com/eliasnorrby/prisma-dataloader-issue-reproduction
Last synced: about 23 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/eliasnorrby/prisma-dataloader-issue-reproduction
- Owner: eliasnorrby
- Created: 2024-02-28T23:13:30.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-02-29T00:00:09.000Z (8 months ago)
- Last Synced: 2024-02-29T01:23:21.184Z (8 months ago)
- Language: TypeScript
- Size: 87.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Reproduction repo for prisma issue #23313
Related issue: prisma/prisma#23313
[From the docs on solving the n + 1 problem:](https://www.prisma.io/docs/orm/prisma-client/queries/query-optimization-performance#solving-n1-in-graphql-with-findunique-and-prisma-clients-dataloader)
> The Prisma Client dataloader automatically batches findUnique queries that ✔ occur in the same tick and ✔ have the same `where` and `include` parameters.
This reproduction aims to demonstrate how batching of `findUnique` calls fails for certain query arguments.
Changes have been applied to `prisma.service.ts` to add debug logging and to `resolvers.user.ts` to add test cases.
## Getting started
```
pnpm install
npx prisma migrate dev
pnpm dev
```Open `http://localhost:3000/graphql`. Run the following query:
```graphql
query Test {
allUsers {
id
name
posts {
id
}
}
}
```**Expected result**: should result in 3 db queries. The first is for `user.findMany`. The second should be a batched query of users, corresponding to `user.findUnique` in the `posts` resolved field. The third should be a `posts` query to get posts for all users.
The actual result depends on the case applied in `resolvers.user.ts`. Without any extra args uncommented, batching works as expected.
Adding a simple arg, like `name: 'Alice'` also results in a single query to get users (although the API result won't contain all posts now – it's a contrived example).
Adding any variation of `AND` / `OR` breaks batching. Now each user gets fetched using an individual query, and their posts are fetched individually as well.
Case 7 is perhaps the most simple breaking case. Making this change:
```diff
return this.prismaService.user
.findUnique({
where: {
id: user.id,
+ AND: [],
}
})
.posts()
```makes the total number of queries go from 3 to 7.