diff --git a/docs/transports/aiohttp.rst b/docs/transports/aiohttp.rst index a54809cc..4b792232 100644 --- a/docs/transports/aiohttp.rst +++ b/docs/transports/aiohttp.rst @@ -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 diff --git a/tests/test_aiohttp.py b/tests/test_aiohttp.py index 815b4904..5c135b29 100644 --- a/tests/test_aiohttp.py +++ b/tests/test_aiohttp.py @@ -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 diff --git a/tests/test_requests.py b/tests/test_requests.py index a0f8ca27..2afbd84a 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -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):