Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return 200 errors #39

Merged
merged 3 commits into from
May 10, 2020
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: 7 additions & 1 deletion graphql_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,13 @@ def format_execution_result(
if execution_result.errors:
fe = [format_error(e) for e in execution_result.errors] # type: ignore
response = {"errors": fe}
status_code = 400

if execution_result.errors and any(
not getattr(e, "path", None) for e in execution_result.errors
):
status_code = 400
else:
response["data"] = execution_result.data
else:
response = {"data": execution_result.data}

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
extras_require={
"all": install_all_requires,
"test": install_all_requires + tests_requires,
"dev": dev_requires,
"dev": install_all_requires + dev_requires,
"flask": install_flask_requires,
},
include_package_data=True,
Expand Down
5 changes: 3 additions & 2 deletions tests/flask/test_graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,15 +371,16 @@ def test_supports_pretty_printing_by_request(app, client):

def test_handles_field_errors_caught_by_graphql(app, client):
response = client.get(url_string(app, query="{thrower}"))
assert response.status_code == 400
assert response.status_code == 200
assert response_json(response) == {
"errors": [
{
"locations": [{"column": 2, "line": 1}],
"path": ["thrower"],
"message": "Throws!",
}
]
],
"data": None,
}


Expand Down
6 changes: 4 additions & 2 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ def test_encode_execution_results_with_error():
"path": ["somePath"],
}
],
"data": None,
}
assert output.status_code == 400
assert output.status_code == 200


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


def test_encode_execution_results_with_batch():
Expand Down
9 changes: 9 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ def test_allows_get_with_operation_name():
{"data": {"test": "Hello World", "shared": "Hello Everyone"}, "errors": None}
]

response = encode_execution_results(results)
assert response.status_code == 200


def test_reports_validation_errors():
results, params = run_http_query(
Expand All @@ -116,6 +119,9 @@ def test_reports_validation_errors():
}
]

response = encode_execution_results(results)
assert response.status_code == 400


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

response = encode_execution_results(results)
assert response.status_code == 200


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