Further gql improvements & added test data #181
This commit is contained in:
@@ -18,7 +18,7 @@ class CollectionGraphTypeFactory:
|
||||
|
||||
gql_type = strawberry.type(
|
||||
type(
|
||||
f"{node_type.__name__}Collection",
|
||||
f"{node_type.__name__.replace("GraphType", "")}Collection",
|
||||
(),
|
||||
{
|
||||
"__annotations__": {
|
||||
|
||||
@@ -4,6 +4,7 @@ from typing import Callable, Type, Any, Optional
|
||||
import strawberry
|
||||
from strawberry.exceptions import StrawberryException
|
||||
|
||||
from cpl.database.abc.data_access_object_abc import DataAccessObjectABC
|
||||
from cpl.dependency.inject import inject
|
||||
from cpl.dependency.service_provider import ServiceProvider
|
||||
from cpl.graphql.abc.strawberry_protocol import StrawberryProtocol
|
||||
@@ -69,9 +70,6 @@ class Query(StrawberryProtocol):
|
||||
sort_type: Type[StrawberryProtocol],
|
||||
resolver: Callable,
|
||||
) -> Field:
|
||||
# self._schema.with_type(filter_type)
|
||||
# self._schema.with_type(sort_type)
|
||||
|
||||
def _resolve_collection(filter=None, sort=None, skip=0, take=10):
|
||||
items = resolver()
|
||||
if filter:
|
||||
@@ -103,6 +101,53 @@ class Query(StrawberryProtocol):
|
||||
f.with_argument(int, "take", default_value=10)
|
||||
return f
|
||||
|
||||
def dao_collection_field(
|
||||
self,
|
||||
t: Type[StrawberryProtocol],
|
||||
dao_type: Type[DataAccessObjectABC],
|
||||
name: str,
|
||||
filter_type: Type[StrawberryProtocol],
|
||||
sort_type: Type[StrawberryProtocol],
|
||||
) -> Field:
|
||||
assert issubclass(dao_type, DataAccessObjectABC), "dao_type must be a subclass of DataAccessObjectABC"
|
||||
dao = self._provider.get_service(dao_type)
|
||||
if not dao:
|
||||
raise ValueError(f"DAO '{dao_type.__name__}' not registered in service provider")
|
||||
|
||||
filter = self._provider.get_service(filter_type)
|
||||
if not filter:
|
||||
raise ValueError(f"Filter '{filter_type.__name__}' not registered in service provider")
|
||||
|
||||
sort = self._provider.get_service(sort_type)
|
||||
if not sort:
|
||||
raise ValueError(f"Sort '{sort_type.__name__}' not registered in service provider")
|
||||
|
||||
async def _resolver(filter=None, sort=None, take=10, skip=0):
|
||||
sort_dict = None
|
||||
|
||||
if sort is not None:
|
||||
sort_dict = {}
|
||||
for k, v in sort.__dict__.items():
|
||||
if v is None:
|
||||
continue
|
||||
|
||||
if isinstance(v, SortOrder):
|
||||
sort_dict[k] = str(v.value).lower()
|
||||
continue
|
||||
|
||||
sort_dict[k] = str(v).lower()
|
||||
|
||||
total_count = await dao.count(filter)
|
||||
data = await dao.find_by(filter, sort_dict, take, skip)
|
||||
return Collection(nodes=data, total_count=total_count, count=len(data))
|
||||
|
||||
f = self.field(name, CollectionGraphTypeFactory.get(t), _resolver)
|
||||
f.with_argument(filter.to_strawberry(), "filter")
|
||||
f.with_argument(sort.to_strawberry(), "sort")
|
||||
f.with_argument(int, "skip", default_value=0)
|
||||
f.with_argument(int, "take", default_value=10)
|
||||
return f
|
||||
|
||||
@staticmethod
|
||||
def _build_resolver(f: "Field"):
|
||||
params: list[inspect.Parameter] = []
|
||||
@@ -164,4 +209,4 @@ class Query(StrawberryProtocol):
|
||||
namespace[name] = self._field_to_strawberry(f)
|
||||
|
||||
namespace["__annotations__"] = annotations
|
||||
return strawberry.type(type(f"{self.__class__.__name__}GraphType", (), namespace))
|
||||
return strawberry.type(type(f"{self.__class__.__name__.replace("GraphType", "")}", (), namespace))
|
||||
|
||||
@@ -2,5 +2,5 @@ from enum import Enum, auto
|
||||
|
||||
|
||||
class SortOrder(Enum):
|
||||
ASC = auto()
|
||||
DESC = auto()
|
||||
ASC = "ASC"
|
||||
DESC = "DESC"
|
||||
Reference in New Issue
Block a user