【发布时间】:2020-10-27 14:16:37
【问题描述】:
在我的结构中,我想引入如下所示的循环依赖,以避免向后端提交两个单独的查询。有人可以建议如何在 Python 中完成此操作。
下面是示例代码:
parent.py
import graphene
class Parent(graphene.ObjectType):
id = graphene.ID()
name = graphene.String()
child= graphene.Field(Child)
child.py
import graphene
class Child(graphene.ObjectType):
id = graphene.ID()
name = graphene.String()
parent = graphene.Field(Parent)
test.py
from parent import Parent
print("TEST")
错误
ImportError: cannot import name 'Parent' from partially initialized module 'parent' (most likely due to a circular import)
更新 以下也不起作用(循环导入错误)
import graphene
class Child(graphene.ObjectType):
import app.parent as P
id = graphene.ID()
name = graphene.String()
parent = graphene.Field(P.Parent)
...
import graphene
class Parent(graphene.ObjectType):
import app.child as C
id = graphene.ID()
name = graphene.String()
child = graphene.Field(C.Child)
...
from app.parent import Parent
print("TEST")
AttributeError: partially initialized module 'app.parent' has no attribute 'Parent' (most likely due to a circular import)
【问题讨论】:
标签: python graphql graphene-python