dataloader icon indicating copy to clipboard operation
dataloader copied to clipboard

[REQUEST]: How to pass auth headers to dataloader

Open RahulSadaphalXebia opened this issue 3 years ago • 4 comments

What problem are you trying to solve?

I have one API which takes an array of id's and returns the array of records. This API endpoint accepts auth headers for user authentication. Now the issue is while using data loader I am getting multiple auth headers from different resolvers. So how to authenticate these multiple auth tokens, as in the end data loader will pass only one auth token to the API end point.

RahulSadaphalXebia avatar Oct 07 '22 04:10 RahulSadaphalXebia

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?

thekevinbrown avatar Jan 06 '23 03:01 thekevinbrown

@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?

RahulSadaphalXebia avatar Jan 06 '23 04:01 RahulSadaphalXebia

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:

  1. 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.
  2. 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?

thekevinbrown avatar Jan 06 '23 06:01 thekevinbrown

@thekevinbrown thanks for your response..I will try this approach

RahulSadaphalXebia avatar Jan 08 '23 16:01 RahulSadaphalXebia