小编dcr*_*ekp的帖子

Python GraphQL 如何声明自引用石墨烯对象类型

我有一个 django 模型,它有一个与自身相关的外键,我想将此模型表示为 graphene ObjectType

我知道使用DjangoObjectTypegraphene_django 库执行此操作是微不足道的。

我正在寻找不使用graphene_django 的优雅python 解决方案。

我想代表的模型示例

# models.py
class Category(models.Model):
    name = models.CharField(unique=True, max_length=200)
    parent = models.ForeignKey(
        'self', on_delete=models.SET_NULL, null=True, blank=True,
        related_name='child_category')
Run Code Online (Sandbox Code Playgroud)

下面的架构显然不能扩展并且ParentCategoryType没有parent字段,所以它不是严格意义上的父级CategoryType

# schema.py
class ParentCategoryType(graphene.ObjectType):
    id = graphene.types.uuid.UUID()
    name = graphene.String()

class CategoryType(graphene.ObjectType):
    id = graphene.types.uuid.UUID()
    name = graphene.String()
    parent = graphene.Field(ParentCategoryType)
Run Code Online (Sandbox Code Playgroud)

下面的代码给出了一个CategoryType未定义的错误。

#schema.py
class CategoryType(graphene.ObjectType):
    id = graphene.types.uuid.UUID()
    name = graphene.String()
    parent = graphene.Field(CategoryType)
Run Code Online (Sandbox Code Playgroud)

非常感谢任何帮助。

python recursion graphql

5
推荐指数
1
解决办法
973
查看次数

标签 统计

graphql ×1

python ×1

recursion ×1