Implementing DataLoader with Apollo Server
Learn how to use the DataLoader library with Apollo Server to drastically reduce the number of database queries a GraphQL application sends.
Our GraphQL backend is sending too many small individual requests, uncovering an important performance issue. We’d like to batch these requests together to reduce the number of queries and improve our application’s performance.
We’ll see how we can use a DataLoader library with Apollo Server and significantly optimize our application in this lesson.
Using DataLoader
To use the DataLoader library, we first need to add it to our project.
npm install --save dataloader
The library functionality is implemented in the DataLoader
type, allowing the batching of multiple queries in a single request. We need to specify how to fetch data in our application. To do this, we need to create an instance of DataLoader
and pass a function that can fetch multiple objects given a list of keys.
Get hands-on with 1200+ tech skills courses.