From d89fc13e5e57ba18b6cdd87fbb1c5a30bbc342b2 Mon Sep 17 00:00:00 2001 From: MDoreto Date: Thu, 18 Jan 2024 01:24:44 -0300 Subject: [PATCH] :bug: fix: mutations params --- graphemy/models.py | 80 ++++++++++++++++++++++++---------------------- graphemy/router.py | 4 +-- pyproject.toml | 2 +- 3 files changed, 44 insertions(+), 42 deletions(-) diff --git a/graphemy/models.py b/graphemy/models.py index ba39170..b439b9b 100644 --- a/graphemy/models.py +++ b/graphemy/models.py @@ -70,7 +70,6 @@ class Graphemy(SQLModel): _schema = None _query = None _filter = None - _input = None __customfields__ = {} _default_mutation: bool = False _delete_mutation: bool = False @@ -155,28 +154,6 @@ class Schema: cls, all_fields=True, name=f'{cls.__name__}Schema' )(Schema) - @classmethod - @property - def input(cls): - - if not cls._input.default: - - class Filter: - pass - - for field_name, field in cls.__annotations__.items(): - setattr( - Filter, - field_name, - strawberry.field( - default=None, graphql_type=Optional[field] - ), - ) - cls._input.default = strawberry.input(name=f'{cls.__name__}Input')( - Filter - ) - return cls._input.default - @classmethod @property def mutation(cls): @@ -184,7 +161,18 @@ def mutation(cls): pk = [pk.name for pk in insp(cls).primary_key] - async def mutation(self, params: cls.input) -> cls.schema: + class Filter: + pass + + for field_name, field in cls.__annotations__.items(): + setattr( + Filter, + field_name, + strawberry.field(default=None, graphql_type=Optional[field]), + ) + input = strawberry.input(name=f'{cls.__name__}Input')(Filter) + + async def mutation(self, params: input) -> cls.schema: return await put_item(cls, params, pk) return mutation @@ -196,7 +184,21 @@ def delete_mutation(cls): pk = [pk.name for pk in insp(cls).primary_key] - async def mutation(self, params: cls.input) -> cls.schema: + class Filter: + pass + + for field_name, field in cls.__annotations__.items(): + if field_name in pk: + setattr( + Filter, + field_name, + strawberry.field( + default=None, graphql_type=Optional[field] + ), + ) + input = strawberry.input(name=f'{cls.__name__}InputPk')(Filter) + + async def mutation(self, params: input) -> cls.schema: return await delete_item(cls, params, pk) cls._delete = mutation @@ -215,27 +217,27 @@ def get_keys(model: 'Graphemy', id: str | list[str]) -> tuple | str: Retrieve one or multiple attributes or keys from a Graphemy instance. Args: - model (Graphemy): An instance of the Graphemy class from which attributes/keys will be retrieved. - id (str or list of str): The attribute/key name(s) to be retrieved from the model. + model (Graphemy): An instance of the Graphemy class from which attributes/keys will be retrieved. + id (str or list of str): The attribute/key name(s) to be retrieved from the model. Returns: - str, tuple, or any: The retrieved attribute(s) or key(s) from the model. If 'id' is a single string,the corresponding attribute/key value is returned. If 'id' is a list of strings, a tuple containing the corresponding attribute/key values in the order specified is returned. The returned values may be converted to strings if they are integers or have leading/trailing whitespaces. + str, tuple, or any: The retrieved attribute(s) or key(s) from the model. If 'id' is a single string,the corresponding attribute/key value is returned. If 'id' is a list of strings, a tuple containing the corresponding attribute/key values in the order specified is returned. The returned values may be converted to strings if they are integers or have leading/trailing whitespaces. Examples: - >>> from graphemy import Graphemy + >>> from graphemy import Graphemy - >>> class Hero(Graphemy): - ... name:str - ... power_level:int + >>> class Hero(Graphemy): + ... name:str + ... power_level:int - >>> hero_instance = Hero(name='Superman', power_level=100) + >>> hero_instance = Hero(name='Superman', power_level=100) - >>> get_keys(hero_instance, 'name') - 'Superman' - >>> get_keys(hero_instance, 'power_level') - 100 - >>> get_keys(hero_instance, ['name', 'power_level']) - ('Superman', 100) + >>> get_keys(hero_instance, 'name') + 'Superman' + >>> get_keys(hero_instance, 'power_level') + 100 + >>> get_keys(hero_instance, ['name', 'power_level']) + ('Superman', 100) """ if isinstance(id, list): return tuple([getattr(model, id[i]) for i in range(len(id))]) diff --git a/graphemy/router.py b/graphemy/router.py index c5a0d44..189be46 100644 --- a/graphemy/router.py +++ b/graphemy/router.py @@ -126,7 +126,7 @@ def __init__( need_mutation = False temp = strawberry.mutation( cls.mutation, - permission_classes=[cls.auth('delete_mutation')], + permission_classes=[cls.auth('mutation')], ) setattr( mutation, @@ -139,7 +139,7 @@ def __init__( 'delete_' + cls.__tablename__.lower(), strawberry.mutation( cls.delete_mutation, - permission_classes=[cls.auth('mutation')], + permission_classes=[cls.auth('delete_mutation')], ), ) if need_query: diff --git a/pyproject.toml b/pyproject.toml index ddacce2..f01e21d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "graphemy" -version = "0.3.2" +version = "0.3.3" description = "A Python library for integrating SQLModel and Strawberry, providing a seamless GraphQL integration with FastAPI and advanced features for database interactions." authors = ["Matheus Doreto "] readme = "README.md"