jondot/graphene

Handle trailing underscores in field name.

Morreski opened this issue · 0 comments

Hi !

Let's say I have a graphql query that takes a parameter which is named after a reserved python keyword (e.g: from). Following python conventions, this is how I would declare it:

    some_query  = graphene.Field(SomeSchema, from_=graphene.String(name="from"))

Here, name is used so that a user don't have to deal with the ugly underscore. Which is nice. However, there is a problem in the resolver:

    def resolve_some_query(_, args, context, infos):
        args["from_"]  # KeyError ! Because the arg name is 'from' which is not what I expect.

Now this is not a big deal, I could just use args["from"] when I need the value. But it becomes a problem when I want to pass all the parameters to an other function like this:

    def resolve_some_query(_, args, context, infos):
        call_some_handler(**args)

Because my function signature would looks like this and cause a syntax error:

    def call_some_handler(from=None):
        pass

I think trailing underscores should be removed when calling graphene.utils.str_converters.to_snake_case this would allow usage of python reserved keyword as parameters name while not breaking compatibility. If you guys agree with this I can make a PR during the weekend.