Changeset View
Standalone View
swh/graphql/resolvers/resolvers.py
Show All 16 Lines | |||||
# - As an attribute/item in the object/dict returned by a backend (eg: Origin.url) | # - As an attribute/item in the object/dict returned by a backend (eg: Origin.url) | ||||
from typing import Optional, Union | from typing import Optional, Union | ||||
from ariadne import ObjectType, UnionType | from ariadne import ObjectType, UnionType | ||||
from graphql.type import GraphQLResolveInfo | from graphql.type import GraphQLResolveInfo | ||||
from swh.graphql import resolvers as rs | from swh.graphql import resolvers as rs | ||||
from swh.graphql.errors import NullableObjectError | |||||
from swh.graphql.utils import utils | from swh.graphql.utils import utils | ||||
from .resolver_factory import get_connection_resolver, get_node_resolver | from .resolver_factory import get_connection_resolver, get_node_resolver | ||||
query: ObjectType = ObjectType("Query") | query: ObjectType = ObjectType("Query") | ||||
origin: ObjectType = ObjectType("Origin") | origin: ObjectType = ObjectType("Origin") | ||||
visit: ObjectType = ObjectType("Visit") | visit: ObjectType = ObjectType("Visit") | ||||
visit_status: ObjectType = ObjectType("VisitStatus") | visit_status: ObjectType = ObjectType("VisitStatus") | ||||
Show All 38 Lines | ) -> rs.visit.OriginVisitNode: | ||||
""" """ | """ """ | ||||
resolver = get_node_resolver("visit") | resolver = get_node_resolver("visit") | ||||
return resolver(obj, info, **kw) | return resolver(obj, info, **kw) | ||||
@visit.field("latestStatus") | @visit.field("latestStatus") | ||||
def latest_visit_status_resolver( | def latest_visit_status_resolver( | ||||
obj: rs.visit.BaseVisitNode, info: GraphQLResolveInfo, **kw | obj: rs.visit.BaseVisitNode, info: GraphQLResolveInfo, **kw | ||||
) -> rs.visit_status.LatestVisitStatusNode: | ) -> Optional[rs.visit_status.LatestVisitStatusNode]: | ||||
""" """ | """ """ | ||||
resolver = get_node_resolver("latest-status") | resolver = get_node_resolver("latest-status") | ||||
try: | |||||
return resolver(obj, info, **kw) | return resolver(obj, info, **kw) | ||||
except NullableObjectError: | |||||
# FIXME, make this pattern generic for all the resolvers | |||||
return None | |||||
anlambert: Why the use of that `NullableObjectError` exception ? The call to resolver should already… | |||||
Done Inline Actionsresolver(obj, info, **kw) will always return an object, not None. This will create issues for objects with mandatory sub fields. jayeshv: resolver(obj, info, **kw) will always return an object, not None. This will create issues for… | |||||
Not Done Inline ActionsAck, I still think that it is simpler if a resolver could return None as some nodes can be nullable in the schema but whatever. anlambert: Ack, I still think that it is simpler if a resolver could return `None` as some nodes can be… | |||||
Done Inline ActionsTo add some more context.
2: No error but return None (eg latestStatus in visit, latestVisit in origin, directory in revision etc) the resolver should fail silently.
I will raise this exception in a few more places in another diff. jayeshv: To add some more context.
There are two types of behavior while getting a node.
1: should show… | |||||
@query.field("snapshot") | @query.field("snapshot") | ||||
def snapshot_resolver( | def snapshot_resolver( | ||||
obj: None, info: GraphQLResolveInfo, **kw | obj: None, info: GraphQLResolveInfo, **kw | ||||
) -> rs.snapshot.SnapshotNode: | ) -> rs.snapshot.SnapshotNode: | ||||
""" """ | """ """ | ||||
resolver = get_node_resolver("snapshot") | resolver = get_node_resolver("snapshot") | ||||
return resolver(obj, info, **kw) | return resolver(obj, info, **kw) | ||||
▲ Show 20 Lines • Show All 161 Lines • ▼ Show 20 Lines | |||||
def origin_snapshots_resolver( | def origin_snapshots_resolver( | ||||
obj: rs.origin.BaseOriginNode, info: GraphQLResolveInfo, **kw | obj: rs.origin.BaseOriginNode, info: GraphQLResolveInfo, **kw | ||||
) -> rs.snapshot.OriginSnapshotConnection: | ) -> rs.snapshot.OriginSnapshotConnection: | ||||
""" """ | """ """ | ||||
resolver = get_connection_resolver("origin-snapshots") | resolver = get_connection_resolver("origin-snapshots") | ||||
return resolver(obj, info, **kw) | return resolver(obj, info, **kw) | ||||
@visit.field("status") | @visit.field("statuses") | ||||
def visitstatus_resolver( | def visitstatus_resolver( | ||||
obj: rs.visit.BaseVisitNode, info: GraphQLResolveInfo, **kw | obj: rs.visit.BaseVisitNode, info: GraphQLResolveInfo, **kw | ||||
) -> rs.visit_status.VisitStatusConnection: | ) -> rs.visit_status.VisitStatusConnection: | ||||
resolver = get_connection_resolver("visit-status") | resolver = get_connection_resolver("visit-status") | ||||
return resolver(obj, info, **kw) | return resolver(obj, info, **kw) | ||||
@snapshot.field("branches") | @snapshot.field("branches") | ||||
▲ Show 20 Lines • Show All 80 Lines • Show Last 20 Lines |
Why the use of that NullableObjectError exception ? The call to resolver should already return None if the visit status search does not return any result, no ?