How do I pass custom options into the batch function DataLoader uses to resolve resources?
See original GitHub issueI have a circumstance where we want to return a modified result based on a graphQL variable. It appears that the loaders only take in an id
argument, leaving no way to pass through other options for the server to consume. Ideally, I’d like to do something like this:
const postLoader = new DataLoader((keys, options) => {
...return promise with posts
});
...
const ids = [1,2,3];
postLoader.loadMany(ids, { withCommentType: 'pending' });
The way I have solved it now is to create a separate loader for each option, but this solution seems far less scalable:
const postLoader = new DataLoader((keys) => {
...return promise with posts
});
const postWithPendingCommentsLoader = new DataLoader((keys) => {
...return promise with posts including comments that are pending review
});
...
const ids = [1,2,3];
postWithPendingCommentsLoader.loadMany(ids);
Are there any plans to allow an options passthrough like this in the future? Or am I missing an alternate solution?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:36
- Comments:12 (1 by maintainers)
Top Results From Across the Web
Configure Data Loader - Salesforce Developers
Use the Settings menu to change the Data Loader default operation settings. Open the Data Loader. Select Settings | Settings. Edit the fields...
Read more >Data sources - Apollo GraphQL Docs
Data sources are classes that Apollo Server can use to encapsulate fetching data from a particular source, such as a database or a...
Read more >Data Loader Guide - Salesforce Implementation guides
Data Loader is a client application for the bulk import or export of data. Use it to insert, update, delete, or export Salesforce...
Read more >Passing down arguments using Facebook's DataLoader
First, we are overfetching a lot of fields if we have different field requiements in the same batch request. Second, if we have...
Read more >Dataloader — absinthe v1.7.0 - HexDocs
Dataloader provides an easy way efficiently load data in batches. ... This is needed to use the various dataloader helpers to resolve a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Typically DataLoader instances are creates as part of a request context, in which case other elements of a context can be referenced directly without the need to pass them in. Passing them in by call would require fairly complex logic to partition batches based on equivalent contexts. Instead it’s preferred to just create a new instance per context.
To the original question, DataLoader expects a strict key -> value relationship and does not support additional arguments. If additional arguments are needed, they can be considered part of the key (example,
{ id: 1, withCommentType: 'pending' }
) so they can be considered different keys (and thus cached differently) from keys that might represent a similar object with different arguments.Alternatively (as well discussed above) if there are a small number of potential values for an argument, multiple DataLoaders can be created, one per potential value. However this may depend on your application domain.
We could have a set of batch functions like this.
To turn it into an object of data loaders we do
And then we can load books in this manner;
How it works? When the dataloader is created, we create a batch function with
ids => fnRef(ids)
, butfnRef
will be created at runtime when we run something likeloaders.getBooks(newDbConnection)