Skip to content

Commit 6c13ef6

Browse files
authored
Return 200 errors (#39)
1 parent ea81704 commit 6c13ef6

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

graphql_server/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,13 @@ def format_execution_result(
294294
if execution_result.errors:
295295
fe = [format_error(e) for e in execution_result.errors] # type: ignore
296296
response = {"errors": fe}
297-
status_code = 400
297+
298+
if execution_result.errors and any(
299+
not getattr(e, "path", None) for e in execution_result.errors
300+
):
301+
status_code = 400
302+
else:
303+
response["data"] = execution_result.data
298304
else:
299305
response = {"data": execution_result.data}
300306

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
extras_require={
5151
"all": install_all_requires,
5252
"test": install_all_requires + tests_requires,
53-
"dev": dev_requires,
53+
"dev": install_all_requires + dev_requires,
5454
"flask": install_flask_requires,
5555
},
5656
include_package_data=True,

tests/flask/test_graphqlview.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,15 +371,16 @@ def test_supports_pretty_printing_by_request(app, client):
371371

372372
def test_handles_field_errors_caught_by_graphql(app, client):
373373
response = client.get(url_string(app, query="{thrower}"))
374-
assert response.status_code == 400
374+
assert response.status_code == 200
375375
assert response_json(response) == {
376376
"errors": [
377377
{
378378
"locations": [{"column": 2, "line": 1}],
379379
"path": ["thrower"],
380380
"message": "Throws!",
381381
}
382-
]
382+
],
383+
"data": None,
383384
}
384385

385386

tests/test_helpers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ def test_encode_execution_results_with_error():
105105
"path": ["somePath"],
106106
}
107107
],
108+
"data": None,
108109
}
109-
assert output.status_code == 400
110+
assert output.status_code == 200
110111

111112

112113
def test_encode_execution_results_with_empty_result():
@@ -148,8 +149,9 @@ def format_error(error):
148149
assert isinstance(output.status_code, int)
149150
assert json.loads(output.body) == {
150151
"errors": [{"msg": "Some msg", "loc": "1:2", "pth": "some/path"}],
152+
"data": None,
151153
}
152-
assert output.status_code == 400
154+
assert output.status_code == 200
153155

154156

155157
def test_encode_execution_results_with_batch():

tests/test_query.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ def test_allows_get_with_operation_name():
9292
{"data": {"test": "Hello World", "shared": "Hello Everyone"}, "errors": None}
9393
]
9494

95+
response = encode_execution_results(results)
96+
assert response.status_code == 200
97+
9598

9699
def test_reports_validation_errors():
97100
results, params = run_http_query(
@@ -116,6 +119,9 @@ def test_reports_validation_errors():
116119
}
117120
]
118121

122+
response = encode_execution_results(results)
123+
assert response.status_code == 400
124+
119125

120126
def test_non_dict_params_in_non_batch_query():
121127
with raises(HttpQueryError) as exc_info:
@@ -398,6 +404,9 @@ def test_handles_field_errors_caught_by_graphql():
398404
(None, [{"message": "Throws!", "locations": [(1, 2)], "path": ["thrower"]}])
399405
]
400406

407+
response = encode_execution_results(results)
408+
assert response.status_code == 200
409+
401410

402411
def test_handles_syntax_errors_caught_by_graphql():
403412
results, params = run_http_query(schema, "get", data=dict(query="syntaxerror"))

0 commit comments

Comments
 (0)