Back to Browse

Using DataLoader to Avoid N+1 Queries in GraphQL

7.1K views
Feb 16, 2020
15:34

N+1 queries are incredibly easy to produce when resolving GraphQL fields. In this example, loading each album's artist produces an N+1 query. Imagine there are 10 albums... we need 10 (N) SQL queries to load each of the artists, plus 1 to load the albums. With the pattern we are going to look at today, we only need 2 SQL queries to load the data we need. This example uses Knex and Postgres, but the concepts will also work with MongoDB, MySQL, and other query builders or ORMs. Source code available at: https://github.com/leighhalliday/nextjs-graphql-example/tree/dataloader DataLoader: https://github.com/graphql/dataloader

Download

1 formats

Video Formats

360pmp423.6 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

Using DataLoader to Avoid N+1 Queries in GraphQL | NatokHD