田野`sqlalchemy.types.Enum`在GrapheneSQLAlchemy中映射到GraphQL模式中的字符串

2024-03-29 10:21:02 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑以下枚举类:

import enum
class CoverTitleColor(enum.Enum):
    YELLOW = "yellow"
    RED = "red"
    BLUE = "blue"
    ORANGE = "orange"
    WHITE = "white"
    PURPLE = "purple"

设置为SQLAlchemy ORM字段,如下所示:

^{pr2}$

通过以下方式暴露于石墨烯中:

^{3}$

但是,当通过GraphQL通过以下查询自省TypeBook时:

{
  __type(name: "TypeBook") {
    name
    fields {
      name
      type {
        name
        kind
        ofType {
          name
          kind
        }
      }
    }
  }
}

coverTitleColor字段被解释为字符串:

{
  "name": "coverTitleColor",
  "type": {
    "name": null,
    "kind": "NON_NULL",
    "ofType": {
      "name": "String",
      "kind": "SCALAR"
    }
  }
},

当查询出该字段时,例如,通过:

query {
  books(year: 1990) {
    coverTitleColor
  } 
}

结果确实是这样的字符串:

{
  "data": {
    "books": [
      {
        "coverTitleColor": "CoverTitleColor.YELLOW"
      },
      {
        "coverTitleColor": "CoverTitleColor.RED"
      }
    ]
  }
}

我错过什么了吗?这是预期的行为吗?在

The full working code can be found under https://github.com/somada141/demo-graphql-sqlalchemy-falcon/tree/issue-graphene-sqlalchemy-135

This question has also been posted as a GH issue under https://github.com/graphql-python/graphene-sqlalchemy/issues/135


Tags: 字符串namehttpssqlalchemytypeenumredbooks