Graphene: Support to map input args to object model

Created on 25 May 2017  路  2Comments  路  Source: graphql-python/graphene

I find that this style of code is very verbose to map input args fields to the model's fields.

class CreatePerson(graphene.Mutation):
    class Input:
        name = graphene.String()

    ok = graphene.Boolean()
    person = graphene.Field(lambda: Person)

    @staticmethod
    def mutate(root, args, context, info):
        person = Person(name=args.get('name'))
        ok = True
        return CreatePerson(person=person, ok=ok)

It's even worse when Person has more fields.

For example:

  • If Person is a form of Person(name, age, address, mobile...)
    => I have to write: Person(name=args.get('name'), age=args.get('age'), ...)

I wonder if there are any ways to solve it?

Thanks

Most helpful comment

You can do something like Person(**args)

All 2 comments

You can do something like Person(**args)

Yikes, I know it recently! Thanks!

Was this page helpful?
0 / 5 - 0 ratings