Changeset View
Changeset View
Standalone View
Standalone View
swh/graphql/resolvers/base_connection.py
# Copyright (C) 2022 The Software Heritage developers | # Copyright (C) 2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from abc import ABC, abstractmethod | from abc import ABC, abstractmethod | ||||
import binascii | import binascii | ||||
from dataclasses import dataclass | from dataclasses import dataclass | ||||
from typing import Any, List, Optional, Type, Union | from typing import Any, List, Optional, Type, Union | ||||
from graphql.type import GraphQLResolveInfo | |||||
from swh.graphql.backends.archive import Archive | from swh.graphql.backends.archive import Archive | ||||
from swh.graphql.backends.search import Search | from swh.graphql.backends.search import Search | ||||
from swh.graphql.errors import PaginationError | from swh.graphql.errors import PaginationError | ||||
from swh.graphql.utils import utils | from swh.graphql.utils import utils | ||||
from swh.storage.interface import PagedResult | from swh.storage.interface import PagedResult | ||||
from .base_node import BaseNode | from .base_node import BaseNode | ||||
Show All 15 Lines | class BaseConnection(ABC): | ||||
Base resolver for all the connections | Base resolver for all the connections | ||||
""" | """ | ||||
_node_class: Optional[Type[BaseNode]] = None | _node_class: Optional[Type[BaseNode]] = None | ||||
_page_size: int = 50 # default page size (default value for the first arg) | _page_size: int = 50 # default page size (default value for the first arg) | ||||
_max_page_size: int = 1000 # maximum page size(max value for the first arg) | _max_page_size: int = 1000 # maximum page size(max value for the first arg) | ||||
def __init__(self, obj, info, paged_data=None, **kwargs): | def __init__(self, obj, info, paged_data=None, **kwargs): | ||||
self.obj: Optional[Any] = obj | self.obj: Optional[BaseNode] = obj | ||||
self.info = info | self.info: GraphQLResolveInfo = info | ||||
self.kwargs = kwargs | self.kwargs = kwargs | ||||
# initialize commonly used vars | |||||
self.archive = Archive() | self.archive = Archive() | ||||
self.search = Search() | self.search = Search() | ||||
self._paged_data: PagedResult = paged_data | self._paged_data: PagedResult = paged_data | ||||
@property | @property | ||||
def edges(self) -> List[ConnectionEdge]: | def edges(self) -> List[ConnectionEdge]: | ||||
""" | """ | ||||
Return the list of connection edges, each with a cursor | Return the list of connection edges, each with a cursor | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | class BaseConnection(ABC): | ||||
def _get_paged_result(self): | def _get_paged_result(self): | ||||
""" | """ | ||||
Override for desired behaviour | Override for desired behaviour | ||||
return a PagedResult object | return a PagedResult object | ||||
""" | """ | ||||
# FIXME, make this call async (not for v1) | # FIXME, make this call async (not for v1) | ||||
return None | return None | ||||
def _get_after_arg(self) -> str: | def _get_after_arg(self): | ||||
""" | """ | ||||
Return the decoded next page token. Override to support a different | Return the decoded next page token. Override to support a different | ||||
cursor type | cursor type | ||||
""" | """ | ||||
# different implementation is used in SnapshotBranchConnection | # different implementation is used in SnapshotBranchConnection | ||||
try: | try: | ||||
cursor = utils.get_decoded_cursor(self.kwargs.get("after")) | cursor = utils.get_decoded_cursor(self.kwargs.get("after")) | ||||
except (UnicodeDecodeError, binascii.Error) as e: | except (UnicodeDecodeError, binascii.Error) as e: | ||||
Show All 23 Lines |