from fastql import Context, DataLoader, Field, Type, Query
async def load_users(keys: list[int]) -> list[object]:
rows = await db.fetch_users(keys) # one query for every key
by_id = {row.id: row for row in rows}
return [by_id.get(key) for key in keys] # aligned to keys, one entry each
class AppContext(Context):
def __init__(self) -> None:
self.users = DataLoader(load_users)
@Type
class Post:
author_id: int
@Field
async def author(self, ctx: Context) -> "User":
return await ctx.users.load(self.author_id)