Logo

dev-resources.site

for different kinds of informations.

GraphQL query complexity + NestJS + Dataloader

Published at
12/24/2024
Categories
nestjs
graphql
dataloader
node
Author
kasir-barati
Categories
4 categories in total
nestjs
open
graphql
open
dataloader
open
node
open
Author
12 person written this
kasir-barati
open
GraphQL query complexity + NestJS + Dataloader

I was doing some exploration around GraphQL query complexity, depth limitation, and how to incorporate dataloader in NestJS to make my GraphQL API faster.

Heads up

  1. Just before getting into details I need to clarify that this is a contrived example.
  2. UPDATE:

Database state

  • A dockerized PostgreSQL version 17.
  • I have about 1,000 users.
  • Each user has about 100 posts.

Resolver

I am fetching only 5 posts when you execute the following query inside the resolver for the getPosts:

{
  getPosts {
    id
    author {
      id
      posts {
        id
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

But in the posts resolver I decided to fetch all of their posts (100). And that's where the bottleneck is.

Statistics before Dataloader

It was talking up to 5.5 seconds to retrieve data. And it was freaking huge. As you might have guessed it, I was using @ResolveField and sending a separate SELECT command to my underlying database.

Statistics after Dataloader

Now it fetches the same dataset in ~3.7 seconds. It is 32% performance increase. This might sound a bit disappointing but look at it this way:

  1. This is a contrived example.
  2. Take into account all those I/O costs that will be eliminated to some extend.
  3. You do not need to pay more than you have to (in pay-as-you-go payment model your database instance was constantly being hit with new requests).

Repo


There go for: apps/dataloader-example. BTW in this repo you can see my implementation of static query cost analysis which you can learn more about it here. The part that I especially proud of is where I incorporated depth of a field in the overall complexity of a field (for context look at this issue).


If this helped you consider giving me a star on my GitHub repo for this Post :).


Follow me:

Instagram: https://www.instagram.com/node.js.developers.kh/
Facebook: https://www.facebook.com/kasirbarati
X: https://x.com/kasir_barati
YouTube: https://www.youtube.com/@kasir-barati
GitHub: https://github.com/kasir-barati/
Dev.to: https://dev.to/kasir-barati
LinkedIn: https://linkedin.com/in/kasir-barati

graphql Article's
30 articles in total
Favicon
GraphQL Transforming API Development
Favicon
A Beginner’s Guide to Building GraphQL APIs with Apollo Server
Favicon
AWS Cognito + GraphQL Directive = ACL with minimal effort
Favicon
How to Add GitHub Contribution Stats to Your React App
Favicon
Netlify + FalkorDB: GRAPH Database Integration for Netlify Just Got Easier
Favicon
Setup GraphQL Mock Server
Favicon
Deploy graphql project
Favicon
Applications and Advantages of GraphQL in Modern Web Applications
Favicon
Automatically Generate REST and GraphQL APIs From Your Database
Favicon
Which One Should You Choose NEST JS or EXPRESS JS?
Favicon
Simplify Content Management with spurtCMS Powerful and Flexible
Favicon
Building REST APIs vs GraphQL: Which One is Right for Your Project?
Favicon
API Design Best Practices in 2025: REST, GraphQL, and gRPC
Favicon
GraphQL vs REST: When to Choose Which for Your Node.js Backend
Favicon
Understanding the Differences Between GraphQL and REST API Gateways
Favicon
Rails GraphQL Auth - JWT, Email & Security
Favicon
Essential APIs Every Developer Should Know: A Quick Guide
Favicon
From REST to GraphQL: Why and How I Made the Switch
Favicon
An Introduction to GraphQL: Only the Data You Need
Favicon
Effortless API Scaling: Unlock the Power of AWS AppSync
Favicon
Handling Errors in GraphQL APIs💡✨
Favicon
GraphQL query complexity + NestJS + Dataloader
Favicon
My Experience with AsyncThunk in Redux Toolkit
Favicon
GraphFusion is Now Open Source – Join Us in Building the Future of AI Knowledge Graphs 🚀
Favicon
Integrating Contentful in React: A Beginner’s Guide to Content Modeling and Fetching Data with GraphQL
Favicon
Extending your GraphQL service: Federation or Schema Stitching
Favicon
15 Best GraphQL Tools for 2025
Favicon
Introduction to GraphQL
Favicon
How to attach extra data to a GraphQL response on Apollo Server
Favicon
Using WordPress as a Data Entry Site to Power a Central API

Featured ones: