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: trailing-whitespace
- id: check-added-large-files - id: check-added-large-files
- id: detect-private-key - id: detect-private-key
- id: double-quote-string-fixer
- id: check-ast - id: check-ast
- id: check-merge-conflict - id: check-merge-conflict

View File

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

View File

@ -446,36 +446,40 @@ async def load_shouts_random_top(_, _info, options):
limit = options.get('limit', 10) limit = options.get('limit', 10)
q = q.group_by(Shout.id).order_by(func.random()).limit(limit) 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) return await get_shouts_from_query(q)
@query.field('load_shouts_random_topic') @query.field('load_shouts_random_topic')
async def load_shouts_random_topic(_, info, limit: int = 10): async def load_shouts_random_topic(_, info, limit: int = 10):
topic = get_random_topic() topic = get_random_topic()
shouts = []
if topic: if topic:
q = ( shouts = fetch_shouts_by_topic(topic, limit)
select(Shout) if shouts:
.options( return {'topic': topic, 'shouts': shouts}
joinedload(Shout.authors), return { 'error': 'failed to get random topic after few retries', shouts: [], topic: {} }
joinedload(Shout.topics),
)
.filter( def fetch_shouts_by_topic(topic, limit):
and_( q = (
Shout.deleted_at.is_(None), select(Shout)
Shout.featured_at.is_not(None), .options(
Shout.topics.any(slug=topic.slug), 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) aliased_reaction = aliased(Reaction)
q = add_stat_columns(q, 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 MutationType, QueryType
from ariadne import load_schema_from_path, make_executable_schema
query = QueryType() query = QueryType()
mutation = MutationType() mutation = MutationType()
resolvers = [query, mutation] resolvers = [query, mutation]
schema = make_executable_schema(load_schema_from_path('schema/'), resolvers)