From e2f2dff755790d90c1dd45845edacc853f9e3cfd Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 30 Nov 2023 13:30:50 +0300 Subject: [PATCH] topics-sql-debug --- CHANGELOG.txt | 3 ++- resolvers/topic.py | 27 ++++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 9791ef65..9d3357d7 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,6 +1,7 @@ [0.2.17] - schema: enum types workaround, ReactionKind, InviteStatus, ShoutVisibility -- schema: Shout.created_by, Shout.updated_by fixes +- schema: Shout.created_by, Shout.updated_by +- schema: Shout.authors can be empty - resovlers: optimized reacted shouts updates query diff --git a/resolvers/topic.py b/resolvers/topic.py index 1b3edbf5..4ad59c7c 100644 --- a/resolvers/topic.py +++ b/resolvers/topic.py @@ -23,15 +23,20 @@ def add_topic_stat_columns(q): q = ( q.outerjoin(ShoutTopic, Topic.id == ShoutTopic.topic) - .add_columns(func.count(distinct(ShoutTopic.shout)).label("shouts_stat")) + .add_columns(func.count(distinct(ShoutTopic.shout)) + .label("shouts_stat")) .outerjoin(aliased_shout_author, ShoutTopic.shout == aliased_shout_author.shout) - .add_columns(func.count(distinct(aliased_shout_author.author)).label("authors_stat")) + .add_columns(func.count(distinct(aliased_shout_author.author)) + .label("authors_stat")) .outerjoin(aliased_topic_follower) - .add_columns(func.count(distinct(aliased_topic_follower.follower)).label("followers_stat")) + .add_columns(func.count(distinct(aliased_topic_follower.follower)) + .label("followers_stat")) ) q = q.group_by(Topic.id) + print(f"[resolvers.topics] Generated SQL: {str(q)}") # Add this line for debugging + return q @@ -49,14 +54,6 @@ def get_topics_from_query(q): return topics -def topics_followed_by(author_id): - q = select(Topic) - q = add_topic_stat_columns(q) - q = q.join(TopicFollower).where(TopicFollower.follower == author_id) - - return get_topics_from_query(q) - - @query.field("get_topics_all") async def get_topics_all(_, _info): q = select(Topic) @@ -65,6 +62,14 @@ async def get_topics_all(_, _info): return get_topics_from_query(q) +def topics_followed_by(author_id): + q = select(Topic) + q = add_topic_stat_columns(q) + q = q.join(TopicFollower).where(TopicFollower.follower == author_id) + + return get_topics_from_query(q) + + @query.field("get_topics_by_community") async def get_topics_by_community(_, _info, community_id: int): q = select(Topic).where(Topic.community == community_id)