为现有 graphene.Enum 增值
Add value to existing graphene.Enum
有没有办法为现有的 graphene.Enum
添加值?
我想使用 graphene-sqlalchemy sort_enum() 功能并添加我自己的附加功能 - TOTAL_COUNT_ASC
和 TOTAL_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)
有没有办法为现有的 graphene.Enum
添加值?
我想使用 graphene-sqlalchemy sort_enum() 功能并添加我自己的附加功能 - TOTAL_COUNT_ASC
和 TOTAL_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)