Skip to content

Commit

Permalink
🐛 fix: custom_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Matheus-Doreto committed Nov 15, 2023
1 parent 8de89aa commit 1b9467e
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 151 deletions.
6 changes: 6 additions & 0 deletions examples/tutorial/models/bank.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import strawberry
from sqlmodel import Field

from graphemy import MyModel, dl, get_one
Expand All @@ -6,6 +7,11 @@
class Bank(MyModel, table=True):
_default_mutation = True
_delete_mutation = True
__customfields__ = {
'custom': strawberry.field(
graphql_type=list[str], default_factory=['custom fields']
)
}
id: int = Field(primary_key=True)
name: str
code: int
Expand Down
30 changes: 16 additions & 14 deletions graphemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ class Schema:
],
),
)
for field, v in cls.__customfields__.items():
setattr(Schema, field, v)
cls._schema = strawberry.experimental.pydantic.type(
cls, all_fields=True, name=f'{cls.__name__}Schema'
)(Schema)
Expand Down Expand Up @@ -221,27 +223,27 @@ def get_keys(model: 'MyModel', id: str | list[str]) -> tuple | str:
Retrieve one or multiple attributes or keys from a MyModel instance.
Args:
model (MyModel): An instance of the MyModel 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 (MyModel): An instance of the MyModel 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 MyModel
>>> from graphemy import MyModel
>>> class Hero(MyModel):
... name:str
... power_level:int
>>> class Hero(MyModel):
... 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
Loading

0 comments on commit 1b9467e

Please sign in to comment.