graphql-python / graphql-python/graphene-django

Enhance docs for DataLoader & django 4.0+ async examples

Open
#1,390 11 comments 10 reactions 0 assignees View on GitHub
✨enhancement
Dominant language
Python
Stars
4.4k
Forks
760
PR merge metrics
No merged PRs in 30d

Description

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**
```
aiodataloader==0.3.0
Django==4.2b
graphene==3.2
graphene-django==3.0.0
graphene-file-upload==1.3.0
graphql-core==3.2.3
graphql-relay==3.2.0
```

**What I have**

```python
from graphene_django import DjangoObjectType
from aiodataloader import DataLoader

class PhotoFileByImageIdDataLoader(DataLoader):
cache = False

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]

loader = PhotoFileByImageIdDataLoader()

class ImageNode(DjangoObjectType):
...
async def resolve_photo_file(self, *args, **kwargs):
return await loader.load(self.id)
```

What I'm getting after execution:
```json
{
"errors":[
{
"message":"Received incompatible instance \"\".",
"locations":[
{
"line":26,
"column":7
}
],
"path":[
"feed",
"edges",
0,
"node",
"images",
"edges",
0,
"node",
"photoFile"
]
},
...
],
"results": [ ... ]
}
```

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/](https://docs.graphene-python.org/en/latest/execution/dataloader/)

Have anyone stumbled upon something similar? I'm having hard times getting this work

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.