Ali*_*ehi 5 python graphql graphene-python
在我的结构中,我想引入如下所示的循环依赖关系,以避免向后端提交两个单独的查询。有人可以建议如何在 Python 中完成此操作吗?
下面是示例代码:
父级.py
import graphene
class Parent(graphene.ObjectType):
id = graphene.ID()
name = graphene.String()
child= graphene.Field(Child)
Run Code Online (Sandbox Code Playgroud)
孩子.py
import graphene
class Child(graphene.ObjectType):
id = graphene.ID()
name = graphene.String()
parent = graphene.Field(Parent)
Run Code Online (Sandbox Code Playgroud)
测试.py
from parent import Parent
print("TEST")
Run Code Online (Sandbox Code Playgroud)
错误
ImportError: cannot import name 'Parent' from partially initialized module 'parent' (most likely due to a circular import)
Run Code Online (Sandbox Code Playgroud)
更新 以下内容也不起作用(循环导入错误)
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)
Run Code Online (Sandbox Code Playgroud)
小智 3
总而言之- graphene.Field('<class-loc>.<class-name>')。在你的情况下,graphene.Field('parent.Parent')应该graphene.Field('child.Child')完成这项工作。
遇到了完全相同的问题,并且认为必须有某种方法可以仅使用字符串表示来定义模式。在浏览代码时,我发现import_string内部使用的函数帮助我理解了如何做到这一点 -
| 归档时间: |
|
| 查看次数: |
1843 次 |
| 最近记录: |