Skip to content

Commit

Permalink
Fix GraphQLTransportWSHandler returning invalid error message for `qu…
Browse files Browse the repository at this point in the history
…ery` and `mutation` errors. (#1139)
  • Loading branch information
rafalp authored Jan 8, 2024
1 parent f1ca128 commit 94b8729
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
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 @@ async def get_results():
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])
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

0 comments on commit 94b8729

Please sign in to comment.