Open PortfolioOpen Portfolio.
โ† Back to Blog

How to Build a GraphQL DataLoader

May 29, 2026at 2:01 PM UTCBy Pocket Portfolio TeamTechnology
How to Build a GraphQL DataLoader
#graphql#dataloader#api#optimization

Building a GraphQL DataLoader is crucial for optimizing your API performance by reducing the number of requests sent to your database. This guide will walk you through the problem, solution, and key concepts needed to implement a DataLoader in a GraphQL server.

Problem

In a typical GraphQL server, resolving fields that require database access can result in multiple database calls. Consider a scenario where you have a User type with a posts field. If you fetch multiple users and their posts, naive implementation would lead to a separate database query for each user's posts, causing the "N+1" query problem.

Solution with Code

Using DataLoader, you can batch and cache these requests efficiently. The DataLoader will collect all requests for posts in a single tick and send one query to the database, returning the results in the order of the initial requests.

Here's a step-by-step guide to implementing a DataLoader:

  1. Install DataLoader: First, add the DataLoader package to your project.

    npm install dataloader
    
  2. Create a DataLoader: Initialize a DataLoader instance for fetching posts.

    const DataLoader = require('dataloader');
    const { getPostsByUserIds } = require('./database');
    
    const postsLoader = new DataLoader(async (userIds) => {
      const posts = await getPostsByUserIds(userIds);
      return userIds.map(userId => posts.filter(post => post.userId === userId));
    });
    
  3. Integrate with GraphQL Resolvers: Use the DataLoader in your GraphQL resolvers.

    const resolvers = {
      Query: {
        users: async (_, __, { dataSources }) => {
          return dataSources.userAPI.getAllUsers();
        },
      },
      User: {
        posts: (user, _, { loaders }) => {
          return loaders.postsLoader.load(user.id);
        },
      },
    };
    
  4. Pass Loaders Through Context: Ensure the DataLoader is passed through the GraphQL context.

    const server = new ApolloServer({
      typeDefs,
      resolvers,
      context: () => ({
        loaders: {
          postsLoader: postsLoader,
        },
      }),
    });
    

Key Concepts

  • Batching: DataLoader batches requests within a single tick of the event loop to minimize the number of database calls.
  • Caching: By default, DataLoader caches the results of requests, which means if the same key is requested again, it returns the cached result.
  • Optimizing SQL Queries: Instead of multiple queries, DataLoader can help transform them into a single WHERE IN query, reducing database load.

Implementing DataLoader effectively can significantly enhance the performance of your GraphQL API by addressing the "N+1" problem and minimizing redundant database queries.

How to Build a GraphQL DataLoader | Open Portfolio Blog | Open Portfolio