diff --git a/resolvers/zine.py b/resolvers/zine.py index 3794a9ae..176bbb0a 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -1,6 +1,7 @@ from orm import Shout, ShoutAuthor, ShoutTopic, ShoutRating, ShoutViewByDay, User, Community, Resource,\ - ShoutRatingStorage, ShoutViewStorage + ShoutRatingStorage, ShoutViewStorage, Comment, CommentRating from orm.base import local_session +from orm.user import UserStorage from resolvers.base import mutation, query @@ -363,3 +364,18 @@ async def get_shout_by_slug(_, info, slug): shout.rating = await ShoutRatingStorage.get_rating(shout.id) shout.views = await ShoutViewStorage.get_view(shout.id) return shout + +@query.field("getShoutComments") +async def get_shout_comments(_, info, shout_id): + with local_session() as session: + rows = session.query(Comment, func.sum(CommentRating.value).label("rating")).\ + join(CommentRating).\ + where(Comment.shout == shout_id).\ + group_by(Comment.id).all() + comments = [] + for row in rows: + comment = row.Comment + comment.author = await UserStorage.get_user(comment.author) + comment.rating = row.rating + comments.append(comment) + return comments diff --git a/schema.graphql b/schema.graphql index 8b3885bf..5a78640d 100644 --- a/schema.graphql +++ b/schema.graphql @@ -136,10 +136,11 @@ type Query { getMessages(count: Int = 100, page: Int = 1): [Message!]! # shouts - getShoutBySlug(slug: String!): Shout! # NOTE: with .comments: Comments[] + getShoutBySlug(slug: String!): Shout! shoutsByTopic(topic: String!, limit: Int!): [Shout]! shoutsByAuthor(author: String!, limit: Int!): [Shout]! shoutsByCommunity(community: String!, limit: Int!): [Shout]! + getShoutComments(shout_id: Int!): [Comment]! # mainpage topViewed(limit: Int): [Shout]!