mongo模型字段在查询字符串和GraphiQL查询中不可用

问题描述 投票:1回答:1

我正在创建一个项目来查询GraphQL中的mongo模式。我有一个充当mongo集合和架构文件的模型,用于使用graphql Query映射模型。当我尝试查询mongo模型中的字段时,只会填充id,而无法检索其他字段。

下面的代码段:


models.py
class A(EmbeddedDocument):
    content_length = StringField(name="content-length", required=True)
    expires = StringField()
    destination = StringField()
    subscription = StringField()
    priority = StringField()


class B(DynamicEmbeddedDocument):
    pass

class Store(Document):
    meta = {'collection': 'event_store'}
    one = EmbeddedDocumentField(A, required=True)
    two = EmbeddedDocumentField(B, required=True)

schema.py

class StoreType(MongoengineObjectType):

    class Meta:
        model = Store
        interfaces = (Node,)

class Query(graphene.ObjectType):
    node = Node.Field()
    all_stores = MongoengineConnectionField(StoreType)
    store = graphene.Field(StoreType)

    def resolve_all_stores(parent, info, **kwargs):
        return list(EventStoreModel.objects.all())

schema = graphene.Schema(query=Query, types=[StoreType,])

在GraphIQL中查询:当前结果:

query{
  allEvents{
    edges{
      node{
        id
      }
    }
  }
}
result
{
  "data": {
    "allEvents": {
      "edges": [
        {
          "node": {
            "id": "RXZlbnQ6NWQwNjFiZTBiMjNjYTAwYTdmMDk5YTgy"
          }
        }
      ]
    }
  }
}

预期结果:

query{
  allEvents{
    edges{
      node{
        id,
        one,
        two
      }
    }
  }
}
{
  "data": {
    "allEvents": {
      "edges": [
        {
          "node": {
            "id": "RXZlbnQ6NWQwNjFiZTBiMjNjYTAwYTdmMDk5YTgy",
            "one":[data],
            "two":[data]
          }
        }
      ]
    }
  }
}

虽然查询时我需要商店模型(一,二)中的字段。在此先感谢

python mongodb graphql graphene-python flask-graphql
1个回答
0
投票
class AField(MongoengineObjectType):
    class Meta:
        model = B
        interfaces = (Node,)


class BField(MongoengineObjectType):
    class Meta:
        model = A
        interfaces = (Node,)
© www.soinside.com 2019 - 2024. All rights reserved.