简体   繁体   中英

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:

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. To do so, I would need to dynamically create the Arguments class and then pass those into 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.

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';```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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