From b52a9da673626ea5c0dc92aeef2cd1303e91d143 Mon Sep 17 00:00:00 2001 From: "m.kindritskiy" Date: Fri, 20 Sep 2024 11:15:07 +0300 Subject: [PATCH 1/2] fix aliased record field --- hiku/denormalize/base.py | 5 ++++- tests/test_endpoint_graphql.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/hiku/denormalize/base.py b/hiku/denormalize/base.py index 1bca35d4..3cce98d7 100644 --- a/hiku/denormalize/base.py +++ b/hiku/denormalize/base.py @@ -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)): diff --git a/tests/test_endpoint_graphql.py b/tests/test_endpoint_graphql.py index b507621d..d47414bb 100644 --- a/tests/test_endpoint_graphql.py +++ b/tests/test_endpoint_graphql.py @@ -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") @@ -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"}} From 3224bb336eb2de12aa224d47e9aac7607d670e85 Mon Sep 17 00:00:00 2001 From: "m.kindritskiy" Date: Fri, 20 Sep 2024 11:20:34 +0300 Subject: [PATCH 2/2] update docs 0.8 - describe fix for aliased records --- docs/changelog/changes_08.rst | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/docs/changelog/changes_08.rst b/docs/changelog/changes_08.rst index ddcc2cea..34d1bd51 100644 --- a/docs/changelog/changes_08.rst +++ b/docs/changelog/changes_08.rst @@ -6,7 +6,7 @@ Changes in 0.8 - Introduce ``Schema`` . This is a new high-level api with aim to provide single entrypoint for validation/execution and query/mutations. Previously we had to manage two serapate graphs - one for Query other for Mutation or use `endpoint` - api but ``endpoint`` api is more like an integration-like api for http handlers. + api but ``endpoint`` api is more like an integration-like api for http handlers. More on that in :ref:`Schema docs `. - ``Schema`` returns ``ExecutionResult`` dataclass with ``data``, ``errors`` and ``result`` fields. ``data`` already denormalized but access to `Proxy` object at ``result`` field is retained. - ``Endpoint`` now is much simpler under the hood and it basically delegates execution to schema, only providing support for batching. - Drop custom ``validate`` function for federation since we now have better support for ``_entities`` and ``_service`` fields and their corresponding types. @@ -25,6 +25,21 @@ Changes in 0.8 - Drop ``GraphQLError.errors`` field. Earlier we used to store multiple errors in single ``GraphQLError`` but now its one message - one ``GraphQLError``. - Add ``GraphQLError.message`` field - Dropped support for ``Python 3.7``, which ended support on 2023-06-27 +- Fix: now it is possible to alias record field: + + .. code-block:: python + + Graph([Root([Field("user", TypeRef["User"]))], data_types={"User": Record[{"id": Integer, "name": String}]}) + + .. code-block:: graphql + + query { + user { + id + my_name: name + } + } + Backward-incompatible changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~