#11736·graphql

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

Author: jotalanusseCreated Nov 26, 2024Updated Sep 12, 2025
Labelsbug/needs-info

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:

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

Main resolver:

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

Field resolvers:

typescript
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:

typescript
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)

bash
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