Помогите написать запрос с помощью SQLAlchemy в связке с Graphene

Нужно достать авторов из базы и посчитать количество книг каждого. Не могу понять синтаксис построения запроса с помощью sqlalchemy, когда делаю GraphQLAPI в связке FastAPI + graphene + sqlalchemy

class Books(SQLAlchemyObjectType):
    class Meta:
        model = BookModel


class Authors(SQLAlchemyObjectType):
    class Meta:
        model = AuthorModel


class LibraryQuery(graphene.ObjectType):
    books = graphene.List(Books)
    authors = graphene.List(Authors)

    def resolve_authors(self, info, **kwargs):
        authors_query = Authors.get_query(info)

        return authors_query

    def resolve_search_authors(self, info, **kwargs):
        name = kwargs.get("name")
        authors_query = Authors.get_query(info)
        authors = authors_query.filter((AuthorModel.name.contains(name))).all()

        return authors

Как я понимаю SQL запрос должен быть такой:

   SELECT
    a.name,
    COUNT(*) as number_of_books
    FROM
        author a
        JOIN book_author ba ON a.id = ba.author_id
    GROUP BY
        a.name
    ORDER BY number_of_books DESC

Также models.py:

association_table = Table('book_author', Base.metadata,
                          Column('book_id', Integer, ForeignKey('book.id')),
                          Column('author_id', Integer, ForeignKey('author.id')))


class Book(Base):
    __tablename__ = "book"
    id = Column(Integer, primary_key=True)
    title = Column(String)
    published = Column(Date)
    author = relationship("Author", secondary=association_table, back_populates="book")


class Author(Base):
    __tablename__ = "author"
    id = Column(Integer, primary_key=True)
    name = Column(String)
    book = relationship("Book", secondary=association_table, back_populates="author")

Ответы (0 шт):