Improved queries #162

This commit is contained in:
Sven Heidemann 2023-01-15 17:47:27 +01:00
parent 6a1ad1ec9f
commit c75cc54d16
6 changed files with 31 additions and 8 deletions

View File

@ -19,6 +19,7 @@ from bot_graphql.mutations.level_mutation import LevelMutation
from bot_graphql.queries.auto_role_query import AutoRoleQuery from bot_graphql.queries.auto_role_query import AutoRoleQuery
from bot_graphql.queries.auto_role_rule_query import AutoRoleRuleQuery from bot_graphql.queries.auto_role_rule_query import AutoRoleRuleQuery
from bot_graphql.queries.client_query import ClientQuery from bot_graphql.queries.client_query import ClientQuery
from bot_graphql.queries.known_user_query import KnownUserQuery
from bot_graphql.queries.level_query import LevelQuery from bot_graphql.queries.level_query import LevelQuery
from bot_graphql.queries.server_query import ServerQuery from bot_graphql.queries.server_query import ServerQuery
from bot_graphql.queries.user_joined_server_query import UserJoinedServerQuery from bot_graphql.queries.user_joined_server_query import UserJoinedServerQuery
@ -41,6 +42,7 @@ class GraphQLModule(ModuleABC):
services.add_singleton(Query) services.add_singleton(Query)
services.add_singleton(Mutation) services.add_singleton(Mutation)
services.add_transient(QueryABC, ServerQuery) services.add_transient(QueryABC, ServerQuery)
services.add_transient(QueryABC, KnownUserQuery)
services.add_transient(QueryABC, LevelQuery) services.add_transient(QueryABC, LevelQuery)
services.add_transient(QueryABC, LevelMutation) services.add_transient(QueryABC, LevelMutation)
services.add_transient(QueryABC, ClientQuery) services.add_transient(QueryABC, ClientQuery)

View File

@ -38,6 +38,7 @@ type Server implements TableQuery {
id: ID id: ID
discord_id: String discord_id: String
name: String name: String
auto_roles: [AutoRole]
clients: [Client] clients: [Client]
members: [User] members: [User]
levels: [Level] levels: [Level]
@ -90,7 +91,7 @@ input UserFilter {
discord_id: String discord_id: String
name: String name: String
xp: Int xp: Int
ontime: Int ontime: Float
level: LevelFilter level: LevelFilter
page_index: Int page_index: Int
@ -104,11 +105,11 @@ type User implements TableQuery {
discord_id: String discord_id: String
name: String name: String
xp: Int xp: Int
ontime: Int ontime: Float
level: Level level: Level
joined_servers: [UserJoinedServer] joined_servers: [UserJoinedServer]
joined_voice_channel: [UserJoinedVoiceChannel] joined_voice_channels: [UserJoinedVoiceChannel]
server: Server server: Server

View File

@ -35,13 +35,13 @@ class AutoRoleQuery(DataQueryABC):
def resolve_message_id(x: AutoRole, *_): def resolve_message_id(x: AutoRole, *_):
return x.discord_message_id return x.discord_message_id
def resolve_server(self, x: AutoRole, *_, filter: ServerFilter): def resolve_server(self, x: AutoRole, *_, filter: ServerFilter = None):
if filter is not None: if filter is not None:
return filter.filter(self._servers.get_server_by_id(x.server_id)) return filter.filter(self._servers.get_server_by_id(x.server_id))
return self._servers.get_server_by_id(x.server_id) return self._servers.get_server_by_id(x.server_id)
def resolve_rules(self, x: AutoRole, *_, filter: AutoRoleRuleFilter): def resolve_rules(self, x: AutoRole, *_, filter: AutoRoleRuleFilter = None):
if filter is not None: if filter is not None:
return filter.filter(self._auto_role_rules.get_auto_role_rules_by_auto_role_id(x.auto_role_id)) return filter.filter(self._auto_role_rules.get_auto_role_rules_by_auto_role_id(x.auto_role_id))

View File

@ -0,0 +1,18 @@
from bot_data.model.known_user import KnownUser
from bot_graphql.abc.data_query_abc import DataQueryABC
class KnownUserQuery(DataQueryABC):
def __init__(self):
DataQueryABC.__init__(self, "KnownUser")
self.set_field("id", self.resolve_id)
self.set_field("discord_id", self.resolve_discord_id)
@staticmethod
def resolve_id(x: KnownUser, *_):
return x.known_user_id
@staticmethod
def resolve_discord_id(x: KnownUser, *_):
return x.discord_id

View File

@ -50,7 +50,7 @@ class ServerQuery(DataQueryABC):
guild = self._bot.get_guild(server.discord_server_id) guild = self._bot.get_guild(server.discord_server_id)
return None if guild is None else guild.name return None if guild is None else guild.name
def resolve_auto_roles(self, server: Server, *_, filter: AutoRoleFilter): def resolve_auto_roles(self, server: Server, *_, filter: AutoRoleFilter = None):
if filter is not None: if filter is not None:
return filter.filter(self._auto_roles.get_auto_roles_by_server_id(server.server_id)) return filter.filter(self._auto_roles.get_auto_roles_by_server_id(server.server_id))

View File

@ -32,7 +32,7 @@ class UserQuery(DataQueryABC):
self.set_field("ontime", self.resolve_ontime) self.set_field("ontime", self.resolve_ontime)
self.set_field("level", self.resolve_level) self.set_field("level", self.resolve_level)
self.set_field("joined_servers", self.resolve_joined_servers) self.set_field("joined_servers", self.resolve_joined_servers)
self.set_field("joined_voice_channel", self.resolve_joined_voice_channel) self.set_field("joined_voice_channels", self.resolve_joined_voice_channel)
self.set_field("server", self.resolve_server) self.set_field("server", self.resolve_server)
@staticmethod @staticmethod
@ -44,7 +44,9 @@ class UserQuery(DataQueryABC):
return user.discord_id return user.discord_id
def resolve_name(self, user: User, *_): def resolve_name(self, user: User, *_):
return self._bot.get_guild(user.server.discord_server_id).get_member(user.discord_id).name guild = self._bot.get_guild(user.server.discord_server_id)
user = guild.get_member(user.discord_id)
return None if user is None else user.name
@staticmethod @staticmethod
def resolve_xp(user: User, *_): def resolve_xp(user: User, *_):