From df42acec26f80e4aecb5cd1fc7ca2c2384ac4a5a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 7 Jan 2023 16:31:28 +0100 Subject: [PATCH] Added mutations without saving data --- kdb-bot/src/bot_graphql/graphql_module.py | 4 ++ kdb-bot/src/bot_graphql/model.gql | 28 +++++++++++--- kdb-bot/src/bot_graphql/mutation.py | 18 +++++++++ kdb-bot/src/bot_graphql/mutations/__init__.py | 0 .../bot_graphql/mutations/level_mutation.py | 37 +++++++++++++++++++ kdb-bot/src/bot_graphql/schema.py | 4 +- 6 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/mutation.py create mode 100644 kdb-bot/src/bot_graphql/mutations/__init__.py create mode 100644 kdb-bot/src/bot_graphql/mutations/level_mutation.py diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index 4678a4c1..dba88df5 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -8,6 +8,8 @@ from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from bot_data.service.seeder_service import SeederService from bot_graphql.abc.query_abc import QueryABC from bot_graphql.graphql_service import GraphQLService +from bot_graphql.mutation import Mutation +from bot_graphql.mutations.level_mutation import LevelMutation from bot_graphql.queries.level_query import LevelQuery from bot_graphql.queries.server_query import ServerQuery from bot_graphql.query import Query @@ -27,7 +29,9 @@ class GraphQLModule(ModuleABC): services.add_singleton(Schema) services.add_singleton(GraphQLService) services.add_singleton(Query) + services.add_singleton(Mutation) services.add_transient(QueryABC, ServerQuery) services.add_transient(QueryABC, LevelQuery) + services.add_transient(QueryABC, LevelMutation) services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_graphql/model.gql b/kdb-bot/src/bot_graphql/model.gql index 93843058..727b92c1 100644 --- a/kdb-bot/src/bot_graphql/model.gql +++ b/kdb-bot/src/bot_graphql/model.gql @@ -1,14 +1,18 @@ +interface TableQuery { + created_at: String + modified_at: String +} + +type Mutation { + level: LevelMutation +} + type Query { servers(filter: ServerFilter): [Server] server_count: Int known_users: [User] } -interface TableQuery { - created_at: String - modified_at: String -} - input ServerFilter { id: ID discord_id: String @@ -102,4 +106,18 @@ type Level implements TableQuery { created_at: String modified_at: String +} + +input LevelInput { + name: String! + color: String! + min_xp: Int! + permissions: String! + server_id: ID! +} + +type LevelMutation { + create_level(input: LevelInput!): Level + update_level(input: LevelInput!): Level + delete_level(id: ID): Level } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/mutation.py b/kdb-bot/src/bot_graphql/mutation.py new file mode 100644 index 00000000..52b19a9f --- /dev/null +++ b/kdb-bot/src/bot_graphql/mutation.py @@ -0,0 +1,18 @@ +from ariadne import MutationType + +from bot_graphql.mutations.level_mutation import LevelMutation + + +class Mutation(MutationType): + + def __init__( + self, + level_mutation: LevelMutation + ): + MutationType.__init__(self) + + self._level_mutation = level_mutation + self.set_field('level', self.resolve_level) + + def resolve_level(self, *_): + return self._level_mutation diff --git a/kdb-bot/src/bot_graphql/mutations/__init__.py b/kdb-bot/src/bot_graphql/mutations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/kdb-bot/src/bot_graphql/mutations/level_mutation.py b/kdb-bot/src/bot_graphql/mutations/level_mutation.py new file mode 100644 index 00000000..b1917843 --- /dev/null +++ b/kdb-bot/src/bot_graphql/mutations/level_mutation.py @@ -0,0 +1,37 @@ +from bot_data.abc.level_repository_abc import LevelRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.model.level import Level +from bot_graphql.abc.query_abc import QueryABC + + +class LevelMutation(QueryABC): + + def __init__( + self, + servers: ServerRepositoryABC, + levels: LevelRepositoryABC + ): + QueryABC.__init__(self, 'LevelMutation') + + self._servers = servers + self._levels = levels + + self.set_field('create_level', self.resolve_create_level) + self.set_field('update_level', self.resolve_create_level) + self.set_field('delete_level', self.resolve_create_level) + + def resolve_create_level(self, *_, input: dict): + level = Level( + input['name'], + input['color'], + int(input['min_xp']), + int(input['permissions']), + self._servers.get_server_by_id(input['server_id']) + ) + return level + + def resolve_update_level(self, *_, input): + return self._levels.get_level_by_id(input.id) + + def resolve_delete_level(self, *_, id: int): + return self._levels.get_level_by_id(id) diff --git a/kdb-bot/src/bot_graphql/schema.py b/kdb-bot/src/bot_graphql/schema.py index f918e9dc..da5e2948 100644 --- a/kdb-bot/src/bot_graphql/schema.py +++ b/kdb-bot/src/bot_graphql/schema.py @@ -4,6 +4,7 @@ from ariadne import make_executable_schema, load_schema_from_path from graphql import GraphQLSchema from bot_graphql.abc.query_abc import QueryABC +from bot_graphql.mutation import Mutation from bot_graphql.query import Query @@ -12,10 +13,11 @@ class Schema: def __init__( self, query: Query, + mutation: Mutation, queries: list[QueryABC] ): type_defs = load_schema_from_path(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'model.gql')) - self._schema = make_executable_schema(type_defs, query, *queries) + self._schema = make_executable_schema(type_defs, query, mutation, *queries) @property def schema(self) -> GraphQLSchema: