graphql-python / graphene-sqlalchemy

Graphene SQLAlchemy integration
http://docs.graphene-python.org/projects/sqlalchemy/en/latest/
MIT License
974 stars 225 forks source link

Enum cannot represent value #375

Closed n00el closed 1 year ago

n00el commented 1 year ago

I have the following sqlalchemy model:

class AccommodationModel(Base):
    __tablename__ = "accommodations"

    ....
    status = Column(Enum(AccommodationStatus), nullable=False, default=AccommodationStatus.creating)

And the following graphene model:

class AccommodationGraphQL(SQLAlchemyObjectType):
    class Meta:
        model = AccommodationModel

And I got the following error when selecting the status field from the query:

Enum 'AccommodationStatus' cannot represent value: <AccommodationStatus.active: 'creating'>

erikwrede commented 1 year ago

Thank you for the report 🙂 Can you please share something about the versions of graphene-sqlalchemy, sqlalchemy and graphene you're using and provide a reprodction including all involved ObjectTypes, Models, Enums and the Query? Without seeing the definition of AccommodationStatus it's hard to give any advice here.

n00el commented 1 year ago

Thank you for your fast reply! Sorry for the informations that I left out.

sqlalchemy: 1.4.46 graphene: 3.2.1 starlette-graphene3: 0.6.0 graphene-sqlalchemy: 3.0.0b3

Here is my enum:

class AccommodationStatus(str, Enum):
    creating = 'creating'
    active = 'active'
    inactive = 'inactive'

Here is my schema:

class CombinedQuery(graphene.ObjectType):
    accommodations = graphene.List(AccommodationGraphQL)
    def resolve_accommodations(self, info):
        query = AccommodationGraphQL.get_query(info)
        return query.all()

schema = graphene.Schema(query=CombinedQuery)

And here is my query:

query {
  accommodations {
    id
    status
  }
}

If I modify my status field to a string in the ObjectType it will work and , but that takes away the advantages provided by enums

class AccommodationGraphQL(SQLAlchemyObjectType):
    class Meta:
        model = AccommodationModel

    status = graphene.String()

OR if I write a custom resolver it will work again, but then I need to write resolvers to all my enums:

    def resolve_status(self, info):
        return self.status.value
erikwrede commented 1 year ago

Tried your code, but cannot reproduce the problem. Does it work for you with exactly this code? You can try putting it into a pytest testcase 🙂

class AccommodationStatus(str, PyEnum):
    creating = 'creating'
    active = 'active'
    inactive = 'inactive'

class AccommodationModel(Base):
    __tablename__ = "accommodations"
    id = Column(Integer, primary_key=True)
    status = Column(Enum(AccommodationStatus), nullable=False, default=AccommodationStatus.creating)

class AccommodationGraphQL(SQLAlchemyObjectType):
    class Meta:
        model = AccommodationModel

    status = graphene.String()

class CombinedQuery(graphene.ObjectType):
    accommodations = graphene.List(AccommodationGraphQL)

    def resolve_accommodations(self, info):
        return [AccommodationModel(id=1, status=AccommodationStatus.creating)]

schema = graphene.Schema(query=CombinedQuery)
query = """
query {
    accommodations {
    id
    status
    }
}
"""
result = schema.execute(query)
print(result)
print(json.dumps(result.formatted))

Enum 'AccommodationStatus' cannot represent value: <AccommodationStatus.active: 'creating'> Judging by this error message, is there any chance this error was caused by you accidentally initializing the enum with status string active instead of creating when setting the enum value to active?

github-actions[bot] commented 1 year ago

This issue was closed due to inactivity. If your request is still relevant, please open a new issue referencing this one and provide all of the requested information.

github-actions[bot] commented 9 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related topics referencing this issue.