diff --git a/swh/graphql/backends/archive.py b/swh/graphql/backends/archive.py index e227822..45b7b4e 100644 --- a/swh/graphql/backends/archive.py +++ b/swh/graphql/backends/archive.py @@ -1,48 +1,48 @@ from swh.storage import get_storage class Archive: def __init__(self): # FIXME, setup config self.storage = get_storage( cls="remote", url="http://moma.internal.softwareheritage.org:5002" ) def get_origin(self, url): return self.storage.origin_get([url])[0] def get_origins(self, after=None, first=50): return self.storage.origin_list(page_token=after, limit=first) def get_origin_visits(self, origin_url, after=None, first=50): return self.storage.origin_visit_get(origin_url, page_token=after, limit=first) def get_origin_visit(self, origin_url, visit_id): return self.storage.origin_visit_get_by(origin_url, visit_id) def get_visit_status(self, origin_url, visit_id, after=None, first=50): return self.storage.origin_visit_status_get( origin_url, visit_id, page_token=after, limit=first ) def get_snapshot(self, snapshot_swhid): return self.storage.snapshot_get(snapshot_swhid) def get_snapshot_branches(self, snapshot, after=None, first=50): return self.storage.snapshot_get_branches( snapshot, branches_from=after, branches_count=first ) def get_revision(self, revision_id): return self.storage.revision_get(revision_ids=[revision_id]) def get_release(self, release_id): return self.storage.release_get(releases=[release_id]) - def get_directory(self, directory_id): + def get_directory_entries(self, directory_id): # FIXME, only for tests return self.storage.directory_ls(directory_id) def get_content(self, content_id): # FIXME, only for tests return self.storage.content_find({"sha1_git": content_id}) diff --git a/swh/graphql/resolvers/base_connection.py b/swh/graphql/resolvers/base_connection.py index 7be1e8f..4dd1800 100644 --- a/swh/graphql/resolvers/base_connection.py +++ b/swh/graphql/resolvers/base_connection.py @@ -1,118 +1,116 @@ from abc import ABC, abstractmethod from typing import Any from swh.graphql.utils import utils # from dataclasses import dataclass # @dataclass # class PageInfo: # nex_page_token: str # class Arguments: # """ # dataclass # """ # after # Elements that come after the specified cursor # first # Returns the first n elements class BaseConnection(ABC): """ Base class for all the connection resolvers """ _node_class: Any = None _page_size = 50 # default page size def __init__(self, obj, info, paged_data=None, **kwargs): self.obj = obj self.info = info self.kwargs = kwargs self._paged_data = paged_data def __call__(self, *args, **kw): return self @property def edges(self): return self._get_edges() @property def nodes(self): """ Override if needed return a list of objects If a node class is set, - return a list of its (Node) intances + return a list of its (Node) instances else a list of raw results """ if self._node_class is not None: return [ self._node_class(self.obj, self.info, node_data=result, **self.kwargs) for result in self.get_paged_data().results ] return self.get_paged_data().results @property def pageInfo(self): # To support the schema naming convention # FIXME Replace with a dataclass # return PageInfo(self.page_data.next_page_token) # FIXME, add more details like startCursor return { "hasNextPage": bool(self.get_paged_data().next_page_token), "endCursor": utils.get_encoded_cursor( self.get_paged_data().next_page_token ), } @property def totalCount(self): # 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): """ Cache to avoid multiple calls to the backend (_get_paged_result) return a PagedResult object """ - if self._paged_data is None: # FIXME, make this call async (not for v1) self._paged_data = self._get_paged_result() return self._paged_data @abstractmethod def _get_paged_result(self): """ Override for desired behaviour return a PagedResult object """ # FIXME, make this call async (not for v1) return None def _get_edges(self): # FIXME, make cursor work per item # Cursor can't be None here return [{"cursor": "dummy", "node": node} for node in self.nodes] def _get_after_arg(self): """ Return the decoded next page token override to use a specific token """ return utils.get_decoded_cursor(self.kwargs.get("after")) def _get_first_arg(self): """ page_size is set to 50 by default """ return self.kwargs.get("first", self._page_size) diff --git a/swh/graphql/resolvers/directory.py b/swh/graphql/resolvers/directory.py index 792560d..2e46424 100644 --- a/swh/graphql/resolvers/directory.py +++ b/swh/graphql/resolvers/directory.py @@ -1,42 +1,39 @@ from swh.graphql.backends import archive from swh.graphql.utils import utils from .base_node import BaseNode class BaseDirectoryNode(BaseNode): def _get_directory_by_id(self, directory_id): - return archive.Archive().get_directory(directory_id) + # fetch more metadata like name + return { + "id": directory_id, + } @property def entries(self): - # FIXME, return a paginated list of - # directory or contnet node object - return self._node[0]["name"] - - @property - def name(self): - return b"test-name" - - @property - def id(self): - return b"test-id" + entries = archive.Archive().get_directory_entries(self._node.id) + # FIXME, local pagination, should be moved to swh-storage (backend) + # return Paginated(DirectoryEntryConnection, entries) + return entries class DirectoryNode(BaseDirectoryNode): def _get_node_data(self): """ When a directory is requested directly (not from a connection) with an id """ directory_id = utils.str_to_swid(self.kwargs.get("Sha1")) + # path = "" return self._get_directory_by_id(directory_id) class RevisionDirectoryNode(BaseDirectoryNode): def _get_node_data(self): """ When a directory is requested from a revision """ directory_id = self.kwargs.get("sha1") return self._get_directory_by_id(directory_id) diff --git a/swh/graphql/resolvers/directory_entry.py b/swh/graphql/resolvers/directory_entry.py new file mode 100644 index 0000000..14474de --- /dev/null +++ b/swh/graphql/resolvers/directory_entry.py @@ -0,0 +1,10 @@ +from .base_connection import BaseConnection +from .base_node import BaseNode + + +class DirectoryEntryNode(BaseNode): + pass + + +class DirectoryEntryConnection(BaseConnection): + pass diff --git a/swh/graphql/schema/schema.graphql b/swh/graphql/schema/schema.graphql index 3078d8c..0845167 100644 --- a/swh/graphql/schema/schema.graphql +++ b/swh/graphql/schema/schema.graphql @@ -1,260 +1,301 @@ scalar SWHID scalar Sha1 scalar DateTime scalar DateTimeZone scalar BinaryText interface Node { id: ID! } interface SWHIDNode { id: SWHID! } interface SWHNode { id: Sha1! } type PageInfo { endCursor: String hasNextPage: Boolean! } type OriginConnection { edges: [OriginEdge] nodes: [Origin] pageInfo: PageInfo! totalCount: Int } type OriginEdge { cursor: String! node: Origin } type Origin implements SWHNode { id: Sha1! url: String! visits( first: Int after: String ): VisitConnection! } type VisitConnection { edges: [VisitEdge] nodes: [Visit] pageInfo: PageInfo! totalCount: Int } type VisitEdge { cursor: String! node: Visit } type Visit implements Node { id: ID! date: DateTime! type: String status( first: Int after: String ): VisitStatusConnection # origin: Origin # FIXME, this can be added later } type VisitStatusConnection { edges: [VisitStatusEdge] nodes: [VisitStatus] pageInfo: PageInfo! totalCount: Int } type VisitStatusEdge { cursor: String! node: VisitStatus } type VisitStatus implements Node { id: ID! status: String! date: DateTime! snapshot: Snapshot type: String } # FIXME, add OriginSnapshotConnection type Snapshot implements SWHNode { id: Sha1! branches( first: Int after: String ): BranchConnection # releases( # first: Int # after: String # ): ReleaseConnection # FIXME, add alias type as well } type BranchConnection { edges: [BranchConnectionEdge] nodes: [Branch] pageInfo: PageInfo! totalCount: Int } type BranchConnectionEdge { cursor: String! node: [Branch] } -# FIXME, this could be alias or Directory as well +# FIXME, this can be Content, Directory, Snapshot, or Alias as well union BranchTarget = Revision | Release type Branch implements Node { id: ID! name: BinaryText type: String # FIXME, change to an enum target: BranchTarget } # type RevisionConnection { # } # type RevisionEdge { # } type Person { email: BinaryText name: BinaryText fullname: BinaryText } type Revision implements SWHNode { id: Sha1! message: BinaryText author: Person committer: Person date: DateTimeZone type: String directoryId: Sha1 directory: Directory parentIds: [Sha1] parents: [Revision] } # type ReleaseConnection { # } # type ReleasEdge { # } type Release implements SWHNode { id: Sha1! name: BinaryText message: BinaryText author: Person date: DateTimeZone } +type DirectoryEntryConnection { + edges: [DirectoryEntryEdge] + nodes: [DirectoryEntry] + pageInfo: PageInfo! + totalCount: Int +} + +type DirectoryEntryEdge { + cursor: String! + node: DirectoryEntry +} + +union DirectoryTarget = Directory | Content + +type DirectoryEntry { + name: BinaryText + type: String # FIXME, replace with enum + target: DirectoryTarget +} + type Directory implements SWHNode { id: Sha1! - name: BinaryText - entries: BinaryText # FIXME, change to Union type + entries: DirectoryEntryConnection +} + +type ContentChecksum { + test: String +} + +type ContentType { + test: String +} + +type ContentLanguage { + test: String +} + +type ContentLicense { + test: String } type Content implements SWHIDNode { id: SWHID! + checksum: ContentChecksum + # data: + filetype: ContentType + language: ContentLanguage + length: Int + license: ContentLicense status: String } type Query { """ Get an origin with its url """ # FIXME, find some unique id to help cache # maybe base64 encode the URL origin( url: String! ): Origin """ Get a list of origins matching the given filters Can also be used to search for an origin """ # FIMXE, use Input types to make this cleaner origins( first: Int after: String ): OriginConnection """ Get a visit object with its id and/or origin and visit id """ # FIXME, find some unique id to help cache visit( originUrl: String! id: String! ): Visit """ Get a snapshot with SWHID """ snapshot( Sha1: String! ): Snapshot # """ # Get all the snapshot for the given origin # """ # originSnapshot( # originUrl: String! # first: Int # after: String # ): SnapshotConnection """ Get the revision with the given swhid """ revision( Sha1: String! ): Revision """ Get the release with the given swhid """ release( Sha1: String! ): Release """ Get the directory with the given swhid """ directory( Sha1: String! ): Directory """ Get the content with the given swhid """ content( SWHID: String! ): Content # """ # Search with the given swhid # """ # searchWithSwhid }