Skip to content

Commit

Permalink
feat: improved alias resolvers (#1181)
Browse files Browse the repository at this point in the history
- added support for resolving trivial nested fields
  • Loading branch information
dacevedo12 committed Jul 31, 2024
1 parent 85d4652 commit d27e3e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 10 additions & 3 deletions ariadne/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,16 @@ def add_resolver_to_field(


def resolve_parent_field(parent: Any, field_name: str) -> Any:
if isinstance(parent, Mapping):
return parent.get(field_name)
return getattr(parent, field_name, None)
value = parent
for name in field_name.split("."):
if isinstance(value, Mapping):
value = value.get(name)
else:
value = getattr(value, name, None)

if value is None:
break
return value


def resolve_to(attr_name: str) -> Resolver:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_default_resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ def callable_resolver(*_):
assert alias_resolver(obj, None)


def test_alias_resolver_supports_nested_name():
parent_mapping = {"nested": {"hello": "world"}}
parent_object = Mock(nested=Mock(hello="world"))

alias_resolver = resolve_to("nested.hello")
assert alias_resolver(parent_mapping, None) == "world"
assert alias_resolver(parent_object, None) == "world"


def test_alias_resolver_passess_field_args_to_callable_return_value():
def callable_resolver(*_, test):
return test
Expand Down

0 comments on commit d27e3e4

Please sign in to comment.