From 680242f1e333c3379689e0a334a5b83e3c894bf7 Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 19 Feb 2024 11:58:02 +0300 Subject: [PATCH] schema-main --- .pre-commit-config.yaml | 1 - {schema => __init__.py}/enum.graphql | 0 {schema => __init__.py}/input.graphql | 0 {schema => __init__.py}/mutation.graphql | 0 {schema => __init__.py}/query.graphql | 0 {schema => __init__.py}/type.graphql | 0 main.py | 6 +++- resolvers/reader.py | 44 +++++++++++++----------- services/schema.py | 3 -- 9 files changed, 29 insertions(+), 25 deletions(-) rename {schema => __init__.py}/enum.graphql (100%) rename {schema => __init__.py}/input.graphql (100%) rename {schema => __init__.py}/mutation.graphql (100%) rename {schema => __init__.py}/query.graphql (100%) rename {schema => __init__.py}/type.graphql (100%) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b930b6ec..ef7b41f8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/schema/enum.graphql b/__init__.py/enum.graphql similarity index 100% rename from schema/enum.graphql rename to __init__.py/enum.graphql diff --git a/schema/input.graphql b/__init__.py/input.graphql similarity index 100% rename from schema/input.graphql rename to __init__.py/input.graphql diff --git a/schema/mutation.graphql b/__init__.py/mutation.graphql similarity index 100% rename from schema/mutation.graphql rename to __init__.py/mutation.graphql diff --git a/schema/query.graphql b/__init__.py/query.graphql similarity index 100% rename from schema/query.graphql rename to __init__.py/query.graphql diff --git a/schema/type.graphql b/__init__.py/type.graphql similarity index 100% rename from schema/type.graphql rename to __init__.py/type.graphql diff --git a/main.py b/main.py index 727e3d63..fb68cd3f 100644 --- a/main.py +++ b/main.py @@ -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': diff --git a/resolvers/reader.py b/resolvers/reader.py index a189425d..c78f977c 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -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 diff --git a/services/schema.py b/services/schema.py index 52516f4d..32fb1525 100644 --- a/services/schema.py +++ b/services/schema.py @@ -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)