-
Notifications
You must be signed in to change notification settings - Fork 89
add support for graphql-core 3 to graphql_ws.aiohttp #43
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
Open
hoefling
wants to merge
6
commits into
graphql-python:master
Choose a base branch
from
hoefling:graphql-core-3-aiohttp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+193
−76
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c546e3a
add support for graphql-core 3 to graphql_ws.aiohttp
hoefling eb0df1a
remove travis and tox jobs for EOL python versions, add coverage meas…
hoefling 512e0ae
pass pytest args to enable coverage reporting
hoefling 7e43e2b
move pytest-cov to requirements_dev.txt
hoefling 0b2efdc
drop ancient pytest versions
hoefling a798b36
fix badge urls
hoefling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import asyncio | ||
import sys | ||
from typing import Awaitable, Callable | ||
|
||
import pytest | ||
from aiohttp import WSMsgType | ||
from aiohttp.client import ClientWebSocketResponse | ||
from aiohttp.test_utils import TestClient | ||
from aiohttp.web import Application, WebSocketResponse | ||
from graphql import GraphQLSchema, build_schema | ||
from graphql_ws.aiohttp import AiohttpSubscriptionServer | ||
|
||
if sys.version_info >= (3, 8): | ||
from unittest.mock import AsyncMock | ||
else: | ||
from asyncmock import AsyncMock | ||
|
||
|
||
AiohttpClientFactory = Callable[[Application], Awaitable[TestClient]] | ||
|
||
|
||
def schema() -> GraphQLSchema: | ||
spec = """ | ||
type Query { | ||
dummy: String | ||
} | ||
type Subscription { | ||
messages: String | ||
error: String | ||
} | ||
schema { | ||
query: Query | ||
subscription: Subscription | ||
} | ||
""" | ||
|
||
async def messages_subscribe(root, _info): | ||
await asyncio.sleep(0.1) | ||
yield "foo" | ||
await asyncio.sleep(0.1) | ||
yield "bar" | ||
|
||
async def error_subscribe(root, _info): | ||
raise RuntimeError("baz") | ||
|
||
schema = build_schema(spec) | ||
schema.subscription_type.fields["messages"].subscribe = messages_subscribe | ||
schema.subscription_type.fields["messages"].resolve = lambda evt, _info: evt | ||
schema.subscription_type.fields["error"].subscribe = error_subscribe | ||
schema.subscription_type.fields["error"].resolve = lambda evt, _info: evt | ||
return schema | ||
|
||
|
||
@pytest.fixture | ||
def client( | ||
loop: asyncio.AbstractEventLoop, aiohttp_client: AiohttpClientFactory | ||
) -> TestClient: | ||
subscription_server = AiohttpSubscriptionServer(schema()) | ||
|
||
async def subscriptions(request): | ||
conn = WebSocketResponse(protocols=('graphql-ws',)) | ||
await conn.prepare(request) | ||
await subscription_server.handle(conn) | ||
return conn | ||
|
||
app = Application() | ||
app["subscription_server"] = subscription_server | ||
app.router.add_get('/subscriptions', subscriptions) | ||
return loop.run_until_complete(aiohttp_client(app)) | ||
|
||
|
||
@pytest.fixture | ||
async def connection(client: TestClient) -> ClientWebSocketResponse: | ||
conn = await client.ws_connect("/subscriptions") | ||
yield conn | ||
await conn.close() | ||
|
||
|
||
async def test_connection_closed_on_error(connection: ClientWebSocketResponse): | ||
connection._writer.transport.write(b'0' * 500) | ||
response = await connection.receive() | ||
assert response.type == WSMsgType.CLOSE | ||
|
||
|
||
async def test_connection_init(connection: ClientWebSocketResponse): | ||
await connection.send_str('{"type":"connection_init","payload":{}}') | ||
response = await connection.receive() | ||
assert response.type == WSMsgType.TEXT | ||
assert response.data == '{"type": "connection_ack"}' | ||
|
||
|
||
async def test_connection_init_rejected_on_error( | ||
monkeypatch, client: TestClient, connection: ClientWebSocketResponse | ||
): | ||
# raise exception in AiohttpSubscriptionServer.on_connect | ||
monkeypatch.setattr( | ||
client.app["subscription_server"], | ||
"on_connect", | ||
AsyncMock(side_effect=RuntimeError()), | ||
) | ||
await connection.send_str('{"type":"connection_init", "payload": {}}') | ||
response = await connection.receive() | ||
assert response.type == WSMsgType.TEXT | ||
assert response.json()['type'] == 'connection_error' | ||
|
||
|
||
async def test_messages_subscription(connection: ClientWebSocketResponse): | ||
await connection.send_str('{"type":"connection_init","payload":{}}') | ||
await connection.receive() | ||
await connection.send_str( | ||
'{"id":"1","type":"start","payload":{"query":"subscription MySub { messages }"}}' | ||
) | ||
first = await connection.receive_str() | ||
assert ( | ||
first == '{"id": "1", "type": "data", "payload": {"data": {"messages": "foo"}}}' | ||
) | ||
second = await connection.receive_str() | ||
assert ( | ||
second | ||
== '{"id": "1", "type": "data", "payload": {"data": {"messages": "bar"}}}' | ||
) | ||
resolve_message = await connection.receive_str() | ||
assert resolve_message == '{"id": "1", "type": "complete"}' | ||
|
||
|
||
async def test_subscription_resolve_error(connection: ClientWebSocketResponse): | ||
await connection.send_str('{"type":"connection_init","payload":{}}') | ||
await connection.receive() | ||
await connection.send_str( | ||
'{"id":"2","type":"start","payload":{"query":"subscription MySub { error }"}}' | ||
) | ||
error = await connection.receive_json() | ||
assert error["payload"]["errors"][0]["message"] == "baz" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the
connection_context.get_operation(op_id).dispose()
seems to depend on the oldrxpy
-dependency that has been removed fromgraphql-core
and is effectively removed in this branch by removingsetup_observable_extension
.this causes an unhandled exception on
unsubscribe