Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REQUEST]: How to pass auth headers to dataloader #316

Open
RahulSadaphalXebia opened this issue Oct 7, 2022 · 4 comments
Open

[REQUEST]: How to pass auth headers to dataloader #316

RahulSadaphalXebia opened this issue Oct 7, 2022 · 4 comments

Comments

@RahulSadaphalXebia
Copy link

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.

@thekevinbrown
Copy link
Contributor

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?

@RahulSadaphalXebia
Copy link
Author

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

@thekevinbrown
Copy link
Contributor

thekevinbrown commented Jan 6, 2023

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?

@RahulSadaphalXebia
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants