Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle trailing underscores in field name. #490

Closed
Morreski opened this issue Jun 19, 2017 · 2 comments
Closed

Handle trailing underscores in field name. #490

Morreski opened this issue Jun 19, 2017 · 2 comments

Comments

@Morreski
Copy link

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.

@syrusakbary
Copy link
Member

syrusakbary commented Jun 24, 2017

Taking a look now, thanks for such a great report!

@syrusakbary
Copy link
Member

I decided to fix the argument output name to be _from instead of from, as it simplifies and makes more clear how to retrieve the arguments from the python resolver.

This way, the previous function you expected to work, should work:

    def resolve_some_query(_, args, context, infos):
        args["from_"]  # Not longer KeyError :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants