diff --git a/kdb-bot/src/bot_api/api.py b/kdb-bot/src/bot_api/api.py index b44b92b3..26b4bae6 100644 --- a/kdb-bot/src/bot_api/api.py +++ b/kdb-bot/src/bot_api/api.py @@ -23,7 +23,7 @@ from bot_api.exception.service_exception import ServiceException from bot_api.logging.api_logger import ApiLogger from bot_api.model.error_dto import ErrorDTO from bot_api.route.route import Route -from bot_data.graphql.schema import schema +from bot_data.graphql.graphql import GraphQL class Api(Flask): @@ -64,11 +64,12 @@ class Api(Flask): self._requests = {} + gql = GraphQL() self.add_url_rule( '/api/graphql', view_func=GraphQLView.as_view( 'graphql', - schema=schema, + schema=gql.schema, graphiql=True # for having the GraphiQL interface ) ) diff --git a/kdb-bot/src/bot_api/api_module.py b/kdb-bot/src/bot_api/api_module.py index f95aa58c..b4c1735a 100644 --- a/kdb-bot/src/bot_api/api_module.py +++ b/kdb-bot/src/bot_api/api_module.py @@ -47,7 +47,6 @@ class ApiModule(ModuleABC): services.add_transient(GuiController) services.add_transient(DiscordService) services.add_transient(ServerController) - #services.add_transient(GraphQLController) # cpl-discord self._dc.add_event(DiscordEventTypesEnum.on_ready.value, BotApiOnReadyEvent) diff --git a/kdb-bot/src/bot_api/controller/graphql_controller.py b/kdb-bot/src/bot_api/controller/graphql_controller.py deleted file mode 100644 index a1cc5425..00000000 --- a/kdb-bot/src/bot_api/controller/graphql_controller.py +++ /dev/null @@ -1,32 +0,0 @@ -import os - -from cpl_core.configuration import ConfigurationABC -from cpl_core.environment import ApplicationEnvironmentABC -from cpl_core.mailing import EMail, EMailClientABC, EMailClientSettings -from cpl_translation import TranslatePipe -from flask import jsonify, request - -from bot_api.api import Api -from bot_api.configuration.authentication_settings import AuthenticationSettings -from bot_api.logging.api_logger import ApiLogger -from bot_api.model.settings_dto import SettingsDTO -from bot_api.model.version_dto import VersionDTO -from bot_api.route.route import Route - - -class GraphQLController: - BasePath = f'/api/graphql' - - def __init__( - self, - config: ConfigurationABC, - env: ApplicationEnvironmentABC, - logger: ApiLogger, - ): - self._config = config - self._env = env - self._logger = logger - - @Route.get(f'{BasePath}') - async def graphql(self): - return '', 200 diff --git a/kdb-bot/src/bot_data/abc/queryABC.py b/kdb-bot/src/bot_data/abc/queryABC.py new file mode 100644 index 00000000..af49f42b --- /dev/null +++ b/kdb-bot/src/bot_data/abc/queryABC.py @@ -0,0 +1,53 @@ +from cpl_core.dependency_injection import ServiceProviderABC +from cpl_query.extension import List +from graphene import ObjectType + +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.abc.client_repository_abc import ClientRepositoryABC +from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC +from bot_data.abc.level_repository_abc import LevelRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC +from bot_data.abc.user_joined_voice_channel_abc import UserJoinedVoiceChannelRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.model.auto_role import AutoRole +from bot_data.model.client import Client +from bot_data.model.known_user import KnownUser +from bot_data.model.level import Level +from bot_data.model.server import Server +from bot_data.model.user import User +from bot_data.model.user_joined_server import UserJoinedServer +from bot_data.model.user_joined_voice_channel import UserJoinedVoiceChannel + + +class QueryABC(ObjectType): + _auto_roles: List[AutoRole] + _clients: List[Client] + _known_users: List[KnownUser] + _levels: List[Level] + _servers: List[Server] + _user_joined_servers: List[UserJoinedServer] + _user_joined_voice_channel: List[UserJoinedVoiceChannel] + _users: List[User] + + @classmethod + @ServiceProviderABC.inject + def init( + cls, + auto_roles: AutoRoleRepositoryABC, + clients: ClientRepositoryABC, + known_users: KnownUserRepositoryABC, + levels: LevelRepositoryABC, + servers: ServerRepositoryABC, + user_joined_servers: UserJoinedServerRepositoryABC, + user_joined_voice_channel: UserJoinedVoiceChannelRepositoryABC, + users: UserRepositoryABC, + ): + cls._auto_roles = auto_roles.get_auto_roles() + cls._clients = clients.get_clients() + cls._known_users = known_users.get_users() + cls._levels = levels.get_levels() + cls._servers = servers.get_servers() + cls._user_joined_servers = user_joined_servers.get_user_joined_servers() + cls._user_joined_voice_channel = user_joined_voice_channel.get_user_joined_voice_channels() + cls._users = users.get_users() diff --git a/kdb-bot/src/bot_data/graphql/graphql.py b/kdb-bot/src/bot_data/graphql/graphql.py new file mode 100644 index 00000000..23df08dc --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/graphql.py @@ -0,0 +1,16 @@ +import graphene +from graphene import Schema + +from bot_data.abc.queryABC import QueryABC +from bot_data.graphql.root_query import RootQuery + + +class GraphQL: + + def __init__(self): + QueryABC.init() + self._schema = graphene.Schema(query=RootQuery) + + @property + def schema(self) -> Schema: + return self._schema diff --git a/kdb-bot/src/bot_data/graphql/query/__init__.py b/kdb-bot/src/bot_data/graphql/queries/__init__.py similarity index 100% rename from kdb-bot/src/bot_data/graphql/query/__init__.py rename to kdb-bot/src/bot_data/graphql/queries/__init__.py diff --git a/kdb-bot/src/bot_data/graphql/queries/client_query.py b/kdb-bot/src/bot_data/graphql/queries/client_query.py new file mode 100644 index 00000000..a900cd58 --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/queries/client_query.py @@ -0,0 +1,12 @@ +import graphene + +from bot_data.abc.queryABC import QueryABC +from bot_data.graphql.types.client_query_type import ClientQueryType + + +class ClientQuery(QueryABC): + clients = graphene.List(ClientQueryType) + + @classmethod + def resolve_clients(cls, root, info): + return cls._clients diff --git a/kdb-bot/src/bot_data/graphql/query/server_query.py b/kdb-bot/src/bot_data/graphql/queries/server_query.py similarity index 72% rename from kdb-bot/src/bot_data/graphql/query/server_query.py rename to kdb-bot/src/bot_data/graphql/queries/server_query.py index d0fa2acc..9092ddd2 100644 --- a/kdb-bot/src/bot_data/graphql/query/server_query.py +++ b/kdb-bot/src/bot_data/graphql/queries/server_query.py @@ -1,12 +1,12 @@ import graphene from cpl_core.dependency_injection import ServiceProviderABC -from graphene import ObjectType +from bot_data.abc.queryABC import QueryABC from bot_data.graphql.types.server_query_type import ServerQueryType from bot_data.service.server_repository_service import ServerRepositoryService -class ServerQuery(ObjectType): +class ServerQuery(QueryABC): servers = graphene.List(ServerQueryType) @staticmethod @@ -14,6 +14,6 @@ class ServerQuery(ObjectType): def _get_servers(servers: ServerRepositoryService): return servers.get_servers() - @staticmethod - def resolve_servers(root, info): - return ServerQuery._get_servers() + @classmethod + def resolve_servers(cls, root, info): + return cls._servers diff --git a/kdb-bot/src/bot_data/graphql/query/client_query.py b/kdb-bot/src/bot_data/graphql/query/client_query.py deleted file mode 100644 index 2e34fbdd..00000000 --- a/kdb-bot/src/bot_data/graphql/query/client_query.py +++ /dev/null @@ -1,19 +0,0 @@ -import graphene -from cpl_core.dependency_injection import ServiceProviderABC -from graphene import ObjectType - -from bot_data.graphql.types.client_query_type import ClientQueryType -from bot_data.service.client_repository_service import ClientRepositoryService - - -class ClientQuery(ObjectType): - clients = graphene.List(ClientQueryType) - - @staticmethod - @ServiceProviderABC.inject - def _get_clients(clients: ClientRepositoryService): - return clients.get_clients() - - @staticmethod - def resolve_clients(root, info): - return ClientQuery._get_clients() diff --git a/kdb-bot/src/bot_data/graphql/root_query.py b/kdb-bot/src/bot_data/graphql/root_query.py index 057820bb..40590f12 100644 --- a/kdb-bot/src/bot_data/graphql/root_query.py +++ b/kdb-bot/src/bot_data/graphql/root_query.py @@ -1,12 +1,10 @@ from graphene import ObjectType, relay -from bot_data.graphql.query.client_query import ClientQuery -from bot_data.graphql.query.server_query import ServerQuery +from bot_data.graphql.queries.server_query import ServerQuery class RootQuery( ServerQuery, - ClientQuery, ObjectType ): node = relay.Node.Field() diff --git a/kdb-bot/src/bot_data/graphql/schema.py b/kdb-bot/src/bot_data/graphql/schema.py deleted file mode 100644 index bf335aee..00000000 --- a/kdb-bot/src/bot_data/graphql/schema.py +++ /dev/null @@ -1,5 +0,0 @@ -import graphene - -from bot_data.graphql.root_query import RootQuery - -schema = graphene.Schema(query=RootQuery) diff --git a/kdb-bot/src/bot_data/graphql/types/client_query_type.py b/kdb-bot/src/bot_data/graphql/types/client_query_type.py index 3d62c3f8..30630989 100644 --- a/kdb-bot/src/bot_data/graphql/types/client_query_type.py +++ b/kdb-bot/src/bot_data/graphql/types/client_query_type.py @@ -1,11 +1,12 @@ -from graphene import ObjectType, Int +from graphene import Int, String +from bot_data.abc.queryABC import QueryABC from bot_data.model.client import Client -class ClientQueryType(ObjectType): +class ClientQueryType(QueryABC): id = Int() - discord_client_id = Int() + discord_id = String() sent_message_count = Int() received_message_count = Int() deleted_message_count = Int() diff --git a/kdb-bot/src/bot_data/graphql/types/server_query_type.py b/kdb-bot/src/bot_data/graphql/types/server_query_type.py index 1ca96651..ca719507 100644 --- a/kdb-bot/src/bot_data/graphql/types/server_query_type.py +++ b/kdb-bot/src/bot_data/graphql/types/server_query_type.py @@ -1,30 +1,23 @@ -from cpl_core.dependency_injection import ServiceProviderABC -from graphene import ObjectType, Int, DateTime, String, Field +from graphene import Int, DateTime, String, List +from bot_data.abc.queryABC import QueryABC from bot_data.graphql.types.client_query_type import ClientQueryType from bot_data.model.server import Server -from bot_data.service.client_repository_service import ClientRepositoryService -class ServerQueryType(ObjectType): +class ServerQueryType(QueryABC): id = Int() discord_server_id = String() created_at = DateTime() modified_at = DateTime() - client = Field(ClientQueryType) + clients = List(ClientQueryType) @staticmethod def resolve_id(server: Server, info): return server.server_id - @staticmethod - @ServiceProviderABC.inject - def _get_clients(clients: ClientRepositoryService): - return clients - - @staticmethod - def resolve_client(server: Server, info): - clients: ClientRepositoryService = ServerQueryType._get_clients() - return clients.find_client_by_server_id(server.server_id) + @classmethod + def resolve_clients(cls, server: Server, info): + return cls._clients.where(lambda c: c.server.server_id == server.server_id)