question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I pass custom options into the batch function DataLoader uses to resolve resources?

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Reactions:36
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
leebyroncommented, Nov 15, 2019

Why isn’t there an option to pass in a contextual object?

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.

5reactions
RAMPKORVcommented, Jan 27, 2019

We could have a set of batch functions like this.

const batchFunctions = {
  getBooks: (con) => async (ids) => {
    let [ rows ] = await con.query('SELECT * FROM books WHERE id IN ? ORDER BY FIELD(id, ?)', ids, ids);
    return rows;
  }
}

To turn it into an object of data loaders we do

let loaders = createLoadersFromBatchFunctions(batchFunctions, {})

And then we can load books in this manner;

let bookFromOldDb = await loaders.getBooks(legacyDbConnection).load(6);
let bookFromNewDb = await loaders.getBooks(newDbConnection).load(4);

How it works? When the dataloader is created, we create a batch function with ids => fnRef(ids), but fnRef will be created at runtime when we run something like loaders.getBooks(newDbConnection)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found