1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
Subscriptions
=============
Using the :ref:`websockets transport <websockets_transport>`, it is possible to execute GraphQL subscriptions,
either using the sync or async usage.
The async usage is recommended for any non-trivial tasks (it allows efficient concurrent queries and subscriptions).
See :ref:`Async permanent session <async_permanent_session>` and :ref:`Async advanced usage <async_advanced_usage>`
for more advanced examples.
.. note::
The websockets transport can also execute queries or mutations, it is not restricted to subscriptions.
Sync
----
.. code-block:: python
from gql import Client, gql
from gql.transport.websockets import WebsocketsTransport
# Select your transport with a defined url endpoint
transport = WebsocketsTransport(url='wss://your_server/graphql')
# Create a GraphQL client using the defined transport
client = Client(transport=transport)
# Provide a GraphQL subscription query
query = gql('''
subscription yourSubscription {
...
}
''')
# Connect and subscribe to the results using a simple 'for'
for result in client.subscribe(query):
print (result)
Async
-----
.. code-block:: python
import asyncio
from gql import Client, gql
from gql.transport.websockets import WebsocketsTransport
async def main():
# Select your transport with a defined url endpoint
transport = WebsocketsTransport(url='wss://your_server/graphql')
# Create a GraphQL client using the defined transport
client = Client(transport=transport)
# Provide a GraphQL subscription query
query = gql('''
subscription yourSubscription {
...
}
''')
# 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 as session:
# Then get the results using 'async for'
async for result in client.subscribe(query):
print (result)
asyncio.run(main())
|