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

DataLoader.load(None) explicitly disallowed. #52

Open
bryanhelmig opened this issue Feb 19, 2018 · 1 comment
Open

DataLoader.load(None) explicitly disallowed. #52

bryanhelmig opened this issue Feb 19, 2018 · 1 comment

Comments

@bryanhelmig
Copy link

bryanhelmig commented Feb 19, 2018

This refers to this section of code:

def load(self, key=None):
'''
Loads a key, returning a `Promise` for the value represented by that key.
'''
if key is None:
raise TypeError((
'The loader.load() function must be called with a value,' +
'but got: {}.'
).format(key))

Which is rather limiting, consider an ORM with nullable FK:

class SomeObject(models.Model):
    friend = models.ForeignKey(SomeObject, null=True, blank=True)
    friends = models.ManyToManyField(SomeObject, blank=True)

def some_object_resolver(parent, info, **kwargs):
    # raises error when load is None! sad :(
    return dataloader.load(parent.friend_id)

def some_object_resolver(parent, info, **kwargs):
    # empty iterable is fine? yay? :)
    return dataloader.load_many([
         friend.id for friend in parent.friends.all()
    ])

For the most reusable DataLoaders it would be nice to allow None to pass through such that batch_load_fn() handles empty values without an issue.

I suppose the alternative is just to return None directly from the resolver if the friend_id is None, but for reusableness' sake it would be awesome to not add that conditional.

@bryanhelmig
Copy link
Author

@syrusakbary for your consideration!

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