Changeset View
Changeset View
Standalone View
Standalone View
swh/graphql/resolvers/snapshot_branch.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 collections import namedtuple | from collections import namedtuple | ||||
from swh.graphql.backends import archive | from swh.graphql.backends import archive | ||||
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_connection import BaseConnection | from .base_connection import BaseConnection | ||||
from .base_node import BaseNode | from .base_node import BaseNode | ||||
from .snapshot import SnapshotNode | |||||
class SnapshotBranchNode(BaseNode): | class SnapshotBranchNode(BaseNode): | ||||
""" | """ | ||||
target field for this Node is a UNION in the schema | Node resolver for a snapshot branch | ||||
It is resolved in resolvers.resolvers.py | |||||
""" | """ | ||||
# target field for this Node is a UNION type | |||||
# It is resolved in the top level (resolvers.resolvers.py) | |||||
def _get_node_from_data(self, node_data): | def _get_node_from_data(self, node_data): | ||||
""" | # node_data is not a dict in this case | ||||
node_data is not a dict in this case | # overriding to support this special data structure | ||||
overriding to support this special data structure | |||||
""" | |||||
# STORAGE-TODO; return an object in the normal format | # STORAGE-TODO; return an object in the normal format | ||||
branch_name, branch_obj = node_data | branch_name, branch_obj = node_data | ||||
node = { | node = { | ||||
"name": branch_name, | "name": branch_name, | ||||
"type": branch_obj.target_type.value, | "type": branch_obj.target_type.value, | ||||
"target": branch_obj.target, | "target": branch_obj.target, | ||||
} | } | ||||
return namedtuple("NodeObj", node.keys())(*node.values()) | return namedtuple("NodeObj", node.keys())(*node.values()) | ||||
@property | @property | ||||
def targetHash(self): # To support the schema naming convention | def targetHash(self): # To support the schema naming convention | ||||
return self._node.target | return self._node.target | ||||
class SnapshotBranchConnection(BaseConnection): | class SnapshotBranchConnection(BaseConnection): | ||||
_node_class = SnapshotBranchNode | |||||
def _get_paged_result(self): | |||||
""" | """ | ||||
When branches requested from a snapshot | Connection resolver for the branches in a snapshot | ||||
self.obj.swhid is the snapshot SWHID here | |||||
(as returned from resolvers/snapshot.py) | |||||
""" | """ | ||||
obj: SnapshotNode | |||||
_node_class = SnapshotBranchNode | |||||
def _get_paged_result(self): | |||||
# self.obj.swhid is the snapshot SWHID | |||||
result = archive.Archive().get_snapshot_branches( | result = archive.Archive().get_snapshot_branches( | ||||
self.obj.swhid.object_id, | self.obj.swhid.object_id, | ||||
after=self._get_after_arg(), | after=self._get_after_arg(), | ||||
first=self._get_first_arg(), | first=self._get_first_arg(), | ||||
target_types=self.kwargs.get("types"), | target_types=self.kwargs.get("types"), | ||||
name_include=self.kwargs.get("nameInclude"), | name_include=self.kwargs.get("nameInclude"), | ||||
) | ) | ||||
# FIXME Cursor must be a hex to be consistent with | # FIXME Cursor must be a hex to be consistent with | ||||
# the base class, hack to make that work | # the base class, hack to make that work | ||||
end_cusrsor = ( | end_cusrsor = ( | ||||
result["next_branch"].hex() if result["next_branch"] is not None else None | result["next_branch"].hex() if result["next_branch"] is not None else None | ||||
) | ) | ||||
# FIXME, this pagination is not consistent with other connections | # FIXME, this pagination is not consistent with other connections | ||||
# FIX in swh-storage to return PagedResult | # FIX in swh-storage to return PagedResult | ||||
# STORAGE-TODO | # STORAGE-TODO | ||||
return PagedResult( | return PagedResult( | ||||
results=result["branches"].items(), next_page_token=end_cusrsor | results=result["branches"].items(), next_page_token=end_cusrsor | ||||
) | ) | ||||
def _get_after_arg(self): | def _get_after_arg(self): | ||||
""" | # Snapshot branch is using a different cursor; logic to handle that | ||||
Snapshot branch is using a different cursor; logic to handle that | |||||
""" | |||||
# FIXME Cursor must be a hex to be consistent with | # FIXME Cursor must be a hex to be consistent with | ||||
# the base class, hack to make that work | # the base class, hack to make that work | ||||
after = utils.get_decoded_cursor(self.kwargs.get("after", "")) | after = utils.get_decoded_cursor(self.kwargs.get("after", "")) | ||||
return bytes.fromhex(after) | return bytes.fromhex(after) |