简体   繁体   English

石墨烯的通用创建模型突变

[英]Generic Create Model Mutation for Graphene

I'm trying to create some kind of a generic create mutation in graphene for a flask application.我正在尝试为烧瓶应用程序在石墨烯中创建某种通用的创建突变。 In order to create a mutation, the syntax would normally be as follows:为了创建一个mutation,语法通常如下:

class CreateMutation(graphene.Mutation):
    class Arguments:
        model_attribute1
        model_attribute2
        ...

    def mutate(root, info, model_attribute1, model_attribute2):
        create model here

I would like to create some kind of generic create mutation class.我想创建某种通用的 create 变异类。 To do so, I would need to dynamically create the Arguments class and then pass those into mutate.为此,我需要动态创建 Arguments 类,然后将它们传递给 mutate。 I've figured out I can get attributes we need for the mutation from the sqlalchemy model with SqlAlchemyModel.__table__.columns , but I am having trouble figuring out how to create the Arguments class given these columns.我发现我可以使用SqlAlchemyModel.__table__.columnsSqlAlchemyModel.__table__.columns模型中获取突变所需的属性,但是我无法弄清楚如何在给定这些列的情况下创建 Arguments 类。

Try this:尝试这个:

def create_class(args: dict[str, str]):
    class Arguments: pass
    for arg in args:
        setattr(Arguments, arg, args[arg])
    return Arguments

x = create_class({'thing': '100'}); assert x.thing == '100';```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM