Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 6 additions & 2 deletions infrahub_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,9 @@ async def execute_graphql(
response = decode_json(response=resp)

if "errors" in response:
raise GraphQLError(errors=response["errors"], query=query, variables=variables)
if raise_for_error:
raise GraphQLError(errors=response["errors"], query=query, variables=variables)
return response["errors"]

return response["data"]

Expand Down Expand Up @@ -1694,7 +1696,9 @@ def execute_graphql(
response = decode_json(response=resp)

if "errors" in response:
raise GraphQLError(errors=response["errors"], query=query, variables=variables)
if raise_for_error:
raise GraphQLError(errors=response["errors"], query=query, variables=variables)
return response["errors"]

return response["data"]

Expand Down
34 changes: 33 additions & 1 deletion tests/unit/sdk/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pytest_httpx import HTTPXMock

from infrahub_sdk import InfrahubClient, InfrahubClientSync
from infrahub_sdk.exceptions import NodeNotFoundError
from infrahub_sdk.exceptions import GraphQLError, NodeNotFoundError
from infrahub_sdk.node import InfrahubNode, InfrahubNodeSync
from tests.unit.sdk.conftest import BothClients

Expand Down Expand Up @@ -800,3 +800,35 @@ async def test_clone_define_branch(clients: BothClients, client_type: str) -> No
assert clone.default_branch == clone_branch
assert original_branch != clone_branch
assert clone.store._default_branch == clone_branch


@pytest.mark.parametrize("client_type", client_types)
async def test_execute_graphql_error(httpx_mock: HTTPXMock, clients, client_type) -> None:
httpx_mock.add_response(method="POST", json={"errors": ["foo"]}, is_reusable=True)

query = """
query GetTags {
BuiltinTag {
edges {
node {
id
display_label
}
}
}
}
"""

if client_type == "standard":
with pytest.raises(GraphQLError):
await clients.standard.execute_graphql(query=query, raise_for_error=True)

response = await clients.standard.execute_graphql(query=query, raise_for_error=False)
else:
with pytest.raises(GraphQLError):
clients.sync.execute_graphql(query=query, raise_for_error=True)

response = clients.sync.execute_graphql(query=query, raise_for_error=False)

assert response
assert response[0] == "foo"