Graphene-SQLAlchemy中`sqlalchemy.types.Enum`的字段映射到GraphQL模式中的字符串

时间:2018-05-25 15:16:48

标签: python orm sqlalchemy graphql graphene-python

考虑以下枚举类:

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

设置为SQLAlchemy ORM字段:

class Book(Base, OrmBaseMixin):
    __tablename__ = "books"

    book_id = sqlalchemy.Column(
        sqlalchemy.types.Integer(),
        primary_key=True,
    )
    ...
    cover_title_color = sqlalchemy.Column(
        sqlalchemy.types.Enum(CoverTitleColor),
        nullable=False
    )

通过以下方式接触石墨烯:

class TypeBook(SQLAlchemyObjectType):
    class Meta:
        model = Book

但是,当通过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"
      }
    ]
  }
}

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

  

可以在https://github.com/somada141/demo-graphql-sqlalchemy-falcon/tree/issue-graphene-sqlalchemy-135

下找到完整的工作代码      

此问题也已在https://github.com/graphql-python/graphene-sqlalchemy/issues/135

下发布为GH问题

0 个答案:

没有答案