简体   繁体   English

为现有的石墨烯增加价值。枚举

[英]Add value to existing graphene.Enum

Is there a way to add a value to an existing graphene.Enum ?有没有办法为现有的graphene.Enum添加一个值?

I want to use graphene-sqlalchemy sort_enum() functionality and to add additional functionality of my own - TOTAL_COUNT_ASC and TOTAL_COUNT_DESC that will enable sorting by total count, but I can figure out how to do it.我想使用 graphene- sqlalchemy sort_enum()功能并添加我自己的附加功能 - TOTAL_COUNT_ASCTOTAL_COUNT_DESC将启用按总数排序,但我可以弄清楚如何做到这一点。 Directly adding the fields to the graphene.Enum doesn't work:直接将字段添加到 graphene.Enum 不起作用:

SqlModel.sort_enum().TOTAL_COUNT_ASC = "TOTAL_COUNT_ASC"

Thanks,谢谢,

The best way that I found is:我发现的最好方法是:


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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM