From e7a1697f11a83f52cdc3d2b2f191689f377fe216 Mon Sep 17 00:00:00 2001 From: Untone Date: Sat, 3 Feb 2024 20:08:22 +0300 Subject: [PATCH] get-my-followings-fix --- main.py | 1 - resolvers/follower.py | 28 +++++++++++++--------------- resolvers/topic.py | 2 +- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/main.py b/main.py index 098e34da..f0e2503b 100644 --- a/main.py +++ b/main.py @@ -35,7 +35,6 @@ async def start_up(): # start viewed service await ViewedStorage.init() - # start search service search_service.info() diff --git a/resolvers/follower.py b/resolvers/follower.py index 2ed2d851..9cdc1dca 100644 --- a/resolvers/follower.py +++ b/resolvers/follower.py @@ -1,10 +1,11 @@ import logging from typing import List -from sqlalchemy.orm import selectinload +from sqlalchemy import aliased from sqlalchemy.sql import and_ from orm.author import Author, AuthorFollower +from orm.community import Community from orm.reaction import Reaction from orm.shout import Shout, ShoutReactionsFollower from orm.topic import Topic, TopicFollower @@ -79,34 +80,31 @@ async def unfollow(_, info, what, slug): @login_required async def get_my_followed(_, info): user_id = info.context['user_id'] + topics = set() + authors = set() communities = [] - with local_session() as session: author = session.query(Author).filter(Author.user == user_id).first() - if isinstance(author, Author): author_id = author.id - - # Using joinedload to eagerly load AuthorFollower and Author data + aliased_author = aliased(Author) authors_query = ( - session.query(Author) + session.query(aliased_author, AuthorFollower) .join(AuthorFollower, AuthorFollower.follower == author_id) - .options(selectinload(Author.followers)) - .all() + .filter(AuthorFollower.author == aliased_author.id) ) - # Using joinedload to eagerly load TopicFollower and Topic data topics_query = ( - session.query(Topic) + session.query(Topic, TopicFollower) .join(TopicFollower, TopicFollower.follower == author_id) - .options(selectinload(Topic.followers)) - .all() + .filter(TopicFollower.topic == Topic.id) ) - # No need for a separate query for communities as it's fetched directly - communities = author.communities + authors = set(session.execute(authors_query).scalars()) + topics = set(session.execute(topics_query).scalars()) + communities = session.query(Community).all() - return {'topics': topics_query, 'authors': authors_query, 'communities': communities} + return {'topics': list(topics), 'authors': list(authors), 'communities': communities} @query.field('get_shout_followers') diff --git a/resolvers/topic.py b/resolvers/topic.py index 026b21ec..b1baccc7 100644 --- a/resolvers/topic.py +++ b/resolvers/topic.py @@ -132,7 +132,7 @@ async def update_topic(_, _info, inp): session.commit() return {'topic': topic} - return {'error': 'cannot update' } + return {'error': 'cannot update'} @mutation.field('delete_topic')