What is BaseLoader?

BaseLoader, an extension of DataLoader is included in the Graphweaver package, is a powerful tool that enhances data loading capabilities in GraphQL APIs.

It builds upon DataLoader, an open-source library developed by Facebook, to provide batching, caching, and performance optimisation features.

What is DataLoader?

Before diving into BaseLoader, let's understand DataLoader.

DataLoader is a utility library designed to handle data batching and caching. It allows you to fetch multiple individual items from a data source, such as a database, in a single request instead of making separate requests for each item.

Imagine you have a Task entity that has a many-to-one relationship with a User entity, and you want to retrieve a list of users as well as a list of each user’s tasks. With DataLoader, you can batch all the requests together and fetch all the task details in a single query, significantly improving efficiency.

DataLoader also caches the results of each request, reducing the need for redundant queries in the future.

DataLoader is particularly valuable in GraphQL servers where you often need to fetch related data.

Now, let's explore how BaseLoader leverages DataLoader to enhance data loading capabilities in Graphweaver.

How does BaseLoader work?

BaseLoader provides an interface for loading single and related entities with caching, error handling, and performance optimisation features. It uses DataLoader under the hood to batch and optimise data retrieval.

BaseLoader consists of two primary functions: