Skip to content

Commit 5a9c0cc

Browse files
authored
DOC add phoenix channel transport example (#354)
1 parent 5719d8f commit 5a9c0cc

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import asyncio
2+
3+
from gql import Client, gql
4+
from gql.transport.phoenix_channel_websockets import PhoenixChannelWebsocketsTransport
5+
6+
7+
async def main():
8+
9+
transport = PhoenixChannelWebsocketsTransport(
10+
channel_name="YOUR_CHANNEL", url="wss://YOUR_URL/graphql"
11+
)
12+
13+
# Using `async with` on the client will start a connection on the transport
14+
# and provide a `session` variable to execute queries on this connection
15+
async with Client(transport=transport) as session:
16+
17+
# Execute single query
18+
query = gql(
19+
"""
20+
query yourQuery {
21+
...
22+
}
23+
"""
24+
)
25+
26+
result = await session.execute(query)
27+
print(result)
28+
29+
30+
asyncio.run(main())

docs/transports/phoenix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ framework `channels`_.
1010
Reference:
1111
:class:`gql.transport.phoenix_channel_websockets.PhoenixChannelWebsocketsTransport`
1212

13+
.. literalinclude:: ../code_examples/phoenix_channel_async.py
14+
1315
.. _Absinthe: http://absinthe-graphql.org
1416
.. _Phoenix: https://www.phoenixframework.org
1517
.. _channels: https://hexdocs.pm/phoenix/Phoenix.Channel.html#content

0 commit comments

Comments
 (0)