schema-main

This commit is contained in:
Untone 2024-02-19 11:58:02 +03:00
parent 0301d8041d
commit 680242f1e3
9 changed files with 29 additions and 25 deletions

View File

@ -8,7 +8,6 @@ repos:
- id: trailing-whitespace
- id: check-added-large-files
- id: detect-private-key
- id: double-quote-string-fixer
- id: check-ast
- id: check-merge-conflict

View File

@ -2,19 +2,23 @@ import os
from importlib import import_module
from os.path import exists
from ariadne import load_schema_from_path, make_executable_schema
from ariadne.asgi import GraphQL
from starlette.applications import Starlette
from starlette.routing import Route
from services.rediscache import redis
from services.schema import schema
from services.search import search_service
from services.sentry import start_sentry
from services.viewed import ViewedStorage
from services.webhook import WebhookEndpoint
from settings import DEV_SERVER_PID_FILE_NAME, MODE
from services.schema import resolvers
import_module('resolvers')
schema = make_executable_schema(load_schema_from_path('schema/'), resolvers)
async def start():
if MODE == 'development':

View File

@ -446,36 +446,40 @@ async def load_shouts_random_top(_, _info, options):
limit = options.get('limit', 10)
q = q.group_by(Shout.id).order_by(func.random()).limit(limit)
# print(q.compile(compile_kwargs={"literal_binds": True}))
return await get_shouts_from_query(q)
@query.field('load_shouts_random_topic')
async def load_shouts_random_topic(_, info, limit: int = 10):
topic = get_random_topic()
shouts = []
if topic:
q = (
select(Shout)
.options(
joinedload(Shout.authors),
joinedload(Shout.topics),
)
.filter(
and_(
Shout.deleted_at.is_(None),
Shout.featured_at.is_not(None),
Shout.topics.any(slug=topic.slug),
)
shouts = fetch_shouts_by_topic(topic, limit)
if shouts:
return {'topic': topic, 'shouts': shouts}
return { 'error': 'failed to get random topic after few retries', shouts: [], topic: {} }
def fetch_shouts_by_topic(topic, limit):
q = (
select(Shout)
.options(
joinedload(Shout.authors),
joinedload(Shout.topics),
)
.filter(
and_(
Shout.deleted_at.is_(None),
Shout.featured_at.is_not(None),
Shout.topics.any(slug=topic.slug),
)
)
)
aliased_reaction = aliased(Reaction)
q = add_stat_columns(q, aliased_reaction)
aliased_reaction = aliased(Reaction)
q = add_stat_columns(q, aliased_reaction)
q = q.group_by(Shout.id).order_by(desc(Shout.created_at)).limit(limit)
q = q.group_by(Shout.id).order_by(desc(Shout.created_at)).limit(limit)
shouts = get_shouts_from_query(q)
shouts = get_shouts_from_query(q)
return {'topic': topic, 'shouts': shouts}
return shouts

View File

@ -1,8 +1,5 @@
from ariadne import MutationType, QueryType
from ariadne import load_schema_from_path, make_executable_schema
query = QueryType()
mutation = MutationType()
resolvers = [query, mutation]
schema = make_executable_schema(load_schema_from_path('schema/'), resolvers)