Using graphql type resolvers result in the N+1 problem, that is, N extra database queries per 1 'parent' database query. This repo demonstrates this with a basic example, and one possible solution using dataloader.
TODO
npm install
npm run db:migrate
npm run db:seed
npm start
Change shouldUseDataLoader
in the config.js file to true or false, to use or not use the dataloader, respectively. nodemon will reload the server, and you will see a message in the console saying if the dataloader is being used.
- Send the following query from the graphql playground (http://localhost:4000/)
query {
books {
id
title
author {
id
name
}
}
}
-
While not using the dataloader, you will see 1 sql query executed to select all books, and N (25 in this example) sql queries executed to get all the authors.
-
Set
shouldUseDataLoader
in the config.js file to true and save the file, and resend the query in step 1. You will see 1 sql query executed to select all books, and 1 sql query executed to select all authors 🥳. -
To see the an even more drastic example, send the following query with
shouldUseDataLoader
set to false:
query {
authors {
id
name
books {
id
title
}
}
}
- Each author may have up to 5 books. Not a great deal of data, by any means, but in a scenario where every author had 5 books, 1 sql query would be executed to get all authors, and 125 sql queries would be executed to get all books ☠️.
-
built using node v12.13.1
-
npx is used to run knex and nodemon so no global installation is required. (see: package.json)
-
npm start script is configured for windows, to run on osx change the following:
"set DEBUG=knex:query && npx nodemon src/index"
to
"DEBUG=knex:query npx nodemon src/index"