如何使“ SQLAlchemyConnectionField”成为必需的?

时间:2019-07-09 14:04:14

标签: python graphene-python graphene-sqlalchemy

我正在尝试创建一个查询,该查询需要从Connection类型中对字段进行子选择。我尝试了SQLAlchemyConnectionField的几种不同组合,导致各种错误。

books_connection = SQLAlchemyConnectionField(graphene.NonNull(Book))
books_connection = SQLAlchemyConnectionField(Book, required=True)

两者都导致

TypeError: issubclass() arg 1 must be a class

books_connection = graphene.NonNull(SQLAlchemyConnectionField(Book))
books_connection = graphene.Field(SQLAlchemyConnectionField(Book), required=True)

都是

AttributeError: 'SQLAlchemyConnectionField' object has no attribute '_meta'

Book类型定义如下:

class Book(SQLAlchemyObjectType):
    class Meta:
        model = models.Book
        interfaces = (graphene.relay.Node, )
        only_fields = ("id", "title", "author", "last_read_at", "is_best_seller", "price", )

    genre = BookGenreEnum()

Query类型如下:

class Query(graphene.ObjectType):
    book = graphene.Field(Book, id=graphene.ID(), required=True)
    books = graphene.List(graphene.NonNull(Book), required=True)
    books_connection = SQLAlchemyConnectionField(Book) # Want this to be required!

    def resolve_book(parent, info, **kwargs):
        session = info.context.get("session")
        return session.query(models.Book).get(kwargs["id"])

    def resolve_books(parent, info):
        session = info.context.get("session")
        return session.query(models.Book).all()

0 个答案:

没有答案