-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New transport AIOHTTPWebsocketsTransport (#478)
- Loading branch information
1 parent
ede1350
commit 00b61d5
Showing
15 changed files
with
4,619 additions
and
21 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import asyncio | ||
import logging | ||
|
||
from gql import Client, gql | ||
from gql.transport.aiohttp_websockets import AIOHTTPWebsocketsTransport | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
|
||
async def main(): | ||
|
||
transport = AIOHTTPWebsocketsTransport( | ||
url="wss://countries.trevorblades.com/graphql" | ||
) | ||
|
||
# Using `async with` on the client will start a connection on the transport | ||
# and provide a `session` variable to execute queries on this connection | ||
async with Client( | ||
transport=transport, | ||
) as session: | ||
|
||
# Execute single query | ||
query = gql( | ||
""" | ||
query getContinents { | ||
continents { | ||
code | ||
name | ||
} | ||
} | ||
""" | ||
) | ||
result = await session.execute(query) | ||
print(result) | ||
|
||
# Request subscription | ||
subscription = gql( | ||
""" | ||
subscription { | ||
somethingChanged { | ||
id | ||
} | ||
} | ||
""" | ||
) | ||
async for result in session.subscribe(subscription): | ||
print(result) | ||
|
||
|
||
asyncio.run(main()) |
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,7 @@ | ||
gql.transport.aiohttp_websockets | ||
================================ | ||
|
||
.. currentmodule:: gql.transport.aiohttp_websockets | ||
|
||
.. automodule:: gql.transport.aiohttp_websockets | ||
:member-order: bysource |
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,31 @@ | ||
.. _aiohttp_websockets_transport: | ||
|
||
AIOHTTPWebsocketsTransport | ||
========================== | ||
|
||
The AIOHTTPWebsocketsTransport is an alternative to the :ref:`websockets_transport`, | ||
using the `aiohttp` dependency instead of the `websockets` dependency. | ||
|
||
It also supports both: | ||
|
||
- the `Apollo websockets transport protocol`_. | ||
- the `GraphQL-ws websockets transport protocol`_ | ||
|
||
It will propose both subprotocols to the backend and detect the supported protocol | ||
from the response http headers returned by the backend. | ||
|
||
.. note:: | ||
For some backends (graphql-ws before `version 5.6.1`_ without backwards compatibility), it may be necessary to specify | ||
only one subprotocol to the backend. It can be done by using | ||
:code:`subprotocols=[AIOHTTPWebsocketsTransport.GRAPHQLWS_SUBPROTOCOL]` | ||
or :code:`subprotocols=[AIOHTTPWebsocketsTransport.APOLLO_SUBPROTOCOL]` in the transport arguments. | ||
|
||
This transport allows to do multiple queries, mutations and subscriptions on the same websocket connection. | ||
|
||
Reference: :class:`gql.transport.aiohttp_websockets.AIOHTTPWebsocketsTransport` | ||
|
||
.. literalinclude:: ../code_examples/aiohttp_websockets_async.py | ||
|
||
.. _version 5.6.1: https://github.com/enisdenjo/graphql-ws/releases/tag/v5.6.1 | ||
.. _Apollo websockets transport protocol: https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md | ||
.. _GraphQL-ws websockets transport protocol: https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md |
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
Oops, something went wrong.