Skip to content

Commit ceafcd9

Browse files
authored
Merge pull request #248 from mirumee/exception_only_errors_key
Raise GraphQLClientGraphQLMultiError instead of GraphQLClientInvalidResponseError
2 parents 79ad7fc + b9275a6 commit ceafcd9

File tree

34 files changed

+167
-62
lines changed

34 files changed

+167
-62
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Added `operationName` to payload sent by generated client's methods.
1010
- Fixed base clients to pass `mypy --strict` without installed optional dependencies.
1111
- Renamed `GraphQlClientInvalidResponseError` to `GraphQLClientInvalidResponseError` (breaking change).
12+
- Changed base clients to raise `GraphQLClientGraphQLMultiError` for payloads with `errors` key but no `data` (breaking change).
1213

1314

1415
## 0.10.0 (2023-11-15)

ariadne_codegen/client_generators/dependencies/async_base_client.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ def get_data(self, response: httpx.Response) -> Dict[str, Any]:
127127
except ValueError as exc:
128128
raise GraphQLClientInvalidResponseError(response=response) from exc
129129

130-
if (not isinstance(response_json, dict)) or ("data" not in response_json):
130+
if (not isinstance(response_json, dict)) or (
131+
"data" not in response_json and "errors" not in response_json
132+
):
131133
raise GraphQLClientInvalidResponseError(response=response)
132134

133-
data = response_json["data"]
135+
data = response_json.get("data")
134136
errors = response_json.get("errors")
135137

136138
if errors:

ariadne_codegen/client_generators/dependencies/async_base_client_open_telemetry.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,12 @@ def get_data(self, response: httpx.Response) -> Dict[str, Any]:
166166
except ValueError as exc:
167167
raise GraphQLClientInvalidResponseError(response=response) from exc
168168

169-
if (not isinstance(response_json, dict)) or ("data" not in response_json):
169+
if (not isinstance(response_json, dict)) or (
170+
"data" not in response_json and "errors" not in response_json
171+
):
170172
raise GraphQLClientInvalidResponseError(response=response)
171173

172-
data = response_json["data"]
174+
data = response_json.get("data")
173175
errors = response_json.get("errors")
174176

175177
if errors:

ariadne_codegen/client_generators/dependencies/base_client.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def execute(
6464
**kwargs,
6565
)
6666

67-
def get_data(self, response: httpx.Response) -> dict[str, Any]:
67+
def get_data(self, response: httpx.Response) -> Dict[str, Any]:
6868
if not response.is_success:
6969
raise GraphQLClientHttpError(
7070
status_code=response.status_code, response=response
@@ -75,18 +75,20 @@ def get_data(self, response: httpx.Response) -> dict[str, Any]:
7575
except ValueError as exc:
7676
raise GraphQLClientInvalidResponseError(response=response) from exc
7777

78-
if (not isinstance(response_json, dict)) or ("data" not in response_json):
78+
if (not isinstance(response_json, dict)) or (
79+
"data" not in response_json and "errors" not in response_json
80+
):
7981
raise GraphQLClientInvalidResponseError(response=response)
8082

81-
data = response_json["data"]
83+
data = response_json.get("data")
8284
errors = response_json.get("errors")
8385

8486
if errors:
8587
raise GraphQLClientGraphQLMultiError.from_errors_dicts(
8688
errors_dicts=errors, data=data
8789
)
8890

89-
return cast(dict[str, Any], data)
91+
return cast(Dict[str, Any], data)
9092

9193
def _process_variables(
9294
self, variables: Optional[Dict[str, Any]]

ariadne_codegen/client_generators/dependencies/base_client_open_telemetry.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def execute(
8787
query=query, operation_name=operation_name, variables=variables, **kwargs
8888
)
8989

90-
def get_data(self, response: httpx.Response) -> dict[str, Any]:
90+
def get_data(self, response: httpx.Response) -> Dict[str, Any]:
9191
if not response.is_success:
9292
raise GraphQLClientHttpError(
9393
status_code=response.status_code, response=response
@@ -98,18 +98,20 @@ def get_data(self, response: httpx.Response) -> dict[str, Any]:
9898
except ValueError as exc:
9999
raise GraphQLClientInvalidResponseError(response=response) from exc
100100

101-
if (not isinstance(response_json, dict)) or ("data" not in response_json):
101+
if (not isinstance(response_json, dict)) or (
102+
"data" not in response_json and "errors" not in response_json
103+
):
102104
raise GraphQLClientInvalidResponseError(response=response)
103105

104-
data = response_json["data"]
106+
data = response_json.get("data")
105107
errors = response_json.get("errors")
106108

107109
if errors:
108110
raise GraphQLClientGraphQLMultiError.from_errors_dicts(
109111
errors_dicts=errors, data=data
110112
)
111113

112-
return cast(dict[str, Any], data)
114+
return cast(Dict[str, Any], data)
113115

114116
def _execute(
115117
self,

ariadne_codegen/client_generators/dependencies/exceptions.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ def from_dict(cls, error: Dict[str, Any]) -> "GraphQLClientGraphQLError":
5454

5555

5656
class GraphQLClientGraphQLMultiError(GraphQLClientError):
57-
def __init__(self, errors: List[GraphQLClientGraphQLError], data: Dict[str, Any]):
57+
def __init__(
58+
self,
59+
errors: List[GraphQLClientGraphQLError],
60+
data: Optional[Dict[str, Any]] = None,
61+
):
5862
self.errors = errors
5963
self.data = data
6064

@@ -63,7 +67,7 @@ def __str__(self) -> str:
6367

6468
@classmethod
6569
def from_errors_dicts(
66-
cls, errors_dicts: List[Dict[str, Any]], data: Dict[str, Any]
70+
cls, errors_dicts: List[Dict[str, Any]], data: Optional[Dict[str, Any]] = None
6771
) -> "GraphQLClientGraphQLMultiError":
6872
return cls(
6973
errors=[GraphQLClientGraphQLError.from_dict(e) for e in errors_dicts],

tests/client_generators/dependencies/test_async_base_client.py

+5
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,11 @@ def test_get_data_raises_graphql_client_invalid_response_error(
546546
},
547547
],
548548
},
549+
{
550+
"errors": [
551+
{"message": "Error message"},
552+
],
553+
},
549554
],
550555
)
551556
def test_get_data_raises_graphql_client_graphql_multi_error(mocker, response_content):

tests/client_generators/dependencies/test_async_base_client_open_telemetry.py

+5
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,11 @@ def test_get_data_raises_graphql_client_invalid_response_error(
563563
},
564564
],
565565
},
566+
{
567+
"errors": [
568+
{"message": "Error message"},
569+
],
570+
},
566571
],
567572
)
568573
def test_get_data_raises_graphql_client_graphql_multi_error(mocker, response_content):

