Log in to GraphQL Editor
GraphQL Performance issues and how to handle them

Tomek Poniatowicz


GraphQL Performance issues and how to handle them

One of biggest GraphQL flaws is missing of some basic implementation know from which are crucial for application performance. A simple GraphQL server comes without a built-in caching or batching mechanism. This might cause some problems for you apps.

Redundant calls ... lots of redundant calls

GraphQL is about querying for specific fields on objects. The fields are intended to be stand-alone functions which means that unsophisticated GraphQL server might produce redundant calls to issue new database requests each time a field is resolved.

Let’s take a simple GraphQl Blog as an example. Our blog will use pagination to load faster, let’s say that we want to display 5 at once.

We want to get our posts along with their authors names. Our author’s details is a standard ‘user’ fields that has its own resolver so we need:

  • 1 call for list of our blog posts
  • 5 calls to get authors for each post (we need to get user for each resolver)

But what if these posts have comments? Let’s say that each blog post has 2 comments and of course each comment has its author, this gives as:

  • 1 call for list of blog posts
  • 5 calls for authors of each post
  • 5 calls to get lists of comments of each post
  • 10 calls to get the comments’ authors

This gives us 21 calls. What if we have 10 or 20 comments under each posts? The number of calls will skyrocket and this will cause serious issue with the application loading time.

Redundat calls

Batching & Caching

Fortunately there is a solution! Dataloader is a utility developed by Facebook dev team that lets you batch and cache database calls.

Batching is DataLoader's primary feature. When Dataloader sees that you’re hitting same table multiple times, it’ll batch all calls together i.e. 5 blog posts’ authors & 10 comments’ authors would be batched in a one call.

To get started you need to create loaders by providing a batch loading function.

var DataLoader = require('dataloader');

var userLoader = new DataLoader((keys) => myBatchGetUsers(keys));

The function accepts an Array of keys and returns a Promise which resolves to an Array of values. You need to load individual values from the loader. DataLoader will merge individual loads which occur the same time and then call your batch function with all requested keys.

  .then((user) => userLoader.load(user.invitedByID))
  .then((invitedBy) => console.log(`User 1 was invited by ${invitedBy}`));

// Elsewhere in your application
  .then((user) => userLoader.load(user.lastInvitedID))
  .then((lastInvited) => console.log(`User 2 last invited ${lastInvited}`));

Caching is another DataLoader’s feature. Each DataLoader instance represents a unique cache and when Dataloader detects that two blog posts, comments or their combination have the same author instead of making a new database call it will reuse the object it already has in memory.

If you want to make sure that your application running as smooth as possible I highly suggest trying DataLoader. Read more about the implemenation of DataLoader check its GitHub repo or watch "DataLoader - Source code walkthrough" by Lee Byron, one of the GraphQL creators.


Check out our other blogposts

My saas story - oct'18 first OKRs and :s
Robert Matyszewski
Robert Matyszewski
My saas story - oct'18 first OKRs and :s
4 min read
over 5 years ago
A look at what using TypeScript with GraphQL can do for you
Michał Tyszkiewicz
Michał Tyszkiewicz
A look at what using TypeScript with GraphQL can do for you
5 min read
almost 3 years ago
What's new in Node.js v14
Tomek Poniatowicz
Tomek Poniatowicz
What's new in Node.js v14
3 min read
almost 4 years ago

Ready for take-off?

Elevate your work with our editor that combines world-class visual graph, documentation and API console

Get Started with GraphQL Editor