Skip to content

Commit

Permalink
improved alias resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
dacevedo12 authored Aug 2, 2024
1 parent 85d4652 commit 7dffde4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
- Replaced hardcoded HTTP statuses with `HTTPStatus` from the `http` stdlib module.
- Added `include_cookies` option to the `ExplorerApollo`.
- Fixed typing on `extract_data_from_request` method.
- Fixed tests websockets after starlette update
- Fixed tests websockets after starlette update.
- Added `share_enabled` param to `ExplorerPlayground` to enable share playground feature.
- Added support for nested attribute resolution in alias resolvers.


## 0.23 (2024-03-18)
Expand Down
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 7dffde4

Please sign in to comment.