strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
4.01k stars 533 forks source link

Investigate if we can/should allow returning dictionaries in `load_fn` for dataloaders #1285

Open patrick91 opened 3 years ago

patrick91 commented 3 years ago

Currently we allow to return list in load functions for dataloaders, as shown in our example:

from typing import List

async def load_users(keys: List[int]) -> List[User]:
    return [User(id=key) for key in keys]

we could allow returning dictionaries too:

from typing import List

async def load_users(keys: List[int]) -> List[User]:
    return {id: User(id=key) for key in keys}

Upvote & Fund

Fund with Polar

aryaniyaps commented 3 years ago

dictionaries help avoiding duplicates, so this might be worth investigating?

jkimbo commented 3 years ago

From discord:

@AndrewIngram: The load key isn’t always a simple scalar (it could be all the resolver args, more common when batching connections and similar), so wouldn’t necessarily work as a dict key. So returning an array of the same length as the key array is safer. But I don’t see a reason not to optionally allow returning a dict.

https://discord.com/channels/689806334337482765/750088532479180840/893486642499846224