【问题标题】:make ID field required with MongoEngineObjectType使用 MongoEngineObjectType 使 ID 字段成为必需
【发布时间】:2019-10-19 02:07:34
【问题描述】:

我将石墨烯与石墨烯-mongo 一起使用。我的 graphql 架构的类型类似于:

type Report {
   id:ID!
   name:String!
}

我的这种类型的石墨烯类是

class Product(MongoengineObjectType):

    class Meta:
        model = MongoProduct

mongoengine 类是

class MongoProduct(mng.DynamicDocument):        
    name = mng.fields.StringField(required=True)

如何使id 字段成为必填字段? GraphiQL 在name 旁边显示一个感叹号,但在id 旁边没有。

【问题讨论】:

    标签: mongodb graphql mongoengine graphene-python


    【解决方案1】:
    class MongoProduct(mng.DynamicDocument):
        id = ObjectIdField(primary_key=True, required=True)    # Optional: Add default=bson.ObjectId        
        name = mng.fields.StringField(required=True)
    

    id 也可以是 IntField 或 StringField,但我建议坚持使用 ObjectId

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      • 2019-09-08
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多