You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, is it possible chain two dataloaders? I have one resolver where I need using two dataloaders, is it possible? I can't use SQL join because it's slow.
A couple of weeks ago I was experimenting with something and I needed to do something similar to this, I came up with this solution
classMyDataLoaderextendsDataLoader{constructor(batchLoadFn,options){// Initialize the DataLoader classsuper(batchLoadFn,options)// A custom function to run before .load resolves.// It doesn't do anything by default, it just returns what // was passed to itthis._beforeResolveFn=row=>row}// Overwrite the default load handler, so that we have a chance// to run our custom method// .loadMany is just a wrapper around .load, which is why we only need to// overwrite this one.asyncload(key){// Call the original load method, passing in the key,// then run our middleware function, and after it resolves,// return the result to the caller.// You can imagine that we are trying to resolve a Many to Many relationship,// where the result of calling super.load returns an array of foreign keys.// We can then pipe them through another dataloader to get the actual objectsreturnsuper.load(key).then(this._beforeResolveFn)}// Sets a custom function to run before the .load promise resolvesasyncbeforeResolve(fn){this._beforeResolveFn=fn}}// Arbitrary usageconstProductLoader=newMyDataLoader(...)// accepts productIdconstCategoryLoader=newMyDataLoader(...)// accepts categoryId// accepts productId, returns a list of categoryId's, like// `SELECT * FROM productsCategories pc WHERE pc."productId" IN (...)` constProductCategoryLoader=newMyDataLoader()// We know that the ProductCategoryLoader returns us an array of ids,// So we pipe its result through the CategoryLoader, and after that resolves// .load will return an array of categories to the ProductCategoryLoader.load calleeProductCategoryLoader.beforeResolve(categoryIds=>CategoryLoader.loadMany(categoryIds))ProductCategoryLoader.load(1)// Product id.then(categories=>{...})// An array of categories
I figured it would somewhat simplify the more complex use-cases.
It requires extending the base class, but its a transparent wrapper by default. Also, I just wrote this in a text editor, didn't test this specific implementation, because I don't have access to my code at the moment, but the general idea is the same ( in case it doesn't work after copy/paste ).
Hi, is it possible chain two dataloaders? I have one resolver where I need using two dataloaders, is it possible? I can't use SQL join because it's slow.
The text was updated successfully, but these errors were encountered: