-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass extra request options to aiohttp (#36)
- Loading branch information
1 parent
47651d9
commit 87481af
Showing
6 changed files
with
95 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Advanced usage | ||
|
||
## Passing request options to `aiohttp` | ||
|
||
While you can set various default options on your `aiohttp.ClientSession` instance, | ||
there's sometimes the need to pass extra options to the underlying request made by `aiohttp`. | ||
|
||
For this purpose, any additional keyword argument passed to `GraphQLClient.execute` will be passed to `aiohttp.ClientSession.request`. | ||
|
||
```python | ||
import aiohttp | ||
from aiogqlc import GraphQLClient | ||
|
||
async def foo(): | ||
async with aiohttp.ClientSession() as session: | ||
client = GraphQLClient("https://example.com/graphql/", session=session) | ||
|
||
response = await client.execute( | ||
document="query { someField }", | ||
headers={"Authorization": "Bearer SomeToken"}, | ||
timeout=aiohttp.ClientTimeout(total=10), | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from aiogqlc import GraphQLClient | ||
from tests.app import create_app | ||
|
||
|
||
async def test_execute_extra_kwargs_are_passed_to_aiohttp(graphql_session): | ||
query = """ | ||
query { | ||
authorizationHeader | ||
} | ||
""" | ||
|
||
client = GraphQLClient(endpoint="/graphql", session=graphql_session) | ||
response = await client.execute(query, headers={"Authorization": "Bearer Token123"}) | ||
|
||
assert await response.json() == {"data": {"authorizationHeader": "Bearer Token123"}} | ||
|
||
|
||
async def test_default_headers_can_be_overridden(aiohttp_client): | ||
app = create_app() | ||
graphql_session = await aiohttp_client( | ||
app, headers={"Authorization": "Bearer DefaultToken"} | ||
) | ||
|
||
query = """ | ||
query { | ||
authorizationHeader | ||
} | ||
""" | ||
|
||
client = GraphQLClient(endpoint="/graphql", session=graphql_session) | ||
response = await client.execute( | ||
query, headers={"Authorization": "Bearer SpecialToken"} | ||
) | ||
|
||
result = await response.json() | ||
assert result["data"]["authorizationHeader"] != "Bearer DefaultToken" | ||
assert result["data"]["authorizationHeader"] == "Bearer SpecialToken" |