syrusakbary / promise

Ultra-performant Promise implementation in Python
MIT License
362 stars 76 forks source link

DataLoader.load(None) explicitly disallowed. #52

Open bryanhelmig opened 6 years ago

bryanhelmig commented 6 years ago

This refers to this section of code:

https://github.com/syrusakbary/promise/blob/b055d1e2aa1368062bc4f265b525bf2e48fadc47/promise/dataloader.py#L52-L60

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 commented 6 years ago

@syrusakbary for your consideration!