为现有的 graphene.Enum 增加价值

问题描述

有没有办法给现有的 graphene.Enum 添加一个值?

我想使用 graphene-sqlalchemy sort_enum() 功能添加我自己的附加功能 - TOTAL_COUNT_ASCTOTAL_COUNT_DESC,这将启用按总数排序,但我可以弄清楚如何去做吧。直接将字段添加到 graphene.Enum 不起作用:

sqlModel.sort_enum().TOTAL_COUNT_ASC = "TOTAL_COUNT_ASC"

谢谢,

解决方法

我发现的最好方法是:


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)