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]
          }
        }
      ]
    }
  }
}

在查询时,我需要来自 Store 模型(一、二)的字段。提前致谢

4

1 回答 1

0
class AField(MongoengineObjectType):
    class Meta:
        model = B
        interfaces = (Node,)


class BField(MongoengineObjectType):
    class Meta:
        model = A
        interfaces = (Node,)
于 2020-03-26T01:50:54.253 回答