Skip to content

Commit

Permalink
process_errors() now called by schema (graphql_ws)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristjanvalur committed Aug 25, 2023
1 parent 5195d88 commit 14a83ba
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
5 changes: 0 additions & 5 deletions strawberry/subscriptions/protocols/graphql_ws/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ async def handle_start(self, message: OperationMessage) -> None:
assert result_source.errors
error_payload = result_source.errors[0].formatted
await self.send_message(GQL_ERROR, operation_id, error_payload)
self.schema.process_errors(result_source.errors)
return

self.subscriptions[operation_id] = result_source
Expand Down Expand Up @@ -165,10 +164,6 @@ async def handle_async_results(
if result.extensions:
payload["extensions"] = result.extensions
await self.send_message(GQL_DATA, operation_id, payload)
# log errors after send_message to prevent potential
# slowdown of sending result
if result.errors:
self.schema.process_errors(result.errors)
except asyncio.CancelledError:
# CancelledErrors are expected during task cleanup.
pass
Expand Down
74 changes: 41 additions & 33 deletions tests/websockets/test_graphql_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
from typing import TYPE_CHECKING, AsyncGenerator
from unittest.mock import Mock, patch

import pytest
import pytest_asyncio
Expand All @@ -19,6 +20,7 @@
GQL_START,
GQL_STOP,
)
from tests.views.schema import Schema

if TYPE_CHECKING:
from ..http.clients.aiohttp import HttpClient, WebSocketClient
Expand Down Expand Up @@ -198,25 +200,28 @@ async def test_subscription_cancellation(ws: WebSocketClient):


async def test_subscription_errors(ws: WebSocketClient):
await ws.send_json(
{
"type": GQL_START,
"id": "demo",
"payload": {"query": 'subscription { error(message: "TEST ERR") }'},
}
)
process_errors = Mock()
with patch.object(Schema, "process_errors", process_errors):
await ws.send_json(
{
"type": GQL_START,
"id": "demo",
"payload": {"query": 'subscription { error(message: "TEST ERR") }'},
}
)

response = await ws.receive_json()
assert response["type"] == GQL_DATA
assert response["id"] == "demo"
assert response["payload"]["data"] is None
assert len(response["payload"]["errors"]) == 1
assert response["payload"]["errors"][0]["path"] == ["error"]
assert response["payload"]["errors"][0]["message"] == "TEST ERR"
response = await ws.receive_json()
assert response["type"] == GQL_DATA
assert response["id"] == "demo"
assert response["payload"]["data"] is None
assert len(response["payload"]["errors"]) == 1
assert response["payload"]["errors"][0]["path"] == ["error"]
assert response["payload"]["errors"][0]["message"] == "TEST ERR"
process_errors.assert_called_once()

response = await ws.receive_json()
assert response["type"] == GQL_COMPLETE
assert response["id"] == "demo"
response = await ws.receive_json()
assert response["type"] == GQL_COMPLETE
assert response["id"] == "demo"


async def test_subscription_exceptions(ws: WebSocketClient):
Expand All @@ -241,23 +246,26 @@ async def test_subscription_exceptions(ws: WebSocketClient):


async def test_subscription_field_error(ws: WebSocketClient):
await ws.send_json(
{
"type": GQL_START,
"id": "invalid-field",
"payload": {"query": "subscription { notASubscriptionField }"},
}
)
process_errors = Mock()
with patch.object(Schema, "process_errors", process_errors):
await ws.send_json(
{
"type": GQL_START,
"id": "invalid-field",
"payload": {"query": "subscription { notASubscriptionField }"},
}
)

response = await ws.receive_json()
assert response["type"] == GQL_ERROR
assert response["id"] == "invalid-field"
assert response["payload"] == {
"locations": [{"line": 1, "column": 16}],
"message": (
"Cannot query field 'notASubscriptionField' on type 'Subscription'."
),
}
response = await ws.receive_json()
assert response["type"] == GQL_ERROR
assert response["id"] == "invalid-field"
assert response["payload"] == {
"locations": [{"line": 1, "column": 16}],
"message": (
"Cannot query field 'notASubscriptionField' on type 'Subscription'."
),
}
process_errors.assert_called_once()


async def test_subscription_syntax_error(ws: WebSocketClient):
Expand Down

0 comments on commit 14a83ba

Please sign in to comment.