这是使用Python和Graphene库
我想通过GraphQL向前端提供一个常量列表。我使用了inspection,但它只输出枚举的键。不是价值观。我了解到石墨烯枚举只包含名称/描述
{
__type(name: "FruitEnum") {
enumValues {
name
description
}
}
}
这是回报
{
"data": {
"__type": {
"enumValues": [
{
"name": "APPLE",
"description": null
},
{
"name": "BANANA",
"description": null
},
{
"name": "ORANGE",
"description": null
}
]
}
},
"errors": null
}
这就是实际枚举的样子
class FruitEnum(Enum):
APPLE = "Apple -- but could also be other information for the front end"
BANANA = "Banana"
ORANGE = "Orange"
有没有更好的方法通过GraphQL公开这样的常量列表?可以用解析器修改内省以读取值吗?我正在使用enum.from_enum函数获取一个常规Python枚举并向Graphene注册它
我认为您不会从
enumValues
添加或删除字段,因为它是标准的但是,您可以通过在enum类中指定
description
property
来添加描述因此,您将得到如下响应:
使用Django,您可以简单地编写:
因此,您将得到如下响应:
相关问题 更多 >
编程相关推荐