Improved gql data layer

This commit is contained in:
Sven Heidemann 2023-01-06 14:47:45 +01:00
parent 5455a6b359
commit 75500076a7
13 changed files with 101 additions and 84 deletions

View File

@ -23,7 +23,7 @@ from bot_api.exception.service_exception import ServiceException
from bot_api.logging.api_logger import ApiLogger from bot_api.logging.api_logger import ApiLogger
from bot_api.model.error_dto import ErrorDTO from bot_api.model.error_dto import ErrorDTO
from bot_api.route.route import Route from bot_api.route.route import Route
from bot_data.graphql.schema import schema from bot_data.graphql.graphql import GraphQL
class Api(Flask): class Api(Flask):
@ -64,11 +64,12 @@ class Api(Flask):
self._requests = {} self._requests = {}
gql = GraphQL()
self.add_url_rule( self.add_url_rule(
'/api/graphql', '/api/graphql',
view_func=GraphQLView.as_view( view_func=GraphQLView.as_view(
'graphql', 'graphql',
schema=schema, schema=gql.schema,
graphiql=True # for having the GraphiQL interface graphiql=True # for having the GraphiQL interface
) )
) )

View File

@ -47,7 +47,6 @@ class ApiModule(ModuleABC):
services.add_transient(GuiController) services.add_transient(GuiController)
services.add_transient(DiscordService) services.add_transient(DiscordService)
services.add_transient(ServerController) services.add_transient(ServerController)
#services.add_transient(GraphQLController)
# cpl-discord # cpl-discord
self._dc.add_event(DiscordEventTypesEnum.on_ready.value, BotApiOnReadyEvent) self._dc.add_event(DiscordEventTypesEnum.on_ready.value, BotApiOnReadyEvent)

View File

@ -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

View File

@ -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()

View File

@ -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

View File

@ -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

View File

@ -1,12 +1,12 @@
import graphene import graphene
from cpl_core.dependency_injection import ServiceProviderABC 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.graphql.types.server_query_type import ServerQueryType
from bot_data.service.server_repository_service import ServerRepositoryService from bot_data.service.server_repository_service import ServerRepositoryService
class ServerQuery(ObjectType): class ServerQuery(QueryABC):
servers = graphene.List(ServerQueryType) servers = graphene.List(ServerQueryType)
@staticmethod @staticmethod
@ -14,6 +14,6 @@ class ServerQuery(ObjectType):
def _get_servers(servers: ServerRepositoryService): def _get_servers(servers: ServerRepositoryService):
return servers.get_servers() return servers.get_servers()
@staticmethod @classmethod
def resolve_servers(root, info): def resolve_servers(cls, root, info):
return ServerQuery._get_servers() return cls._servers

View File

@ -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()

View File

@ -1,12 +1,10 @@
from graphene import ObjectType, relay from graphene import ObjectType, relay
from bot_data.graphql.query.client_query import ClientQuery from bot_data.graphql.queries.server_query import ServerQuery
from bot_data.graphql.query.server_query import ServerQuery
class RootQuery( class RootQuery(
ServerQuery, ServerQuery,
ClientQuery,
ObjectType ObjectType
): ):
node = relay.Node.Field() node = relay.Node.Field()

View File

@ -1,5 +0,0 @@
import graphene
from bot_data.graphql.root_query import RootQuery
schema = graphene.Schema(query=RootQuery)

View File

@ -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 from bot_data.model.client import Client
class ClientQueryType(ObjectType): class ClientQueryType(QueryABC):
id = Int() id = Int()
discord_client_id = Int() discord_id = String()
sent_message_count = Int() sent_message_count = Int()
received_message_count = Int() received_message_count = Int()
deleted_message_count = Int() deleted_message_count = Int()

View File

@ -1,30 +1,23 @@
from cpl_core.dependency_injection import ServiceProviderABC from graphene import Int, DateTime, String, List
from graphene import ObjectType, Int, DateTime, String, Field
from bot_data.abc.queryABC import QueryABC
from bot_data.graphql.types.client_query_type import ClientQueryType from bot_data.graphql.types.client_query_type import ClientQueryType
from bot_data.model.server import Server from bot_data.model.server import Server
from bot_data.service.client_repository_service import ClientRepositoryService
class ServerQueryType(ObjectType): class ServerQueryType(QueryABC):
id = Int() id = Int()
discord_server_id = String() discord_server_id = String()
created_at = DateTime() created_at = DateTime()
modified_at = DateTime() modified_at = DateTime()
client = Field(ClientQueryType) clients = List(ClientQueryType)
@staticmethod @staticmethod
def resolve_id(server: Server, info): def resolve_id(server: Server, info):
return server.server_id return server.server_id
@staticmethod @classmethod
@ServiceProviderABC.inject def resolve_clients(cls, server: Server, info):
def _get_clients(clients: ClientRepositoryService): return cls._clients.where(lambda c: c.server.server_id == server.server_id)
return clients
@staticmethod
def resolve_client(server: Server, info):
clients: ClientRepositoryService = ServerQueryType._get_clients()
return clients.find_client_by_server_id(server.server_id)