首页 > 解决方案 > 在 graphql 查询中包含参考字段

问题描述

我最近使用 graphen-mongo 开始使用 graphql,我很难弄清楚以下用例是否可以按照我现在的方式进行。

我有一个看起来像这样的模型

class ParentModel(Document):
    meta = {'collection': 'parent'}

    title = StringField()
    name = StringField()

class ChildModel(Document):
    meta = {'collection': 'child'}

    parent = ReferenceField(ParentModel)
    child_name = StringField()
    child_age = IntField()
    child_gender = EnumField(GenderEnum)

和类型定义为

class ParentType(MongoengineObjectType):
    class Meta:
        model = ParentModel
        interfaces = (Node,)

class ChildType(MongoengineObjectType):
    class Meta:
        model = ChildModel
        interfaces = (Node,)

最后,下面的查询返回parent来自 mongo 的所有 's。

class ParentQuery(ObjectType):
    parent_query = MongoengineConnectionField(ParentType)

到目前为止,一切都很好。

我现在想弄清楚的是,childparent查询parent. 有人可以请教。

注意:远离嵌入式文档

标签: pythongraphqlgraphene-pythongraphene-django

解决方案


推荐阅读