Skip to content

Commit fed3b7b

Browse files
authored
Merge pull request #2000 from kili-technology/feature/lab-4118-aau-when-i-use-the-sdk-and-it-calls-a-graphql-deprecated
fix(LAB-4118): add a warning log to inform the user he uses a deprecated field
2 parents 8a9168f + 57af826 commit fed3b7b

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/kili/core/graphql/graphql_client.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import threading
66
import time
7+
import warnings
78
from pathlib import Path
89
from typing import Any, Optional, Union
910
from urllib.parse import urlparse
@@ -325,6 +326,7 @@ def _raw_execute(
325326
res = self._gql_client.execute(
326327
document=document,
327328
variable_values=variables,
329+
get_execution_result=True,
328330
extra_args={
329331
"headers": {
330332
**(self._gql_transport.headers or {}),
@@ -333,9 +335,24 @@ def _raw_execute(
333335
},
334336
**kwargs,
335337
)
338+
339+
extensions = getattr(res, "extensions", None)
340+
if isinstance(extensions, dict):
341+
for item in extensions.get("deprecations") or []:
342+
warnings.warn(
343+
f"[Kili SDK] Deprecated GraphQL field used: "
344+
f"{item.get('path')}{item.get('reason')}"
345+
)
346+
336347
transport = self._gql_client.transport
337348
if transport:
338349
headers = transport.response_headers # pyright: ignore[reportAttributeAccessIssue]
339350
returned_complexity = int(headers.get("x-complexity", 0)) if headers else 0
340351
self.complexity_consumed += returned_complexity
341-
return res
352+
353+
if res.data is None:
354+
raise kili.exceptions.GraphQLError(
355+
error="GraphQL response contains no data",
356+
)
357+
358+
return res.data

tests/integration/entrypoints/client/test_client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88
import pytest_mock
99
from filelock import FileLock
10+
from graphql import ExecutionResult
1011

1112
from kili.adapters.kili_api_gateway.kili_api_gateway import KiliAPIGateway
1213
from kili.client import Kili
@@ -147,7 +148,10 @@ def test_complexity_increases_with_calls(
147148
mocker: pytest_mock.MockerFixture,
148149
):
149150
graphql_mock = mocker.MagicMock()
150-
graphql_mock.execute.return_value = {"data": 1}
151+
graphql_mock.execute.return_value = ExecutionResult(
152+
data={"data": 1},
153+
extensions=None,
154+
)
151155
graphql_mock.transport.response_headers = {"x-complexity": "125"}
152156

153157
# Given
@@ -172,7 +176,10 @@ def test_complexity_compatibility_with_legacy(
172176
mocker: pytest_mock.MockerFixture,
173177
):
174178
graphql_mock = mocker.MagicMock()
175-
graphql_mock.execute.return_value = {"data": 1}
179+
graphql_mock.execute.return_value = ExecutionResult(
180+
data={"data": 1},
181+
extensions=None,
182+
)
176183
graphql_mock.transport.response_headers = {}
177184

178185
# Given

tests/unit/test_graphql_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest_mock
66
from gql import Client
77
from gql.transport import exceptions
8+
from graphql import ExecutionResult
89
from pyrate_limiter import Duration, Rate
910
from pyrate_limiter.limiter import Limiter
1011

@@ -88,9 +89,11 @@ def mock_execute(*args, **kwargs):
8889
nonlocal before_last_call_timestamp
8990
before_last_call_timestamp = last_call_timestamp
9091
last_call_timestamp = time()
92+
return ExecutionResult({"data": 1}, extensions=None)
9193

9294
client._gql_client = mocker.MagicMock()
9395
client._gql_client.execute.side_effect = mock_execute
96+
client._gql_client.transport.response_headers = {}
9497

9598
# first calls should not be rate limited
9699
for _ in range(MAX_CALLS_PER_MINUTE):
@@ -162,7 +165,7 @@ def mocked_backend_response(*args, **kwargs):
162165
nonlocal nb_times_called
163166
nb_times_called += 1
164167
if nb_times_called > 2:
165-
return {"data": "all good"}
168+
return ExecutionResult({"data": "all good"}, extensions=None)
166169
raise exceptions.TransportQueryError(
167170
msg=(
168171
"[unexpectedRetrieving] Unexpected error when retrieving runtime information."

0 commit comments

Comments
 (0)