From ec7b25df3c44fdb65004e3bf850077bf036a95c0 Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 7 Aug 2024 15:04:17 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=88=D1=8B=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resolvers/reader.py | 50 ++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/resolvers/reader.py b/resolvers/reader.py index 3f04dde4..4b7d69d7 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -39,17 +39,26 @@ def query_shouts(): select( Shout.id.label("shout_id"), func.json_agg( - distinct( - func.json_build_object( - 'id', Author.id, - 'name', Author.name, - 'slug', Author.slug, - 'pic', Author.pic - ) + func.json_build_object( + 'id', Author.id, + 'name', Author.name, + 'slug', Author.slug, + 'pic', Author.pic ) ).label("authors") ) - .join(ShoutAuthor, ShoutAuthor.author == Author.id) + .select_from( + select( + distinct(Author.id), + Author.name, + Author.slug, + Author.pic, + Shout.id.label("shout_id") + ) + .join(ShoutAuthor, ShoutAuthor.author == Author.id) + .join(Shout, Shout.id == ShoutAuthor.shout) + .subquery() + ) .group_by(Shout.id) .subquery() ) @@ -58,17 +67,26 @@ def query_shouts(): select( Shout.id.label("shout_id"), func.json_agg( - distinct( - func.json_build_object( - 'id', Topic.id, - 'title', Topic.title, - 'body', Topic.body, - 'slug', Topic.slug - ) + func.json_build_object( + 'id', Topic.id, + 'title', Topic.title, + 'body', Topic.body, + 'slug', Topic.slug ) ).label("topics") ) - .join(ShoutTopic, ShoutTopic.topic == Topic.id) + .select_from( + select( + distinct(Topic.id), + Topic.title, + Topic.body, + Topic.slug, + Shout.id.label("shout_id") + ) + .join(ShoutTopic, ShoutTopic.topic == Topic.id) + .join(Shout, Shout.id == ShoutTopic.shout) + .subquery() + ) .group_by(Shout.id) .subquery() )