diff --git a/.gitignore b/.gitignore index 9396ea0..2074a28 100644 --- a/.gitignore +++ b/.gitignore @@ -1,14 +1,15 @@ *.egg-info/ *.pyc *.sw? *~ .coverage .eggs/ .mypy_cache .tox __pycache__ build/ dist/ version.txt .mypy_cache/ .vscode/ +demo.txt \ No newline at end of file diff --git a/swh/graphql/resolvers/base_model.py b/swh/graphql/resolvers/base_model.py index 7e21780..e2a7d0f 100644 --- a/swh/graphql/resolvers/base_model.py +++ b/swh/graphql/resolvers/base_model.py @@ -1,21 +1,27 @@ from abc import ABC +from collections import namedtuple class BaseModel(ABC): def __init__(self, node): """ - Wrapper class for the swh.model objects - """ - self._node = node + Wrapper class for the model objects - def __getattr__(self, name): - """ SWH storage is not consistent with return types. It is returing object in some cases and dict in some other - Fix to handle that inconsistency + Mocking an object in case of dict + """ + # FIXME, this could fail in nested dicts + if type(node) is dict: + self._node = namedtuple("ModelObj", node.keys())(*node.values()) + else: + self._node = node + + def __getattr__(self, name): + """ + Any property defined in the sub-class + will get precedence over the _node attributes """ - if type(self._node) is dict: - return self._node.get(name) return getattr(self._node, name) diff --git a/swh/graphql/resolvers/resolver_factory.py b/swh/graphql/resolvers/resolver_factory.py index 6f9d6e3..1feab1d 100644 --- a/swh/graphql/resolvers/resolver_factory.py +++ b/swh/graphql/resolvers/resolver_factory.py @@ -1,33 +1,33 @@ -from .branch import SnapshotBranchConnection from .origin import OriginConnection, OriginNode from .snapshot import SnapshotNode, VisitSnapshotNode +from .snapshot_branch import SnapshotBranchConnection from .visit import OriginVisitConnection, OriginVisitNode from .visit_status import VisitStatusConnection def get_node_resolver(resolver_type): # FIXME, replace with a proper factory method mapping = { "origin": OriginNode, "visit": OriginVisitNode, "visit-snapshot": VisitSnapshotNode, "snapshot": SnapshotNode, } # resolver_type = get_mapping_key(info) # FIXME, get full name if resolver_type not in mapping: raise AttributeError(f"Invalid type request {resolver_type}") return mapping[resolver_type] def get_connection_resolver(resolver_type): # FIXME, replace with a proper factory method mapping = { "origins": OriginConnection, "origin-visits": OriginVisitConnection, "visit-status": VisitStatusConnection, "snapshot-branches": SnapshotBranchConnection, } # resolver_type = get_mapping_key(info) # FIXME, get full name if resolver_type not in mapping: raise AttributeError(f"Invalid type request {resolver_type}") return mapping[resolver_type] diff --git a/swh/graphql/resolvers/snapshot.py b/swh/graphql/resolvers/snapshot.py index 1ab15e3..8cc2c3d 100644 --- a/swh/graphql/resolvers/snapshot.py +++ b/swh/graphql/resolvers/snapshot.py @@ -1,51 +1,49 @@ from swh.graphql.backends import archive from swh.graphql.utils import utils from .base_connection import BaseConnection from .base_model import BaseModel from .base_node import BaseNode class SnapshotModel(BaseModel): pass class SnapshotNode(BaseNode): """ For directly accessing a snapshot with swhid """ - _model_class = SnapshotModel def _get_node(self): """ - self.obj is visitstatus here - snapshot swhid is avaialbe in the object """ + # FIXME, use methods from SWH core snapshot_swhid = utils.str_to_swid(self.kwargs.get("SWHId")) return archive.Archive().get_snapshot(snapshot_swhid) class VisitSnapshotNode(BaseNode): # FIXME, maybe it is a good idea to make a # common function for both Node classes (for handling exceptions) """ For accessing a snapshot through the visit type """ _model_class = SnapshotModel def _get_node(self): """ self.obj is visitstatus here snapshot swhid is avaialbe in the object """ snapshot_swhid = self.obj.snapshot return archive.Archive().get_snapshot(snapshot_swhid) class SnapshotConnection(BaseConnection): """ To get all the snapshots under an origin """ _model_class = SnapshotModel diff --git a/swh/graphql/resolvers/branch.py b/swh/graphql/resolvers/snapshot_branch.py similarity index 89% rename from swh/graphql/resolvers/branch.py rename to swh/graphql/resolvers/snapshot_branch.py index 157f614..5b834dd 100644 --- a/swh/graphql/resolvers/branch.py +++ b/swh/graphql/resolvers/snapshot_branch.py @@ -1,28 +1,28 @@ # from swh.graphql.backends import archive from swh.storage.interface import PagedResult from .base_connection import BaseConnection from .base_model import BaseModel -class BranchModel(BaseModel): +class SnapshotBranchModel(BaseModel): pass class SnapshotBranchConnection(BaseConnection): - _model_class = BranchModel + _model_class = SnapshotBranchModel def _get_page_result(self): """ Branches are avaialble in the snapshot object itself Not making a query """ # FIXME Mocking PagedResult to make base_connection work # FIX this in swh-storage # FIX id results = [ {"name": key, "type": value["target_type"], "id": "temp-id"} for (key, value) in self.obj.branches.items() ][:5] # FIXME, this pagination is broken, fix it with swh-storage - return PagedResult(results=results, next_page_token=self.obj["next_branch"]) + return PagedResult(results=results, next_page_token=self.obj.next_branch) diff --git a/swh/graphql/resolvers/visit.py b/swh/graphql/resolvers/visit.py index 399bb6a..948cd2c 100644 --- a/swh/graphql/resolvers/visit.py +++ b/swh/graphql/resolvers/visit.py @@ -1,36 +1,37 @@ from swh.graphql.backends import archive from swh.graphql.utils import utils from .base_connection import BaseConnection from .base_model import BaseModel from .base_node import BaseNode class VisitModel(BaseModel): @property def id(self): + # FIXME return utils.encode(f"{self.origin}-{str(self.visit)}") class OriginVisitNode(BaseNode): _model_class = VisitModel def _get_node(self): # FIXME, make this call async (not for v1) return archive.Archive().get_origin_visit( self.kwargs.get("originUrl"), int(self.kwargs.get("id")) ) class OriginVisitConnection(BaseConnection): _model_class = VisitModel def _get_page_result(self): """ Get the visits for the given origin parent obj (self.obj) is origin here """ # FIXME, make this call async (not for v1) return archive.Archive().get_origin_visits( self.obj.url, after=self._get_after_arg(), first=self._get_first_arg() ) diff --git a/swh/graphql/resolvers/visit_status.py b/swh/graphql/resolvers/visit_status.py index 017ba0c..fce1b27 100644 --- a/swh/graphql/resolvers/visit_status.py +++ b/swh/graphql/resolvers/visit_status.py @@ -1,23 +1,24 @@ from swh.graphql.backends import archive from swh.graphql.utils import utils from .base_connection import BaseConnection from .base_model import BaseModel class VisitStatusModel(BaseModel): @property def id(self): + # FIXME return utils.encode("temp-id") class VisitStatusConnection(BaseConnection): _model_class = VisitStatusModel def _get_page_result(self): return archive.Archive().get_visit_status( self.obj.origin, self.obj.visit, after=self._get_after_arg(), first=self._get_first_arg(), )