Git Product home page Git Product logo

Comments (4)

thekevinbrown avatar thekevinbrown commented on June 13, 2024 1

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?

from dataloader.

RahulSadaphalXebia avatar RahulSadaphalXebia commented on June 13, 2024 1

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

from dataloader.

thekevinbrown avatar thekevinbrown commented on June 13, 2024

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.

RahulSadaphalXebia avatar RahulSadaphalXebia commented on June 13, 2024

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.