Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug?]: Prisma built-in dataloader not working for GraphQL resolvers #11736

Open
1 task
jotalanusse opened this issue Nov 26, 2024 · 0 comments
Open
1 task
Labels
bug/needs-info More information is needed for reproduction

Comments

@jotalanusse
Copy link

What's not working?

Recently, while optimizing our API service, we encountered the N+1 problem in our GraphQL resolvers. This issue occurs when a separate database query is executed for each related record, resulting in multiple queries being made to retrieve additional information for associated data.

Upon further investigation, we referred to the Prisma documentation, which provides guidance on addressing this issue (link). It suggests leveraging Prisma features, such as the fluent API, to enable the database to automatically batch requests and reduce the number of queries.

The issue is that, despite following the documentation and implementing these optimizations, the queries to the database are still not being batched as expected.

We are starting to suspect that the way Redwood, or its underlying GraphQL engine, executes resolvers might be interfering with the built-in data loaders provided by Prisma.

How do we reproduce the bug?

GraphQL schema definition:

type Truck {
  id: String!
  name: String!
  organizationId: String!
  organization: Organization!
  routeRuns: [RouteRun]!
  createdAt: DateTime!
}

Main resolver:

export const trucksByOrganization: QueryResolvers['trucksByOrganization'] =
  async ({ organizationId }) => {
    return db.truck.findMany({ where: { organizationId } });
  };

Field resolvers:

export const Truck: TruckRelationResolvers = {
  organization: (_args, { root }) => db.truck.findUnique({ where: { id: root.id } }).organization(),
  routeRuns: (_args, { root }) => db.truck.findUnique({ where: { id: root.id } }).routeRuns(),
};

In the code above, the same databse instance is shared for all resolvers, where:

const db = new PrismaClient();

Given the setup described above (using findUnique and the fluent API), automatic batching should function as expected, but for some reason, it does not. Due to the private nature of the project, I cannot share the code directly. However, if a reproduction is needed to test this issue, I am willing to create one and provide it.

What's your environment? (If it applies)

System:
  OS: Linux 6.8 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
  Shell: 5.1.16 - /bin/bash
Binaries:
  Node: 18.20.4 - /tmp/yarn--1732636433916-0.04046345284337405/node
  Yarn: 1.22.19 - /tmp/yarn--1732636433916-0.04046345284337405/yarn
Browsers:
  Chrome: 131.0.6778.85
npmPackages:
  @redwoodjs/api-server: 6.6.4 => 6.6.4 
  @redwoodjs/auth-firebase-setup: 6.6.4 => 6.6.4 
  @redwoodjs/cli: 6.6.4 => 6.6.4 
  @redwoodjs/core: 6.6.4 => 6.6.4 
  @redwoodjs/internal: 6.6.4 => 6.6.4

Are you interested in working on this?

  • I'm interested in working on this
@jotalanusse jotalanusse added the bug/needs-info More information is needed for reproduction label Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/needs-info More information is needed for reproduction
Projects
None yet
Development

No branches or pull requests

1 participant