import importlib
import os

from api_graphql.abc.db_history_model_query_abc import DbHistoryModelQueryABC
from api_graphql.abc.db_model_query_abc import DbModelQueryABC
from api_graphql.abc.mutation_abc import MutationABC
from api_graphql.abc.query_abc import QueryABC
from api_graphql.abc.subscription_abc import SubscriptionABC
from api_graphql.query import Query


# used to import all queries and mutations
def import_graphql_schema_part(part: str):
    routes_dir = os.path.join(os.path.dirname(__file__), part)
    for filename in os.listdir(routes_dir):
        if filename.endswith(".py") and filename != "__init__.py":
            module_name = f"api_graphql.{part}.{filename[:-3]}"
            importlib.import_module(module_name)


import_graphql_schema_part("queries")
import_graphql_schema_part("mutations")

sub_query_classes = [
    DbModelQueryABC,
    DbHistoryModelQueryABC,
    MutationABC,
    SubscriptionABC,
]
query_classes = [
    *[y for x in sub_query_classes for y in x.__subclasses__()],
    *[x for x in QueryABC.__subclasses__() if x not in sub_query_classes],
]

QUERIES = [x() for x in query_classes if x != Query]
