Open gajus opened 5 months ago
Latest commit: c1239224b2ad99b8c35b10d64e81abbfca5d6ae8
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
Lazy DataLoader
Connection pool wrapper with seamless query batching.
Usage
In this example:
results
will be an array with the results of the two queries.How it works
Using the same idea as DataLoader,
LazyDataLoader
will batch all queries that are executed in the same tick. This is done by using sub-queries for every query. Example:Use cases
This is experimental approach to help with the N+1 problem that is common in GraphQL APIs.
The same problem can be solved more efficiently by using a DataLoader directly and hand crafting the queries. The latter approach is more flexible and efficient, but requires more work. In our example, it would require crafting two separate loaders and invoking them explicitly. Meanwhile, this library is a middle ground that can be used in some cases to reduce the impact of the N+1 problem by reducing the number of round trips to the database.
Considerations
I have two primary concerns with this approach:
Regarding the first point, it is conceptually the difference between:
and a union equivalent to:
The latter is still better than just doing a roundtrip for every query, but the former would be a lot more efficient.
Regarding the second point, because every query is going to be a unique batch of queries, it is going to be difficult to get query-level performance insights from the tools that we currently rely on.