Changeset View
Changeset View
Standalone View
Standalone View
swh/graphql/resolvers/target.py
- This file was added.
from __future__ import annotations | |||||
from typing import TYPE_CHECKING, Union | |||||
from .base_node import BaseNode | |||||
if TYPE_CHECKING: | |||||
from .directory_entry import BaseDirectoryEntryNode # pragma: no cover | |||||
from .release import BaseReleaseNode # pragma: no cover | |||||
from .search import SearchResultNode # pragma: no cover | |||||
from .snapshot_branch import BaseSnapshotBranchNode # pragma: no cover | |||||
class TargetNode(BaseNode): | |||||
""" | |||||
Generic resolver for all the targets. | |||||
Every parent object (self.obj) must have two attributes target_type and target_id. | |||||
target_type is exposed as the type attribute and target_id is exposed as the identifier. | |||||
target_id is also used to retrieve the node object on request | |||||
""" | |||||
# Create a subclass for specific target nodes (say with more fields) | |||||
obj: Union[ | |||||
BaseSnapshotBranchNode, | |||||
BaseReleaseNode, | |||||
BaseDirectoryEntryNode, | |||||
SearchResultNode, | |||||
] | |||||
def _get_node_data(self): | |||||
# node field of a target object is resolved in the top level resolver | |||||
return { | |||||
# field exposed in the schema | |||||
"type": self.obj.target_type, | |||||
# field exposed in the schema, the same value is used to | |||||
# retrieve the node object | |||||
"identifier": self.obj.target_id, | |||||
# field NOT exposed in the schema, used to retrieve the target node | |||||
"target_id": self.obj.target_id, | |||||
} |