Added mutations without saving data

This commit is contained in:
Sven Heidemann 2023-01-07 16:31:28 +01:00
parent 44204f5b94
commit df42acec26
6 changed files with 85 additions and 6 deletions

View File

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

View File

@ -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
@ -103,3 +107,17 @@ 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
}

View File

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

View File

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

View File

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