Deep nested objects in django won't work due to async
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Bắt đầu với các ví dụ resolver của Subscription, TaskType và TaskResultType trong issue, sau đó tái hiện subscription lồng nhau task → result → profiles với thiết lập Django. Truy vết nơi xảy ra lỗi ngữ cảnh bất đồng bộ và xác minh rằng subscription lồng nhau đầy đủ trả về dữ liệu profile mà không cần thêm workaround cho resolver.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
- GraphQL AioWS version: (not using AioWS, only django-channels)
- Python version: 3.8.2
- Operating System: MacOSX
Description
I am trying to get clients to subscribe to my graphql with a nested object in django. The request looks like this:
subscription {
taskUpdated(id:"04a5e92bc9084ad981481ab4314a1d33", updateInterval: 3) {
id
token
result {
id,
profiles {
id
}
}
}
}
Tasks contain one result which contain multiple profiles.
What I Did
In my subscription class I used the sync_to_async method which works if used for the first level:
class Subscription(graphene.ObjectType):
task_updated = graphene.Field(
TaskType, id=graphene.String(), update_interval=graphene.Int()
)
async def resolve_task_updated(root, info, id, update_interval):
print("TRYING TO RESOLVE")
while True:
print("While true")
task = await sync_to_async(Task.objects.get, thread_sensitive=True)(
pk=id
)
yield task
await asyncio.sleep(update_interval)
Using the query only targeting task.id everything works perfectly. Once I try to target task.result (which is a new object). I receive the error:
You cannot call this from an async context - use a thread or sync_to_async.
I managed to get a workaround by updating the TaskType and updating the resolver for the result like so:
class TaskType(DjangoObjectType):
class Meta:
model = TaskModel
fields = "__all__"
result = graphene.Field(TaskResultType)
async def resolve_result(self, info):
return await sync_to_async(
TaskResult.objects.get, thread_sensitive=True
)(task=self.id)
That way I could get to the next level (task -> result). When I try to subscribe to task -> result -> profiles there is no way to receive the data anymore. The same error from above is thrown. Even if I update the ResultType accordingly to fetch all profiles async:
class TaskResultType(DjangoObjectType):
class Meta:
model = TaskResult
fields = "__all__"
profiles = graphene.List(ProfileType)
async def resolve_profiles(self, info):
return await sync_to_async(
Profile.objects.filter, thread_sensitive=True
)(task_result=self.id)
I thought dynamic querying of objects and subscribing to nested objects would be an out of the box functionality but it is really difficult. I can't be the only person using this repo to actually receive ORM data?
- Ngôn ngữ chính
- Python
- Star
- 270
- Fork
- 85
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của graphql-python/graphql-ws
-
using this with signals Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
graphql-python/graphql-ws#95 · 1 reaction ·
-
graphql-ws-next Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
graphql-python/graphql-ws#90 · 4 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
graphql-python/graphql-ws#89 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 25/100
graphql-python/graphql-ws#87 ·
-
Update to graphene v3 ? Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 18/100
graphql-python/graphql-ws#66 · 8 bình luận · 2 reaction ·
Tất cả issue của graphql-python/graphql-ws
Issue tương tự
-
essnmx good first issue
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 95/100
-
[Feature] 奇物选择添加优先级 Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
syfoud/Simulated_Scepter#174 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Giskard-AI/giskard-oss#2840 · 1 bình luận ·
-
A claim comment carrying the issue number is silently declined while the workflow reports success Đang mởarea: repo bug perceived difficulty: 2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
yeti-platform/yeti#1380 ·