Search code examples
graphqlgraphene-python

How should I resolve a field with leading underscore in graphene?


I have a field _id

class Article(graphene.ObjectType):
    _id = graphene.Int()
    article_id = graphene.Int()

    def resolve__id(self, info):
        return self.article_id

This one does not work, it will interpret _id as Id.


Solution

  • Graphene tries to convert all fields to camel case to maintain convention with JavaScript: http://docs.graphene-python.org/en/latest/types/schema/#auto-camelcase-field-names

    This can be turned off at the schema level or you can explicitly override the name of a field with whatever you want:

    class Article(graphene.ObjectType):
        id = graphene.Int(name='_id')