tests/client_generators/dependencies/test_base_client.py

+5
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,11 @@ def test_get_data_raises_graphql_client_invalid_response_error(
519519
},
520520
],
521521
},
522+
{
523+
"errors": [
524+
{"message": "Error message"},
525+
],
526+
},
522527
],
523528
)
524529
def test_get_data_raises_graphql_client_graphql_multi_error(mocker, response_content):

tests/client_generators/dependencies/test_base_client_open_telemetry.py

+5
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,11 @@ def test_get_data_raises_graphql_client_invalid_response_error(
532532
},
533533
],
534534
},
535+
{
536+
"errors": [
537+
{"message": "Error message"},
538+
],
539+
},
535540
],
536541
)
537542
def test_get_data_raises_graphql_client_graphql_multi_error(mocker, response_content):

tests/main/clients/custom_config_file/expected_client/async_base_client.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ def get_data(self, response: httpx.Response) -> Dict[str, Any]:
127127
except ValueError as exc:
128128
raise GraphQLClientInvalidResponseError(response=response) from exc
129129

130-
if (not isinstance(response_json, dict)) or ("data" not in response_json):
130+
if (not isinstance(response_json, dict)) or (
131+
"data" not in response_json and "errors" not in response_json
132+
):
131133
raise GraphQLClientInvalidResponseError(response=response)
132134

133-
data = response_json["data"]
135+
data = response_json.get("data")
134136
errors = response_json.get("errors")
135137

136138
if errors:

tests/main/clients/custom_config_file/expected_client/exceptions.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ def from_dict(cls, error: Dict[str, Any]) -> "GraphQLClientGraphQLError":
5454

5555

5656
class GraphQLClientGraphQLMultiError(GraphQLClientError):
57-
def __init__(self, errors: List[GraphQLClientGraphQLError], data: Dict[str, Any]):
57+
def __init__(
58+
self,
59+
errors: List[GraphQLClientGraphQLError],
60+
data: Optional[Dict[str, Any]] = None,
61+
):
5862
self.errors = errors
5963
self.data = data
6064

@@ -63,7 +67,7 @@ def __str__(self) -> str:
6367

