Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions changelog/493.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate the use of `raise_for_error=False` when using `execute_graphql` methods.
27 changes: 21 additions & 6 deletions infrahub_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import copy
import logging
import time
import warnings
from collections.abc import Coroutine, MutableMapping
from functools import wraps
from time import sleep
Expand Down Expand Up @@ -881,7 +882,7 @@ async def execute_graphql(
branch_name: str | None = None,
at: str | Timestamp | None = None,
timeout: int | None = None,
raise_for_error: bool = True,
raise_for_error: bool | None = None,
tracker: str | None = None,
) -> dict:
"""Execute a GraphQL query (or mutation).
Expand All @@ -893,13 +894,20 @@ async def execute_graphql(
branch_name (str, optional): Name of the branch on which the query will be executed. Defaults to None.
at (str, optional): Time when the query should be executed. Defaults to None.
timeout (int, optional): Timeout in second for the query. Defaults to None.
raise_for_error (bool, optional): Flag to indicate that we need to raise an exception if the response has some errors. Defaults to True.
raise_for_error (bool, optional): Deprecated, flag to indicate that we need to raise an exception if the response has some errors.
Defaults to True.
Raises:
GraphQLError: _description_

Returns:
_type_: _description_
"""
if raise_for_error is not None:
warnings.warn(
"Using `raise_for_error` is deprecated, use `try/except` to handle errors.",
DeprecationWarning,
stacklevel=1,
)

branch_name = branch_name or self.default_branch
url = self._graphql_url(branch_name=branch_name, at=at)
Expand All @@ -922,7 +930,7 @@ async def execute_graphql(
try:
resp = await self._post(url=url, payload=payload, headers=headers, timeout=timeout)

if raise_for_error:
if raise_for_error in (None, True):
resp.raise_for_status()

retry = False
Expand Down Expand Up @@ -1625,7 +1633,7 @@ def execute_graphql(
branch_name: str | None = None,
at: str | Timestamp | None = None,
timeout: int | None = None,
raise_for_error: bool = True,
raise_for_error: bool | None = None,
tracker: str | None = None,
) -> dict:
"""Execute a GraphQL query (or mutation).
Expand All @@ -1637,13 +1645,20 @@ def execute_graphql(
branch_name (str, optional): Name of the branch on which the query will be executed. Defaults to None.
at (str, optional): Time when the query should be executed. Defaults to None.
timeout (int, optional): Timeout in second for the query. Defaults to None.
raise_for_error (bool, optional): Flag to indicate that we need to raise an exception if the response has some errors. Defaults to True.
raise_for_error (bool, optional): Deprecated, flag to indicate that we need to raise an exception if the response has some errors.
Defaults to True.
Raises:
GraphQLError: When an error occurs during the execution of the GraphQL query or mutation.

Returns:
dict: The result of the GraphQL query or mutation.
"""
if raise_for_error is not None:
warnings.warn(
"Using `raise_for_error` is deprecated, use `try/except` to handle errors.",
DeprecationWarning,
stacklevel=1,
)

branch_name = branch_name or self.default_branch
url = self._graphql_url(branch_name=branch_name, at=at)
Expand All @@ -1666,7 +1681,7 @@ def execute_graphql(
try:
resp = self._post(url=url, payload=payload, headers=headers, timeout=timeout)

if raise_for_error:
if raise_for_error in (None, True):
resp.raise_for_status()

retry = False
Expand Down