From 5b6486d3c1e318e5dfae6dec3270bf4e58d5c513 Mon Sep 17 00:00:00 2001 From: Hanusz Leszek Date: Mon, 25 May 2020 09:07:40 +0200 Subject: [PATCH 1/2] Make variable_values and operationName optional in aiohttp transport --- gql/transport/aiohttp.py | 9 ++++++--- tests/test_aiohttp.py | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/gql/transport/aiohttp.py b/gql/transport/aiohttp.py index fa66e4db..bdd53852 100644 --- a/gql/transport/aiohttp.py +++ b/gql/transport/aiohttp.py @@ -103,12 +103,15 @@ async def execute( """ query_str = print_ast(document) - payload = { + payload: Dict[str, Any] = { "query": query_str, - "variables": variable_values or {}, - "operationName": operation_name or "", } + if variable_values: + payload["variables"] = variable_values + if operation_name: + payload["operationName"] = operation_name + post_args = { "json": payload, } diff --git a/tests/test_aiohttp.py b/tests/test_aiohttp.py index 2eb6624b..8ee58f7c 100644 --- a/tests/test_aiohttp.py +++ b/tests/test_aiohttp.py @@ -222,3 +222,44 @@ async def handler(request): africa = continents[0] assert africa["code"] == "AF" + + +query2_str = """ + query getEurope ($code: ID!) { + continent (code: $code) { + code + name + } + } +""" + +query2_server_answer = '{"data": {"continent": {"name": "Europe"}}}' + + +@pytest.mark.asyncio +async def test_aiohttp_query_variable_values(event_loop, aiohttp_server): + async def handler(request): + return web.Response(text=query2_server_answer, content_type="application/json") + + app = web.Application() + app.router.add_route("POST", "/", handler) + server = await aiohttp_server(app) + + url = server.make_url("/") + + sample_transport = AIOHTTPTransport(url=url, timeout=10) + + async with Client(transport=sample_transport,) as session: + + params = {"code": "EU"} + + query = gql(query2_str) + + # Execute query asynchronously + result = await session.execute( + query, variable_values=params, operation_name="getEurope" + ) + + continent = result["continent"] + + assert continent["name"] == "Europe" From 5bdee017bd0e3e38f067780f9cbecfdf0eaab0b9 Mon Sep 17 00:00:00 2001 From: Hanusz Leszek Date: Mon, 25 May 2020 09:14:33 +0200 Subject: [PATCH 2/2] Fix incorrect query in test --- tests/test_aiohttp.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_aiohttp.py b/tests/test_aiohttp.py index 8ee58f7c..bc6bd219 100644 --- a/tests/test_aiohttp.py +++ b/tests/test_aiohttp.py @@ -227,7 +227,6 @@ async def handler(request): query2_str = """ query getEurope ($code: ID!) { continent (code: $code) { - code name } }