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

[QUESTION] Redis dataloader can accept parameters #9

Open
AllanPinheiroDeLima opened this issue Feb 27, 2019 · 3 comments
Open

[QUESTION] Redis dataloader can accept parameters #9

AllanPinheiroDeLima opened this issue Feb 27, 2019 · 3 comments

Comments

@AllanPinheiroDeLima
Copy link

Hi, I'm using the dataloader, but I cant send parameters besides the first parameter, cache ID
Am I doing something wrong ?

@DubFriend
Copy link
Collaborator

Hmm, not sure. If you send me a code sample I could get a better diagnoses of the issue?

@AllanPinheiroDeLima
Copy link
Author

AllanPinheiroDeLima commented Mar 1, 2019

I'm using like a factory function, so I can reuse it without redeclaring all redisloader again, but, when I tried to use a function that has parameters into the redisloader, it does not accepted my parameters if it wasn't a number ( wich is the cache container )

For example:
I have a function that updates an user and the I need to clear the hash containing the cache of that specific user ( I'm caching content based on a company level ), and it goes like this:

const redisLoader = require('customRedisLoader')

async function updateUser(arg) {
  return User.update({ where: {  ...args  } }) 
}

const updateUserQuery = redisLoader(updateUser)

But the function does not receive my args, only numbers. My factory function is below

const redisClient = require('redis').createClient();
const RedisDataLoader = require('redis-dataloader')({ redis: redisClient });

var dataloader = (fn) => new RedisDataLoader(
    'avanco',
    new Dataloader(fn, { cache: false }),
    {
        cache: true
    }
)



export default dataloader```

Am I doing something wrong ?

[EDIT]

Thanks for your reply

@DubFriend
Copy link
Collaborator

DubFriend commented Mar 9, 2019

One thing that looks like it might be off is the signature of the function you are passing into the Dataloader. In new Dataloader(fn, { cache: false }). fn should be a function that takes an array of arguments (so they can be batched into a single query)

So

async function updateUser(arg) {
  return User.update({ where: {  ...args  } }) 
}

might be changed to

async function updateUser(arrayOfArgs) {
  return await Promise.all(arrayOfArgs.map(args => User.update({ where: { ...args } })))
}

It may be useful to get deeper familiarity with the Facebook Dataloader documentation as well. https://github.com/facebook/dataloader

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

No branches or pull requests

2 participants