6468
@classmethod
6569
def from_errors_dicts(
66-
cls, errors_dicts: List[Dict[str, Any]], data: Dict[str, Any]
70+
cls, errors_dicts: List[Dict[str, Any]], data: Optional[Dict[str, Any]] = None
6771
) -> "GraphQLClientGraphQLMultiError":
6872
return cls(
6973
errors=[GraphQLClientGraphQLError.from_dict(e) for e in errors_dicts],

tests/main/clients/custom_files_names/expected_client/async_base_client.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ def get_data(self, response: httpx.Response) -> Dict[str, Any]:
127127
except ValueError as exc:
128128
raise GraphQLClientInvalidResponseError(response=response) from exc
129129

130-
if (not isinstance(response_json, dict)) or ("data" not in response_json):
130+
if (not isinstance(response_json, dict)) or (
131+
"data" not in response_json and "errors" not in response_json
132+
):
131133
raise GraphQLClientInvalidResponseError(response=response)
132134

133-
data = response_json["data"]
135+
data = response_json.get("data")
134136
errors = response_json.get("errors")
135137

136138
if errors:

tests/main/clients/custom_files_names/expected_client/exceptions.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ def from_dict(cls, error: Dict[str, Any]) -> "GraphQLClientGraphQLError":
5454

5555

5656
class GraphQLClientGraphQLMultiError(GraphQLClientError):
57-
def __init__(self, errors: List[GraphQLClientGraphQLError], data: Dict[str, Any]):
57+
def __init__(
58+
self,
59+
errors: List[GraphQLClientGraphQLError],
60+
data: Optional[Dict[str, Any]] = None,
61+
):
5862
self.errors = errors
5963
self.data = data
6064

@@ -63,7 +67,7 @@ def __str__(self) -> str:
6367

6468
@classmethod
6569
def from_errors_dicts(
66-
cls, errors_dicts: List[Dict[str, Any]], data: Dict[str, Any]
70+
cls, errors_dicts: List[Dict[str, Any]], data: Optional[Dict[str, Any]] = None
6771
) -> "GraphQLClientGraphQLMultiError":
6872
return cls(
6973
errors=[GraphQLClientGraphQLError.from_dict(e) for e in errors_dicts],

tests/main/clients/custom_scalars/expected_client/async_base_client.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ def get_data(self, response: httpx.Response) -> Dict[str, Any]:
127127
except ValueError as exc:
128128
raise GraphQLClientInvalidResponseError(response=response) from exc
129129

130-
if (not isinstance(response_json, dict)) or ("data" not in response_json):
130+
if (not isinstance(response_json, dict)) or (
131+
"data" not in response_json and "errors" not in response_json
132+
):
131133
raise GraphQLClientInvalidResponseError(response=response)
132134

133-
data = response_json["data"]
135+
data = response_json.get("data")
134136
errors = response_json.get("errors")
135137

136138
if errors:

tests/main/clients/custom_scalars/expected_client/exceptions.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ def from_dict(cls, error: Dict[str, Any]) -> "GraphQLClientGraphQLError":
5454

5555

5656
class GraphQLClientGraphQLMultiError(GraphQLClientError):
57-
def __init__(self, errors: List[GraphQLClientGraphQLError], data: Dict[str, Any]):
57+
def __init__(
58+
self,
59+
errors: List[GraphQLClientGraphQLError],
60+
data: Optional[Dict[str, Any]] = None,
61+
):
5862
self.errors = errors
5963
self.data = data
6064

@@ -63,7 +67,7 @@ def __str__(self) -> str:
6367

6468
@classmethod
6569
def from_errors_dicts(
66-
cls, errors_dicts: List[Dict[str, Any]], data: Dict[str, Any]
70+
cls, errors_dicts: List[Dict[str, Any]], data: Optional[Dict[str, Any]] = None
6771
) -> "GraphQLClientGraphQLMultiError":
6872
return cls(
6973
errors=[GraphQLClientGraphQLError.from_dict(e) for e in errors_dicts],

tests/main/clients/example/expected_client/async_base_client.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ def get_data(self, response: httpx.Response) -> Dict[str, Any]:
127127
except ValueError as exc:
128128
raise GraphQLClientInvalidResponseError(response=response) from exc
129129

130-
if (not isinstance(response_json, dict)) or ("data" not in response_json):
130+
if (not isinstance(response_json, dict)) or (
131+
"data" not in response_json and "errors" not in response_json
132+
):
131133
raise GraphQLClientInvalidResponseError(response=response)
132134

133-
data = response_json["data"]
135+
data = response_json.get("data")
134136
errors = response_json.get("errors")
135137

136138
if errors:

tests/main/clients/example/expected_client/exceptions.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ def from_dict(cls, error: Dict[str, Any]) -> "GraphQLClientGraphQLError":
5454

5555

5656
class GraphQLClientGraphQLMultiError(GraphQLClientError):
57-
def __init__(self, errors: List[GraphQLClientGraphQLError], data: Dict[str, Any]):
57+
def __init__(
58+
self,
59+
errors: List[GraphQLClientGraphQLError],
60+
data: Optional[Dict[str, Any]] = None,
61+
):
5862
self.errors = errors
5963
self.data = data
6064

@@ -63,7 +67,7 @@ def __str__(self) -> str:
6367

6468
@classmethod
6569
def from_errors_dicts(
66-
cls, errors_dicts: List[Dict[str, Any]], data: Dict[str, Any]
70+
cls, errors_dicts: List[Dict[str, Any]], data: Optional[Dict[str, Any]] = None
6771
) -> "GraphQLClientGraphQLMultiError":
6872
return cls(
6973
errors=[GraphQLClientGraphQLError.from_dict(e) for e in errors_dicts],

tests/main/clients/extended_models/expected_client/async_base_client.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ def get_data(self, response: httpx.Response) -> Dict[str, Any]:
127127
except ValueError as exc:
128128
raise GraphQLClientInvalidResponseError(response=response) from exc
129129

130-
if (not isinstance(response_json, dict)) or ("data" not in response_json):
130+
if (not isinstance(response_json, dict)) or (
131+
"data" not in response_json and "errors" not in response_json
132+
):
131133
raise GraphQLClientInvalidResponseError(response=response)
132134

133-
data = response_json["data"]
135+
data = response_json.get("data")
134136
errors = response_json.get("errors")
135137

136138
if errors:

tests/main/clients/extended_models/expected_client/exceptions.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ def from_dict(cls, error: Dict[str, Any]) -> "GraphQLClientGraphQLError":
5454

5555

5656
class GraphQLClientGraphQLMultiError(GraphQLClientError):
57-
def __init__(self, errors: List[GraphQLClientGraphQLError], data: Dict[str, Any]):
57+
def __init__(
58+
self,
59+
errors: List[GraphQLClientGraphQLError],
60+
data: Optional[Dict[str, Any]] = None,
61+
):
5862
self.errors = errors
5963
self.data = data
6064

@@ -63,7 +67,7 @@ def __str__(self) -> str:
6367

6468
@classmethod
6569
def from_errors_dicts(
66-
cls, errors_dicts: List[Dict[str, Any]], data: Dict[str, Any]
70+
cls, errors_dicts: List[Dict[str, Any]], data: Optional[Dict[str, Any]] = None
6771
) -> "GraphQLClientGraphQLMultiError":
6872
return cls(
6973
errors=[GraphQLClientGraphQLError.from_dict(e) for e in errors_dicts],

tests/main/clients/fragments_on_abstract_types/expected_client/async_base_client.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ def get_data(self, response: httpx.Response) -> Dict[str, Any]:
127127
except ValueError as exc:
128128
raise GraphQLClientInvalidResponseError(response=response) from exc
129129

130-
if (not isinstance(response_json, dict)) or ("data" not in response_json):
130+
if (not isinstance(response_json, dict)) or (
131+
"data" not in response_json and "errors" not in response_json
132+
):
131133
raise GraphQLClientInvalidResponseError(response=response)
132134

133-
data = response_json["data"]
135+
data = response_json.get("data")
134136
errors = response_json.get("errors")
135137

136138
if errors:

tests/main/clients/fragments_on_abstract_types/expected_client/exceptions.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ def from_dict(cls, error: Dict[str, Any]) -> "GraphQLClientGraphQLError":
5454

5555

5656
class GraphQLClientGraphQLMultiError(GraphQLClientError):
57-
def __init__(self, errors: List[GraphQLClientGraphQLError], data: Dict[str, Any]):
57+
def __init__(
58+
self,
59+
errors: List[GraphQLClientGraphQLError],
60+
data: Optional[Dict[str, Any]] = None,
61+
):
5862
self.errors = errors
5963
self.data = data
6064

@@ -63,7 +67,7 @@ def __str__(self) -> str:
6367

6468
@classmethod
6569
def from_errors_dicts(
66-
cls, errors_dicts: List[Dict[str, Any]], data: Dict[str, Any]
70+
cls, errors_dicts: List[Dict[str, Any]], data: Optional[Dict[str, Any]] = None
6771
) -> "GraphQLClientGraphQLMultiError":
6872
return cls(
6973
errors=[GraphQLClientGraphQLError.from_dict(e) for e in errors_dicts],

0 commit comments

Comments
 (0)