Deep nested objects in django won't work due to async
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 35/100
调研方向
先从 issue 中的 Subscription、TaskType 和 TaskResultType resolver 示例开始,然后使用 Django 设置复现嵌套的 task → result → profiles subscription。跟踪 async-context 错误发生的位置,并验证完整的嵌套 subscription 能够返回 profile 数据,而无需额外的 resolver 变通方案。
由索引模型根据 Issue 内容生成。
描述
- 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?
- 主要语言
- Python
- 星标
- 270
- 派生
- 85
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
graphql-python/graphql-ws 的其他 Issue
-
难度 5/5 一周以上 新手友好度 20/100
graphql-python/graphql-ws#95 · 1 个 reaction ·
-
graphql-ws-next 未关闭
难度 5/5 一周以上 新手友好度 20/100
graphql-python/graphql-ws#90 · 4 条评论 ·
-
难度 3/5 1-2 天 新手友好度 35/100
graphql-python/graphql-ws#89 ·
-
难度 2/5 1-3 小时 新手友好度 25/100
graphql-python/graphql-ws#87 ·
-
难度 5/5 一周以上 新手友好度 18/100
graphql-python/graphql-ws#66 · 8 条评论 · 2 个 reaction ·
查看 graphql-python/graphql-ws 的全部 Issue
相似的 Issue
-
essnmx good first issue
难度 1/5 1 小时以内 新手友好度 95/100
-
难度 2/5 1-3 小时 新手友好度 65/100
syfoud/Simulated_Scepter#174 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
Giskard-AI/giskard-oss#2840 · 1 条评论 ·
-
A claim comment carrying the issue number is silently declined while the workflow reports success 未关闭area: repo bug perceived difficulty: 2
难度 2/5 1-3 小时 新手友好度 70/100
-
难度 2/5 1-3 小时 新手友好度 75/100
yeti-platform/yeti#1380 ·