From 94b872901a2da9bdeb47c1ee9230e66e4a7a93c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Pito=C5=84?= Date: Mon, 8 Jan 2024 10:37:01 +0100 Subject: [PATCH] Fix GraphQLTransportWSHandler returning invalid error message for `query` and `mutation` errors. (#1139) --- CHANGELOG.md | 1 + ariadne/asgi/handlers/graphql_transport_ws.py | 13 +++++++--- .../test_websockets_graphql_transport_ws.ambr | 23 ++++++++++++++++++ .../test_websockets_graphql_transport_ws.py | 24 +++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 tests/asgi/__snapshots__/test_websockets_graphql_transport_ws.ambr diff --git a/CHANGELOG.md b/CHANGELOG.md index c900f6404..be5462d82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/ariadne/asgi/handlers/graphql_transport_ws.py b/ariadne/asgi/handlers/graphql_transport_ws.py index 26c48c9a9..60ee82b9d 100644 --- a/ariadne/asgi/handlers/graphql_transport_ws.py +++ b/ariadne/asgi/handlers/graphql_transport_ws.py @@ -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: diff --git a/tests/asgi/__snapshots__/test_websockets_graphql_transport_ws.ambr b/tests/asgi/__snapshots__/test_websockets_graphql_transport_ws.ambr new file mode 100644 index 000000000..545d9646f --- /dev/null +++ b/tests/asgi/__snapshots__/test_websockets_graphql_transport_ws.ambr @@ -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'.", + }), + ]) +# --- diff --git a/tests/asgi/test_websockets_graphql_transport_ws.py b/tests/asgi/test_websockets_graphql_transport_ws.py index 38489f37e..7aff2501e 100644 --- a/tests/asgi/test_websockets_graphql_transport_ws.py +++ b/tests/asgi/test_websockets_graphql_transport_ws.py @@ -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, ):