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

Fix GraphQLTransportWSHandler returning invalid error message for query and mutation errors. #1139

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Deprecated `EnumType.bind_to_default_values` method. It will be removed in a future release.
- Added `repair_schema_default_enum_values` to public API.
- Removed `validate_schema_enum_values` and introduced `validate_schema_default_enum_values` in its place. This is a breaking change.
- Fixed an invalid error message returned by the `GraphQLTransportWSHandler` for `query` and `mutation` operations.


## 0.21 (2023-11-08)
Expand Down
13 changes: 10 additions & 3 deletions ariadne/asgi/handlers/graphql_transport_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,15 +367,22 @@
yield result

# if success then AsyncGenerator is expected, for error it will be List
results_producer = get_results() if success else [result]
if success:
results_producer = get_results()
else:
results_producer = result["errors"]

if not success:
results_producer = cast(List[dict], results_producer)
if not isinstance(results_producer, list):
error_payload = cast(List[dict], [results_producer])

Check warning on line 377 in ariadne/asgi/handlers/graphql_transport_ws.py

View check run for this annotation

Codecov / codecov/patch

ariadne/asgi/handlers/graphql_transport_ws.py#L377

Added line #L377 was not covered by tests
else:
error_payload = results_producer

await websocket.send_json(
{
"type": GraphQLTransportWSHandler.GQL_ERROR,
"id": operation_id,
"payload": [results_producer[0]],
"payload": error_payload,
}
)
else:
Expand Down
23 changes: 23 additions & 0 deletions tests/asgi/__snapshots__/test_websockets_graphql_transport_ws.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# serializer version: 1
# name: test_invalid_query_error_is_handled_using_websocket_connection_graphql_transport_ws
list([
dict({
'locations': list([
dict({
'column': 17,
'line': 1,
}),
]),
'message': "Cannot query field 'error' on type 'Query'.",
}),
dict({
'locations': list([
dict({
'column': 23,
'line': 1,
}),
]),
'message': "Cannot query field 'other' on type 'Query'.",
}),
])
# ---
24 changes: 24 additions & 0 deletions tests/asgi/test_websockets_graphql_transport_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,30 @@ def test_mutation_can_be_executed_using_websocket_connection_graphql_transport_w
assert response["id"] == "test3"


def test_invalid_query_error_is_handled_using_websocket_connection_graphql_transport_ws(
client_graphql_transport_ws, snapshot
):
with client_graphql_transport_ws.websocket_connect(
"/", ["graphql-transport-ws"]
) as ws:
ws.send_json({"type": GraphQLTransportWSHandler.GQL_CONNECTION_INIT})
response = ws.receive_json()
assert response["type"] == GraphQLTransportWSHandler.GQL_CONNECTION_ACK
ws.send_json(
{
"type": GraphQLTransportWSHandler.GQL_SUBSCRIBE,
"id": "test2",
"payload": {
"operationName": "Invalid",
"query": "query Invalid { error other }",
},
}
)
response = ws.receive_json()
assert response["type"] == GraphQLTransportWSHandler.GQL_ERROR
assert snapshot == response["payload"]


def test_custom_query_parser_is_used_for_subscription_over_websocket_transport_ws(
schema,
):
Expand Down