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, Enhancement] Why must dataloaders batch return a List? #39

Open
japrogramer opened this issue Aug 9, 2017 · 0 comments
Open

Comments

@japrogramer
Copy link

'List<key> and returns Promise<List<value>>, but got: {}.'

Hello I know that bat_load_fn receives a list of keys as an argument and expect a list of the same length returned. My problem with this design is if call my batch method, from which i am getting my data and i receive an out of order result for some keys and not all keys I have to than sort and fill in values for keys not returned at the correct index.

class UserDataLoader(DataLoader):
    def batch_load_fn(self, keys):
        # Here we return a promise that will result on the
        # corresponding user for each key in keys
        def process_res(res):
            res_by_uuid = {str(row.uuid): row for row in res}
            return [res_by_uuid.get(key, None) for key in keys]
        return Promise.resolve(UserType.get_queryset().order_by().filter(uuid__in=keys)).\
            then(lambda res: res if len(res) is len(keys) else process_res(res))

So than I propose that the return value be changed to a dictionary of the form {key: value ...}
with this no longer does the user need to worry about ordering, or missing data for keys
because values for keys can be retrieved with

somedict.get(key, None)
@japrogramer japrogramer changed the title [Question] Why must dataloaders batch return a List? [Question, Enhancement] Why must dataloaders batch return a List? Aug 9, 2017
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

1 participant