graphenemongo是一个集成了graphene和mongoengine的库
graphene-mongodb的Python项目详细描述
石墨烯蒙哥
graphennemongodb是一个集成了Graphene和MongoEngine的库。
示例
鉴于Mongoengine文档:
classUser(Document):username=StringField()creation_date=DateTimeField()favourite_color=ListField(StringField())
要为该文档生成Graphene架构,我们创建一个Graphene MongoDB.MongoSchema的子类,或者我们也可以将其称为传递模型作为第一个参数:
fromgraphene_mongodbimportMongoSchemaclassUserSchema(MongoSchema):model=User# ORUserSchema=MongoSchema(User)
架构现在已经生成。现在需要创建一个graphene对象查询:
importgrapheneclassQuery(graphene.ObjectType):user=UserSchema.singleschema=graphene.Schema(query=Query)# now we can do the query:result=schema.execute("""query Data { user(username: "John") { id username }}""")
您可能会注意到上面的示例中的userschema.single属性,类userschema还有许多其他属性。所有这些都解释如下:
Attribute | Description |
---|---|
single | We use single we want that the query result be a unique result. That's the same that make the query in mongoengine calling .first() to get the first object that matches the query. |
list | List is used when we want a list of the documents that matches the query. |
model | That's easy, this attribute stores the original Document of mongoengine that you created. |
fields | This field is more consult, you can use the fields that was converted from mongoengine to graphene. For instance, in our UserSchema class the attribute field will be a dict like this: {'username': graphene.String} |
mutation | Mutate is the attribute that we use when creating Mutations with graphene. See more in Mutations |
突变
有时我们需要在MongoDB中保存新数据,而不是进行查询。基因突变可以做到这一点。 让我们再次使用在示例中创建的userschema。与之前一样,我们创建一个名为query的graphene对象来处理查询,现在我们需要对一个变异执行相同的操作:
classMutation(graphene.ObjectType):create_user=UserSchema.mutateschema=graphene.Schema(query=Query,mutation=Mutation)
注意,我们也更新了变量模式,使其具有一个变异对象。 现在我们可以执行变异查询并在数据库中创建新用户:
result=schema.execute("""mutation testMutation { createUser(username:"John") { person { id username } }}""")
我们在“params”中传递要保存的属性,就像在“…createPerson(username:”john“)…”
在本例中,graphennemongodb为您处理了对象的保存,但有时您需要在实际将对象保存到数据库之前进行验证。下一步将解释您如何做到这一点。
保存前验证
要使用自己的函数进行保存,需要在mongoschema类中创建一个名为mutate的函数。这么说,让我们更新我们的用户模式如下:
classUserSchema(MongoSchema):model=User@staticmethoddefmutate(args,context):new_user=User(**args)new_user.creation_date=datetime.now()new_user.save()returnnew_user
这里没有太多规则,您只需要确保方法接收两个参数,具有静态方法decorator并返回对象的实例。
context参数具有您正在使用的框架的请求对象。例如,如果您使用flask,那么该参数将是flask global request。
查询中的运算符
MongoEngine提供了多种运算符,可以用作“In”、“GTE”等。请参阅mongoengine documentation中的所有运算符。使用graphennemongodb,您可以在查询中使用它们:
result=schema.execute("""query Data { user(username_Icontains: "John", creationDate_Gte:"1997-04-28", favouriteColor_In:["red"]) { id username }}""")
最好的是它们都是由graphennemongodb支持的。
验证执行查询和突变的权限
要验证用户是否可以访问某个字段,可以创建名为validator的属性::
classUserSchema(MongoSchema):model=Userdefvalidator(model,fields,query,special_params):if'not_alowed_field'infields:raiseException('Unauthorized Access')
当存在验证器函数时,GrapheneMongodb将在每个查询或相应模式的突变中调用它。 只需确保引发一个异常,graphene将负责发送回前端的graphql客户端。
待办事项
- 接受用户变异返回无; 在不增加最大递归误差的情况下,支持“自我”的参考字段。