Added first graphene models

This commit is contained in:
2023-01-06 14:14:37 +01:00
parent 9c466733fb
commit 5455a6b359
14 changed files with 184 additions and 18 deletions

View File

@@ -0,0 +1,19 @@
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

@@ -0,0 +1,19 @@
import graphene
from cpl_core.dependency_injection import ServiceProviderABC
from graphene import ObjectType
from bot_data.graphql.types.server_query_type import ServerQueryType
from bot_data.service.server_repository_service import ServerRepositoryService
class ServerQuery(ObjectType):
servers = graphene.List(ServerQueryType)
@staticmethod
@ServiceProviderABC.inject
def _get_servers(servers: ServerRepositoryService):
return servers.get_servers()
@staticmethod
def resolve_servers(root, info):
return ServerQuery._get_servers()

View File

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

View File

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

View File

@@ -0,0 +1,17 @@
from graphene import ObjectType, Int
from bot_data.model.client import Client
class ClientQueryType(ObjectType):
id = Int()
discord_client_id = Int()
sent_message_count = Int()
received_message_count = Int()
deleted_message_count = Int()
received_command_count = Int()
moved_users_count = Int()
@staticmethod
def resolve_id(client: Client, info):
return client.client_id

View File

@@ -0,0 +1,30 @@
from cpl_core.dependency_injection import ServiceProviderABC
from graphene import ObjectType, Int, DateTime, String, Field
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):
id = Int()
discord_server_id = String()
created_at = DateTime()
modified_at = DateTime()
client = Field(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)

View File

@@ -33,6 +33,8 @@ class ClientRepositoryService(ClientRepositoryABC):
result[5],
result[6],
self._servers.get_server_by_id(result[7]),
result[8],
result[9],
id=result[0]
))
@@ -49,6 +51,8 @@ class ClientRepositoryService(ClientRepositoryABC):
result[5],
result[6],
self._servers.get_server_by_id(result[7]),
result[8],
result[9],
id=result[0]
)
@@ -63,6 +67,8 @@ class ClientRepositoryService(ClientRepositoryABC):
result[5],
result[6],
self._servers.get_server_by_id(result[7]),
result[8],
result[9],
id=result[0]
)
@@ -71,9 +77,9 @@ class ClientRepositoryService(ClientRepositoryABC):
result = self._context.select(Client.get_select_by_discord_id_string(discord_id))
if result is None or len(result) == 0:
return None
result = result[0]
return Client(
result[1],
result[2],
@@ -82,6 +88,8 @@ class ClientRepositoryService(ClientRepositoryABC):
result[5],
result[6],
self._servers.get_server_by_id(result[7]),
result[8],
result[9],
id=result[0]
)
@@ -90,9 +98,9 @@ class ClientRepositoryService(ClientRepositoryABC):
result = self._context.select(Client.get_select_by_server_id_string(discord_id))
if result is None or len(result) == 0:
return None
result = result[0]
return Client(
result[1],
result[2],
@@ -101,6 +109,8 @@ class ClientRepositoryService(ClientRepositoryABC):
result[5],
result[6],
self._servers.get_server_by_id(result[7]),
result[8],
result[9],
id=result[0]
)
@@ -109,9 +119,9 @@ class ClientRepositoryService(ClientRepositoryABC):
result = self._context.select(Client.get_select_by_discord_id_and_server_id_string(discord_id, server_id))
if result is None or len(result) == 0:
return None
result = result[0]
return Client(
result[1],
result[2],
@@ -120,17 +130,19 @@ class ClientRepositoryService(ClientRepositoryABC):
result[5],
result[6],
self._servers.get_server_by_id(result[7]),
result[8],
result[9],
id=result[0]
)
def add_client(self, client: Client):
self._logger.trace(__name__, f'Send SQL command: {client.insert_string}')
self._context.cursor.execute(client.insert_string)
def update_client(self, client: Client):
self._logger.trace(__name__, f'Send SQL command: {client.udpate_string}')
self._context.cursor.execute(client.udpate_string)
def delete_client(self, client: Client):
self._logger.trace(__name__, f'Send SQL command: {client.delete_string}')
self._context.cursor.execute(client.delete_string)
@@ -140,12 +152,12 @@ class ClientRepositoryService(ClientRepositoryABC):
if server is None:
self._logger.warn(__name__, f'Cannot find server by id {server_id}')
raise Exception('Value not found')
client = self.find_client_by_discord_id_and_server_id(id, server.server_id)
if client is None:
self._logger.warn(__name__, f'Cannot find client by ids {id}@{server.server_id}')
raise Exception('Value not found')
return client
def append_sent_message_count(self, client_id: int, server_id: int, value: int):

View File

@@ -25,6 +25,8 @@ class ServerRepositoryService(ServerRepositoryABC):
for result in results:
servers.append(Server(
result[1],
result[2],
result[3],
id=result[0]
))
@@ -55,6 +57,8 @@ class ServerRepositoryService(ServerRepositoryABC):
result = self._context.select(Server.get_select_by_id_string(server_id))[0]
return Server(
result[1],
result[2],
result[3],
id=result[0]
)
@@ -63,6 +67,8 @@ class ServerRepositoryService(ServerRepositoryABC):
result = self._context.select(Server.get_select_by_discord_id_string(discord_id))[0]
return Server(
result[1],
result[2],
result[3],
id=result[0]
)
@@ -71,24 +77,24 @@ class ServerRepositoryService(ServerRepositoryABC):
result = self._context.select(Server.get_select_by_discord_id_string(discord_id))
if result is None or len(result) == 0:
return None
result = result[0]
return Server(
result[1],
result[2],
result[3],
id=result[0]
)
def add_server(self, server: Server):
self._logger.trace(__name__, f'Send SQL command: {server.insert_string}')
self._context.cursor.execute(server.insert_string)
def update_server(self, server: Server):
self._logger.trace(__name__, f'Send SQL command: {server.udpate_string}')
self._context.cursor.execute(server.udpate_string)
def delete_server(self, server: Server):
self._logger.trace(__name__, f'Send SQL command: {server.delete_string}')
self._context.cursor.execute(server.delete_string)