【问题标题】:Add value to existing graphene.Enum为现有的石墨烯增加价值。枚举
【发布时间】:2020-12-22 08:26:39
【问题描述】:
有没有办法为现有的graphene.Enum 添加值?
我想使用 graphene-sqlalchemy sort_enum() 功能并添加我自己的附加功能 - TOTAL_COUNT_ASC 和 TOTAL_COUNT_DESC 这将启用按总数排序,但我可以弄清楚如何做到这一点。直接将字段添加到石墨烯。枚举不起作用:
SqlModel.sort_enum().TOTAL_COUNT_ASC = "TOTAL_COUNT_ASC"
谢谢,
【问题讨论】:
标签:
python
enums
graphene-python
graphene-sqlalchemy
【解决方案1】:
我发现的最好方法是:
from graphene_sqlalchemy import utils
# Take the original Enum
ExtendedValues = [(m.name, m.value) for m in SqlModel.sort_enum()._meta.enum]
# Add the new values with EnumValue
ExtendedValues += [("TOTAL_COUNT_ASC", utils.EnumValue('TOTAL_COUNT_ASC', sqlalchemy.func.count())), ('TOTAL_COUNT_DESC', utils.EnumValue('TOTAL_COUNT_DESC', sqlalchemy.func.count().desc()))]
# Create the extended graphene enum
ExtendedEnum = graphene.Enum("ExtendedEnum", ExtendedValues)