Reputation: 6926
I'm using graphql with dataloader. I have this call inside a type. My problem is that when I call "categoryPhotoLoader" I want to pass user_id as a global param for every _id. Is this possible, or I have to create a concat (${_id}_${user_id}
) and get inside the dataloader the first user_id of the keys? (split the string and get the user_id part of the first id)
async photoS3({_id, user_id}, _, {categoryPhotoLoader}) {
return categoryPhotoLoader.load(`${_id}_${user_id}`);
}
I would like something like that
async photoS3({_id, user_id}, _, {categoryPhotoLoader}) {
return categoryPhotoLoader.load(_id, {user_id: user_id});
}
Upvotes: 0
Views: 1261
Reputation: 84657
Dataloader keys do not have to be Strings, they can be Arrays, Objects or other data types. So you can do:
categoryPhotoLoader.load({ _id, user_id })
and this Object will be passed to your batch function. If you do this, you'll want to provide a cacheKeyFn
to your Loader's constructor so that dataloader can tell when two keys are equivalent. In this case, it would be simple enough to do something like:
new DataLoader(batchLoadFn, {
cacheKeyFn: ({ _id, user_id }) => ${_id}${user_id}
,
})
Be wary of using JSON.stringify
since you want to ensure the correct order of properties in the string.
Upvotes: 3