How can I use data loaders with Ariadne and Django? #594
Unanswered
trinigomez
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Somebody else may have better response, but best to my knowledge this is possible but requires running Ariadne under ASGI and wrapping ORM calls in from asgiref import sync_to_async
from aiodataloader import DataLoader
async def batch_load_users(ids):
results = await sync_to_async(User.objects.filter, thread_sensitive=True)(id__in=ids)
results_map = {u.id: u for u in results}
return [results_map.get(i) for i in ids] # This is because Dataloader expects batched result as list of items or none's corresponding to requested id's list
def load_user(info, id: str):
if "users_loader" not in info.context:
info.context["users_loader"] = DataLoader(batch_load_users)
return info.context["users_loader"].load(id)
def resolve_user(_, info, id: str):
return load_user(info, id) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi! I was wondering if it's possible to use data loaders with an ariadne-graphql-django implementation?? If it is possible, is there any documentation or examples on how to do this?
Beta Was this translation helpful? Give feedback.
All reactions