From d20cd1e521ad75164e0b19a9c4f4e51ffdd1aa93 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Tue, 9 Aug 2022 05:29:30 +0300 Subject: [PATCH] lesstrict --- orm/shout.py | 2 +- resolvers/zine.py | 14 ++++++++------ schema.graphql | 4 ++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/orm/shout.py b/orm/shout.py index 22797952..5cb595b8 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -25,7 +25,7 @@ class ShoutAuthor(Base): id = None shout = Column(ForeignKey('shout.slug'), primary_key = True) user = Column(ForeignKey('user.slug'), primary_key = True) - caption: str = Column(String, nullable=False, default = "") + caption: str = Column(String, nullable = True, default = "") class ShoutAllowed(Base): __tablename__ = "shout_allowed" diff --git a/resolvers/zine.py b/resolvers/zine.py index 61f7159e..ec6d9bbe 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -10,7 +10,7 @@ from resolvers.community import community_follow, community_unfollow from resolvers.reactions import reactions_follow, reactions_unfollow from auth.authenticate import login_required from sqlalchemy import select, desc, and_ -from sqlalchemy.orm import selectinload +from sqlalchemy.orm import selectinload, joinedload @query.field("topViewed") @@ -50,14 +50,16 @@ async def view_shout(_, info, slug): @query.field("getShoutBySlug") async def get_shout_by_slug(_, info, slug): - all_fields = [node.name.value for node in info.field_nodes[0].selection_set.selections] - selected_fields = set(["authors", "topics", "reactions", "captions"]).intersection(all_fields) - select_options = [selectinload(getattr(Shout, field)) for field in selected_fields] - # select_options.append(selectinload(ShoutTopic.caption).label("captions")) + shout = None # TODO: append captions anyhow with local_session() as session: shout = session.query(Shout).\ - options(select_options).\ + options([ + selectinload(Shout.topics), + selectinload(Shout.reactions), + selectinload(Shout.authors) + ]).\ + join([ShoutAuthor.user, ShoutAuthor.caption], ShoutAuthor.shout == slug ).\ filter(Shout.slug == slug).first() if not shout: diff --git a/schema.graphql b/schema.graphql index 54905c0f..94eacd34 100644 --- a/schema.graphql +++ b/schema.graphql @@ -322,7 +322,7 @@ type Reaction { type Author { slug: String! - name: String! + name: String userpic: String caption: String # only for full shout } @@ -333,7 +333,7 @@ type Shout { slug: String! body: String! createdAt: DateTime! - authors: [Author!]! + authors: [Author] # ratings: [Rating] community: String cover: String