Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improved alias resolvers (#1181) #1189

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading