Skip to content

Commit

Permalink
🐛 fix: mutations params
Browse files Browse the repository at this point in the history
  • Loading branch information
MDoreto committed Jan 18, 2024
1 parent e5b5b8e commit d89fc13
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 42 deletions.
80 changes: 41 additions & 39 deletions graphemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ class Graphemy(SQLModel):
_schema = None
_query = None
_filter = None
_input = None
__customfields__ = {}
_default_mutation: bool = False
_delete_mutation: bool = False
Expand Down Expand Up @@ -155,36 +154,25 @@ 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):
from sqlalchemy.inspection import inspect as insp

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
Expand All @@ -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
Expand All @@ -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))])
Expand Down
4 changes: 2 additions & 2 deletions graphemy/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit d89fc13

Please sign in to comment.