【问题标题】:Returning a list of enums with string values with Graphene使用 Graphene 返回具有字符串值的枚举列表
【发布时间】:2021-01-14 11:43:12
【问题描述】:

这是使用 Python 和 Graphene 库。

我想通过 GraphQL 向我的前端提供一个常量列表。我沿着使用检查的路线走,但它只会输出枚举的键。不是价值观。我了解到石墨烯枚举只包含名称/描述。

    {
      __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 公开像这样的常量列表的首选方式?可以使用解析器修改内省以读取值吗?我正在使用常规 Python 枚举并使用 Enum.from_enum 函数将其注册到 Graphene。

【问题讨论】:

    标签: python enums graphql graphene-python


    【解决方案1】:

    我认为您不会在 enumValues 中添加或删除字段,因为它是标准。

    但是,您可以通过在枚举类中指定 description property 来添加描述。

    import graphene
    
    from enum import Enum as PyEnum
    
    
    class FruitEnum(PyEnum):
        APPLE = "Apple"
        BANANA = "Banana"
        ORANGE = "Orange"
    
        def get_field_description(self):
            mapper = {
                "default": "default description",
                "APPLE": "Apple description"
            }
            return mapper.get(self.name, mapper['default'])
    
        @property
        def description(self):
            return self.get_field_description()
    
    
    class FruitType(graphene.ObjectType):
        foo = graphene.Enum.from_enum(FruitEnum)()

    因此你会得到一个响应,

    {
      "data": {
        "__type": {
          "name": "FruitEnum",
          "enumValues": [
            {
              "name": "APPLE",
              "description": "Apple description"
            },
            {
              "name": "BANANA",
              "description": "default description"
            },
            {
              "name": "ORANGE",
              "description": "default description"
            }
          ]
        }
      }
    }

    【讨论】:

      【解决方案2】:

      使用 Django 你可以简单地编写:

      from django.db import models
      
      from django.utils.translation import gettext_lazy as _   # Provides a convenient way to translate your descriptions
      
      class FruitEnum(models.TextChoices):
          APPLE = "Apple", _('Apple description goes here')
          BANANA = "Banana", _('Banana description goes here')
          ORANGE = "Orange", _('Orange description goes here')
      

      因此你会得到一个响应,

      {
        "data": {
          "__type": {
            "name": "FruitEnum",
            "enumValues": [
              {
                "name": "APPLE",
                "description": "Apple description goes here"
              },
              {
                "name": "BANANA",
                "description": "Banana description goes here"
              },
              {
                "name": "ORANGE",
                "description": "Orange description goes here"
              }
            ]
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-24
        • 1970-01-01
        • 1970-01-01
        • 2017-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多