0

有没有办法为现有的添加价值graphene.Enum

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

SqlModel.sort_enum().TOTAL_COUNT_ASC = "TOTAL_COUNT_ASC"

谢谢,

4

1 回答 1

0

我发现的最好方法是:


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)
于 2020-12-23T08:57:37.417 回答