From c75cc54d166e27e3a2d548f8a331b22cc1fbbebc Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 15 Jan 2023 17:47:27 +0100 Subject: [PATCH] Improved queries #162 --- kdb-bot/src/bot_graphql/graphql_module.py | 2 ++ kdb-bot/src/bot_graphql/model.gql | 7 ++++--- .../src/bot_graphql/queries/auto_role_query.py | 4 ++-- .../bot_graphql/queries/known_user_query.py | 18 ++++++++++++++++++ .../src/bot_graphql/queries/server_query.py | 2 +- kdb-bot/src/bot_graphql/queries/user_query.py | 6 ++++-- 6 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/queries/known_user_query.py diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index a84d6c00..0d419007 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -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_rule_query import AutoRoleRuleQuery 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.server_query import ServerQuery from bot_graphql.queries.user_joined_server_query import UserJoinedServerQuery @@ -41,6 +42,7 @@ class GraphQLModule(ModuleABC): services.add_singleton(Query) services.add_singleton(Mutation) services.add_transient(QueryABC, ServerQuery) + services.add_transient(QueryABC, KnownUserQuery) services.add_transient(QueryABC, LevelQuery) services.add_transient(QueryABC, LevelMutation) services.add_transient(QueryABC, ClientQuery) diff --git a/kdb-bot/src/bot_graphql/model.gql b/kdb-bot/src/bot_graphql/model.gql index 77661c7f..98fd3dc7 100644 --- a/kdb-bot/src/bot_graphql/model.gql +++ b/kdb-bot/src/bot_graphql/model.gql @@ -38,6 +38,7 @@ type Server implements TableQuery { id: ID discord_id: String name: String + auto_roles: [AutoRole] clients: [Client] members: [User] levels: [Level] @@ -90,7 +91,7 @@ input UserFilter { discord_id: String name: String xp: Int - ontime: Int + ontime: Float level: LevelFilter page_index: Int @@ -104,11 +105,11 @@ type User implements TableQuery { discord_id: String name: String xp: Int - ontime: Int + ontime: Float level: Level joined_servers: [UserJoinedServer] - joined_voice_channel: [UserJoinedVoiceChannel] + joined_voice_channels: [UserJoinedVoiceChannel] server: Server diff --git a/kdb-bot/src/bot_graphql/queries/auto_role_query.py b/kdb-bot/src/bot_graphql/queries/auto_role_query.py index 8a62dcca..f6ba53ef 100644 --- a/kdb-bot/src/bot_graphql/queries/auto_role_query.py +++ b/kdb-bot/src/bot_graphql/queries/auto_role_query.py @@ -35,13 +35,13 @@ class AutoRoleQuery(DataQueryABC): def resolve_message_id(x: AutoRole, *_): 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: return filter.filter(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: return filter.filter(self._auto_role_rules.get_auto_role_rules_by_auto_role_id(x.auto_role_id)) diff --git a/kdb-bot/src/bot_graphql/queries/known_user_query.py b/kdb-bot/src/bot_graphql/queries/known_user_query.py new file mode 100644 index 00000000..4de78ba7 --- /dev/null +++ b/kdb-bot/src/bot_graphql/queries/known_user_query.py @@ -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 diff --git a/kdb-bot/src/bot_graphql/queries/server_query.py b/kdb-bot/src/bot_graphql/queries/server_query.py index e4c9dccf..ece81023 100644 --- a/kdb-bot/src/bot_graphql/queries/server_query.py +++ b/kdb-bot/src/bot_graphql/queries/server_query.py @@ -50,7 +50,7 @@ class ServerQuery(DataQueryABC): guild = self._bot.get_guild(server.discord_server_id) 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: return filter.filter(self._auto_roles.get_auto_roles_by_server_id(server.server_id)) diff --git a/kdb-bot/src/bot_graphql/queries/user_query.py b/kdb-bot/src/bot_graphql/queries/user_query.py index 0e10d7da..43a6e6c9 100644 --- a/kdb-bot/src/bot_graphql/queries/user_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_query.py @@ -32,7 +32,7 @@ class UserQuery(DataQueryABC): self.set_field("ontime", self.resolve_ontime) self.set_field("level", self.resolve_level) 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) @staticmethod @@ -44,7 +44,9 @@ class UserQuery(DataQueryABC): return user.discord_id 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 def resolve_xp(user: User, *_):