Skip to content

add passing options #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions python_graphql_client/graphql_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module containing graphQL client."""

import json
import logging
from typing import Any, Callable
Expand Down Expand Up @@ -60,6 +61,7 @@ async def execute_async(
variables: dict = None,
operation_name: str = None,
headers: dict = {},
**kwargs: Any,
):
"""Make asynchronous request to graphQL server."""
request_body = self.__request_body(
Expand All @@ -69,8 +71,12 @@ async def execute_async(
async with aiohttp.ClientSession() as session:
async with session.post(
self.endpoint,
json=request_body,
headers={**self.headers, **headers},
**{
**self.options,
**kwargs,
"headers": {**self.headers, **headers},
"json": request_body,
},
) as response:
return await response.json()

Expand Down
28 changes: 28 additions & 0 deletions tests/test_graphql_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,34 @@ async def test_execute_basic_query(self, mock_post):
"http://www.test-api.com/", json={"query": query}, headers={}
)

@patch("aiohttp.ClientSession.post")
async def test_execute_basic_query_with_aiohttp_parameters(self, mock_post):
"""Sends a graphql POST request to an endpoint."""
mock_post.return_value.__aenter__.return_value.json = AsyncMock()
client = GraphqlClient(endpoint="http://www.test-api.com/")
query = """
{
tests {
status
}
}
"""

await client.execute_async(
query,
timeout=10,
verify_ssl=False,
headers={"Authorization": "Bearer token"},
)

mock_post.assert_called_once_with(
"http://www.test-api.com/",
json={"query": query},
headers={"Authorization": "Bearer token"},
timeout=10,
verify_ssl=False,
)

@patch("aiohttp.ClientSession.post")
async def test_execute_query_with_variables(self, mock_post):
"""Sends a graphql POST request with variables."""
Expand Down