Skip to content
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
35 changes: 35 additions & 0 deletions docs/transports/aiohttp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,39 @@ This transport uses the `aiohttp`_ library and allows you to send GraphQL querie

.. literalinclude:: ../code_examples/aiohttp_async.py

Authentication
--------------

There are multiple ways to authenticate depending on the server configuration.

1. Using HTTP Headers

.. code-block:: python

transport = AIOHTTPTransport(
url='https://SERVER_URL:SERVER_PORT/graphql',
headers={'Authorization': 'token'}
)

2. Using HTTP Cookies

You can manually set the cookies which will be sent with each connection:

.. code-block:: python

transport = AIOHTTPTransport(url=url, cookies={"cookie1": "val1"})

Or you can use a cookie jar to save cookies set from the backend and reuse them later.

In some cases, the server will set some connection cookies after a successful login mutation
and you can save these cookies in a cookie jar to reuse them in a following connection
(See `issue 197`_):

.. code-block:: python

jar = aiohttp.CookieJar()
transport = AIOHTTPTransport(url=url, client_session_args={'cookie_jar': jar})


.. _aiohttp: https://docs.aiohttp.org
.. _issue 197: https://github.com/graphql-python/gql/issues/197
33 changes: 33 additions & 0 deletions tests/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,39 @@ async def handler(request):
assert africa["code"] == "AF"


@pytest.mark.asyncio
async def test_aiohttp_cookies(event_loop, aiohttp_server):
from aiohttp import web
from gql.transport.aiohttp import AIOHTTPTransport

async def handler(request):
assert "COOKIE" in request.headers
assert "cookie1=val1" == request.headers["COOKIE"]

return web.Response(text=query1_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, cookies={"cookie1": "val1"})

async with Client(transport=sample_transport,) as session:

query = gql(query1_str)

# Execute query asynchronously
result = await session.execute(query)

continents = result["continents"]

africa = continents[0]

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


@pytest.mark.asyncio
async def test_aiohttp_error_code_500(event_loop, aiohttp_server):
from aiohttp import web
Expand Down
37 changes: 37 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,43 @@ def test_code():
await run_sync_test(event_loop, server, test_code)


@pytest.mark.aiohttp
@pytest.mark.asyncio
async def test_requests_cookies(event_loop, aiohttp_server, run_sync_test):
from aiohttp import web
from gql.transport.requests import RequestsHTTPTransport

async def handler(request):
assert "COOKIE" in request.headers
assert "cookie1=val1" == request.headers["COOKIE"]

return web.Response(text=query1_server_answer, content_type="application/json")

app = web.Application()
app.router.add_route("POST", "/", handler)
server = await aiohttp_server(app)

url = server.make_url("/")

def test_code():
sample_transport = RequestsHTTPTransport(url=url, cookies={"cookie1": "val1"})

with Client(transport=sample_transport,) as session:

query = gql(query1_str)

# Execute query synchronously
result = session.execute(query)

continents = result["continents"]

africa = continents[0]

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

await run_sync_test(event_loop, server, test_code)


@pytest.mark.aiohttp
@pytest.mark.asyncio
async def test_requests_error_code_500(event_loop, aiohttp_server, run_sync_test):
Expand Down