fness
fness

Reputation: 91

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.

Upvotes: 3

Views: 297

Answers (1)

Paul3349
Paul3349

Reputation: 481

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

Upvotes: 1

Related Questions