Search code examples
pythongraphqlgraphene-python

Accessing database ID in Graphene/Graphql


I have the following query which filters for a shop with the shown ID (using sqlalchemy):

{
  shop(id: "mydevappstore") {
    id
  }
}

The produces the following result. I would expect the id be the same as above:

{
  "data": {
    "shop": {
      "id": "U2hvcDpteWRldmFwcHN0b3Jl"
    }
  }
}

I don't understand why this happens (also in the Graphene tutorial) and how to fix it.

sqlalchemy_and_graphene.py is

Base = declarative_base()

class ShopModel(Base):
    __tablename__ = 'shop'
    id = Column(String(128), primary_key=True)

class Shop(SQLAlchemyObjectType):
    class Meta:
        model = ShopModel
        interfaces = (relay.Node, )

Solution

  • You need to explicitly specify the id field with the help of source argument

    import graphene
    from graphene_sqlalchemy import SQLAlchemyObjectType
    
    
    class Shop(SQLAlchemyObjectType):
        any_name = graphene.String(source='id')
    
        class Meta:
            model = ShopModel
            interfaces = (relay.Node,)