Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.