我正在尝试创建一个查询,该查询需要从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()