Skip to content

Commit

Permalink
fix aliased record field
Browse files Browse the repository at this point in the history
  • Loading branch information
m.kindritskiy committed Sep 20, 2024
1 parent fa2d110 commit b52a9da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion hiku/denormalize/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ def visit_field(self, obj: Field) -> None:
else:
# Record type itself does not have custom serialization
# TODO: support Scalar/Enum types in Record
self._res[-1][obj.result_key] = self._data[-1][obj.result_key]

# if record field is aliased, use index_key as a result-key
result_key = obj.result_key if obj.alias is None else obj.index_key
self._res[-1][obj.result_key] = self._data[-1][result_key]

def visit_link(self, obj: Link) -> None:
if isinstance(self._type[-1], (Union, Interface)):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_endpoint_graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from hiku.graph import Field, Graph, Root
from hiku.schema import Schema
from hiku.types import String
from hiku.types import Record
from hiku.types import TypeRef


@pytest.fixture(name="sync_graph")
Expand Down Expand Up @@ -123,3 +125,18 @@ def get_custom_context(ec):
{"data": {"answer": "52"}},
{"data": {"__typename": "Query"}},
]


@pytest.mark.asyncio
async def test_denormalize_aliased_record_field():
async def answer(fields):
return [{"answer": "42"} for _ in fields]

dt = {"Answer": Record[{"answer": String}]}
graph = Graph(
[Root([Field("question", TypeRef["Answer"], answer)])], data_types=dt
)

schema = Schema(AsyncIOExecutor(), graph)
result = await schema.execute("{ question { my_answer: answer } }")
assert result.data == {"question": {"my_answer": "42"}}

0 comments on commit b52a9da

Please sign in to comment.