From bce71a6be8ff8fd4f3247be9b3c0e2c56cd80827 Mon Sep 17 00:00:00 2001 From: knst-kotov Date: Sat, 8 Jan 2022 12:04:45 +0300 Subject: [PATCH] shoutsByTopic, shoutsByAuthor and shoutsByCommunity pagination --- resolvers/zine.py | 15 +++++++++------ schema.graphql | 6 +++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/resolvers/zine.py b/resolvers/zine.py index 58eefa91..18e4a3d9 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -352,17 +352,18 @@ async def get_shout_comments(_, info, slug): return comments @query.field("shoutsByTopic") -async def shouts_by_topic(_, info, topic, limit): +async def shouts_by_topic(_, info, topic, page, size): with local_session() as session: shouts = session.query(Shout).\ join(ShoutTopic).\ where(and_(ShoutTopic.topic == topic, Shout.publishedAt != None)).\ order_by(desc(Shout.publishedAt)).\ - limit(limit) + limit(size).\ + offset(page * size) return shouts @query.field("shoutsByAuthor") -async def shouts_by_author(_, info, author, limit): +async def shouts_by_author(_, info, author, page, size): with local_session() as session: user = session.query(User).\ filter(User.slug == author).first() @@ -374,11 +375,12 @@ async def shouts_by_author(_, info, author, limit): join(ShoutAuthor).\ where(and_(ShoutAuthor.user == user.id, Shout.publishedAt != None)).\ order_by(desc(Shout.publishedAt)).\ - limit(limit) + limit(size).\ + offset(page * size) return shouts @query.field("shoutsByCommunity") -async def shouts_by_community(_, info, community, limit): +async def shouts_by_community(_, info, community, page, size): with local_session() as session: #TODO fix postgres high load @@ -389,5 +391,6 @@ async def shouts_by_community(_, info, community, limit): select(Topic.slug).where(Topic.community == community)\ ))).\ order_by(desc(Shout.publishedAt)).\ - limit(limit) + limit(size).\ + offset(page * size) return shouts diff --git a/schema.graphql b/schema.graphql index b5da2c63..67f66278 100644 --- a/schema.graphql +++ b/schema.graphql @@ -154,9 +154,9 @@ type Query { # shouts getShoutBySlug(slug: String!): Shout! - shoutsByTopic(topic: String!, limit: Int!): [Shout]! - shoutsByAuthor(author: String!, limit: Int!): [Shout]! - shoutsByCommunity(community: String!, limit: Int!): [Shout]! + shoutsByTopic(topic: String!, page: Int!, size: Int!): [Shout]! + shoutsByAuthor(author: String!, page: Int!, size: Int!): [Shout]! + shoutsByCommunity(community: String!, page: Int!, size: Int!): [Shout]! getShoutComments(slug: String!): [Comment]! # mainpage