Comments (4)
There's not going to be a way to make it one call if you have to provide different auth tokens to get different users. You should be able to still batch it up per auth token though.
Two options I can see:
- Use one dataloader. In this option, in your dataloader function you group by auth token, then
Promise.all(groups.map(group => /* load the group with one token */));
This would send a single request for each token which is batched in parallel. It would get tricky though if you wanted to use the batching options and such, as dataloader doesn't understand that you're further breaking up the requests inside the function. - Use a different dataloader per auth token. This is more complex but also lets you control batching and such in a more fine-grained way. You could use currying, e.g.
(Pseudocode off the top of my head, not tested, but you should get the idea.)
const dataloaderMap = new Map<string, DataLoader>();
const loaderFunctionFor = (authToken: string) => (keys: readonly string[]) => {
// Load in here using authToken and keys as you like
}
const dataloaderForAuthToken = (authToken: string) => {
if (!dataloaderMap.has(authToken)) {
dataloaderMap.set(authToken, new Dataloader(loaderFunctionFor(authToken)));
}
return dataloaderMap.get(authToken);
}
async function yourResolver() {
// Inside the resolver you're getting user_id and auth from somewhere already
const user_id, auth;
const user = await dataloaderForAuthToken(auth).load(user_id);
}
This approach would allow you to apply the batching options and such. Does that help?
from dataloader.
@thekevinbrown thanks for your response..I will try this approach
from dataloader.
This should be as simple as "build what you need". Dataloader will call your function and pass an array of IDs. You then make the API call to get those IDs, passing the correct header from wherever it lives.
Does that help? If not, could you provide a code example and explain what you expect it to do, vs what it's doing instead?
from dataloader.
@thekevinbrown consider below list of keys
-
let keys = [
{
user_id: 1,
auth: "auth-token-1"
},
{
user_id: 2,
auth: "auth-token-2"
}
] -
As you can see here for each user id, there are separate auth tokens.
-
Now if I create only one api to fetch usersListByUserIds, then I don't have any way to pass different tokens into single api.
-
Other way is to call single api for each user id with auth token and then combine all the api responses.
-
But with this approach it will defeat the purpose of dataloader, where we call the database/api only once
-
So can u plz suggest some alternative solution for above scenario?
from dataloader.
Related Issues (20)
- Move CI to GitHub Actions
- Setup publish token for CI
- Change default branch `master` to `main` HOT 1
- Setup Renovate Bot to keep dependencies up to date HOT 3
- [QUESTION] Why aren't keys being de-duplicated even when the cache is disabled? HOT 6
- ✨ [REQUEST]: Add examples for cacheKeyFn and cacheMap HOT 2
- [QUESTION] README.md doesn't show on npmjs.com HOT 2
- CacheMap.get calls should await the promised value HOT 1
- [QUESTION] Release changes? HOT 9
- [QUESTION] name is now required in options? HOT 4
- [BUG] Readme is missing from NPM HOT 2
- [BUG] `this` context not available in typescript definitions
- [BUG] Use caution with `jest.useFakeTimers()` HOT 1
- [REQUEST] support vercel edge functions by default HOT 4
- Ever considered the use of `queueMicrotask`? HOT 1
- Coalescing multiple load() calls HOT 3
- [REQUEST] Unify the way `load` and `loadMany` handle errors
- -
- [REQUEST] Add batch grouping HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dataloader.