Skip to content

Commit

Permalink
aiohttp transport - optional variables and operation name (graphql-py…
Browse files Browse the repository at this point in the history
…thon#94)

Fix connecting to servers which do not support setting operationName to "" (graphql-python#92)
  • Loading branch information
leszekhanusz authored Jun 14, 2020
1 parent b4d8132 commit 2849c33
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
9 changes: 6 additions & 3 deletions gql/transport/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
40 changes: 40 additions & 0 deletions tests/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,43 @@ async def handler(request):
africa = continents[0]

assert africa["code"] == "AF"


query2_str = """
query getEurope ($code: ID!) {
continent (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"

0 comments on commit 2849c33

Please sign in to comment.