-
Notifications
You must be signed in to change notification settings - Fork 769
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
Enhance docs for DataLoader & django 4.0+ async examples #1390
Comments
+1 |
1 similar comment
+1 |
PR #1394 will address the async support! |
PR #1394 to support async is ready for review! |
Guys, Are we in a situation where DataLoaders don't work in any way and they are just there in the documentation? Or do I need to use a ASGI server for them to work ? There is no mention in the documentation about this. The async/await examples are only found on the DataLoaders page without any mention on initial setup for them to work. In the meantime I found this package that works really well (That is also mentioned in the Ariadne graphql library): https://github.com/jkimbo/graphql-sync-dataloaders That gives you support for graphene v3 + the new graphql core that removed Promise based dataloaders support. What is the graphene-django's v3 way to use DataLoaders in a sync manner? Can someone explain the situation more? Or what needs to be done for the examples from the docs to work? Do we need to wait for #1394 ? Also for people that run Django in a sync manner (and don't plan to move all their queries to async and wrap everything in sync_to_async and async_to_sync) I think it's also a good idea to mention the sync dataloader package above in the DataLoader docs page, just so that people know about it and don't want to move to Async Django. Ariadne are mentioning it, so it should be good to use. @jkimbo What do you think ? Thanks |
Any updates on this? |
Bump |
3 similar comments
Bump |
Bump |
Bump |
I just posted an answer to a similar question on Stackoverflow. My problem was that I couldn't even construct the But I finally got a super small example working using an class BadAsyncDataLoaderGraphQLView(GraphQLView):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.runner = asyncio.Runner()
def get_context(self, request):
context = super().get_context(request)
event_loop = self.runner.get_loop()
context.photo_loader = PhotoFileByImageIdDataLoader(loop=event_loop)
return context
def execute_graphql_request(self, *args, **kwargs):
# this creates a new event loop for *every* request :(
with self.runner:
result = super().execute_graphql_request(*args, **kwargs)
if hasattr(result, '__await__'):
async def wait_for(value):
return await value
result = self.runner.run(wait_for(result))
return result
class ImageNode(DjangoObjectType):
photo_file = ...
@staticmethod
async def resolve_photo_file(parent, info, *args, **kwargs):
return await info.context.photo_loader.load(parent.id) But waiting for #1394 getting merged is probably a safer option 😉. Note that you probably have to decorate your class PhotoFileByImageIdDataLoader(DataLoader):
cache = False
@sync_to_async
def batch_load_fn(self, image_ids):
images = Image.objects.filter(id__in=image_ids).select_related("photo_file")
photo_file_by_image_ids = {image.id: image.photo_file for image in images}
return [photo_file_by_image_ids.get(image_id) for image_id in image_ids] |
I went through docs for dataloader & django execution examples, but they seem not working with django 4+ anymore. Django suggests using sync_to_async ( and async_to_sync) wrappers, but I can't wrap my head around that for now. It would be really cool to see some more examples on running django v4 and dataloader (aiodataloader).
Versions
What I have
What I'm getting after execution:
Important information: in JSON results, there were results, but photo_file field remains null on each object - that's where the problem is with the
resolve_photo_file
method I suppose.I followed docs at https://docs.graphene-python.org/en/latest/execution/dataloader/
Have anyone stumbled upon something similar? I'm having hard times getting this work
The text was updated successfully, but these errors were encountered: