diff --git a/swh/graphql/resolvers/base_connection.py b/swh/graphql/resolvers/base_connection.py --- a/swh/graphql/resolvers/base_connection.py +++ b/swh/graphql/resolvers/base_connection.py @@ -6,10 +6,11 @@ from abc import ABC, abstractmethod import binascii from dataclasses import dataclass -from typing import Any, Optional, Type +from typing import Any, List, Optional, Type, Union from swh.graphql.errors import PaginationError from swh.graphql.utils import utils +from swh.storage.interface import PagedResult from .base_node import BaseNode @@ -17,13 +18,13 @@ @dataclass class PageInfo: hasNextPage: bool - endCursor: str + endCursor: Optional[str] @dataclass class ConnectionEdge: node: Any - cursor: str + cursor: Optional[str] class BaseConnection(ABC): @@ -36,17 +37,23 @@ _max_page_size: int = 1000 # maximum page size(max value for the first arg) def __init__(self, obj, info, paged_data=None, **kwargs): - self.obj = obj + self.obj: Optional[Any] = obj self.info = info self.kwargs = kwargs - self._paged_data = paged_data + self._paged_data: PagedResult = paged_data @property - def edges(self): - return self._get_edges() + def edges(self) -> List[ConnectionEdge]: + """ + Return the list of connection edges, each with a cursor + """ + return [ + ConnectionEdge(node=node, cursor=self._get_index_cursor(index, node)) + for (index, node) in enumerate(self.nodes) + ] @property - def nodes(self): + def nodes(self) -> List[Union[BaseNode, object]]: """ Override if needed; return a list of objects @@ -63,7 +70,7 @@ return self.get_paged_data().results @property - def pageInfo(self): # To support the schema naming convention + def pageInfo(self) -> PageInfo: # To support the schema naming convention # FIXME, add more details like startCursor return PageInfo( hasNextPage=bool(self.get_paged_data().next_page_token), @@ -71,20 +78,17 @@ ) @property - def totalCount(self): # To support the schema naming convention - return self._get_total_count() - - def _get_total_count(self): + def totalCount(self) -> Optional[int]: # To support the schema naming convention """ Will be None for most of the connections override if needed/possible """ + return None - def get_paged_data(self): + def get_paged_data(self) -> PagedResult: """ - Cache to avoid multiple calls to - the backend (_get_paged_result) + Cache to avoid multiple calls to the backend (_get_paged_result) return a PagedResult object """ if self._paged_data is None: @@ -101,15 +105,6 @@ # FIXME, make this call async (not for v1) return None - def _get_edges(self): - """ - Return the list of connection edges, each with a cursor - """ - return [ - ConnectionEdge(node=node, cursor=self._get_index_cursor(index, node)) - for (index, node) in enumerate(self.nodes) - ] - def _get_after_arg(self) -> str: """ Return the decoded next page token. Override to support a different @@ -133,7 +128,7 @@ ) return first - def _get_index_cursor(self, index: int, node: Any): + def _get_index_cursor(self, index: int, node: Any) -> Optional[str]: """ Get the cursor to the given item index """ diff --git a/swh/graphql/resolvers/origin.py b/swh/graphql/resolvers/origin.py --- a/swh/graphql/resolvers/origin.py +++ b/swh/graphql/resolvers/origin.py @@ -14,6 +14,8 @@ Node resolver for an origin requested directly with its URL """ + obj: None + def _get_node_data(self): return archive.Archive().get_origin(self.kwargs.get("url")) diff --git a/swh/graphql/resolvers/release.py b/swh/graphql/resolvers/release.py --- a/swh/graphql/resolvers/release.py +++ b/swh/graphql/resolvers/release.py @@ -38,6 +38,8 @@ Node resolver for a release requested directly with its SWHID """ + obj: None + def _get_node_data(self): return self._get_release_by_id(self.kwargs.get("swhid").object_id) diff --git a/swh/graphql/resolvers/revision.py b/swh/graphql/resolvers/revision.py --- a/swh/graphql/resolvers/revision.py +++ b/swh/graphql/resolvers/revision.py @@ -51,6 +51,8 @@ Node resolver for a revision requested directly with its SWHID """ + obj: None + def _get_node_data(self): return self._get_revision_by_id(self.kwargs.get("swhid").object_id) diff --git a/swh/graphql/utils/utils.py b/swh/graphql/utils/utils.py --- a/swh/graphql/utils/utils.py +++ b/swh/graphql/utils/utils.py @@ -5,7 +5,7 @@ import base64 from datetime import datetime -from typing import List +from typing import List, Optional from swh.storage.interface import PagedResult @@ -18,7 +18,7 @@ return base64.b64encode(source).decode("ascii") -def get_encoded_cursor(cursor: str) -> str: +def get_encoded_cursor(cursor: Optional[str]) -> Optional[str]: if cursor is None: return None return get_b64_string(cursor)