roshnet
roshnet

Reputation: 2073

Cannot resolve a field name with underscores in graphene

I went through the documentation of Python graphene, and it worked. Here is the code -

from graphene import ObjectType, String, Schema


class Query(ObjectType):
    hello = String(name=String(default_value="stranger"))

    def resolve_hello(root, info, name):
        return f'Hello {name}!'


schema = Schema(query=Query)

query = '{ hello(name: "GraphQL") }'

result = schema.execute(query)

print(result.data['hello'])    # "Hello GraphQL!"

However, on changing hello to some_field, and resolve_hello to resolve_some_field, and making query = '{ some_field(name: "GraphQL" }', I got the end result as None.

Is there a way to work with fields that include underscores in them?

Upvotes: 0

Views: 1247

Answers (1)

roshnet
roshnet

Reputation: 2073

I was unaware of how graphene behind the scenes converts snake case to camel case.

Everything was perfect in the schema definition, but the way I called it as a client needed to be changed.

Hence, instead of:

query = '{ some_field(name: "GraphQL" }'

... I needed to do:

query = '{ someField(name: "GraphQL" }'

... because that's how a client side JavaScript would prefer to call it.

Upvotes: 2

Related Questions