diff --git a/docs/code_examples/phoenix_channel_async.py b/docs/code_examples/phoenix_channel_async.py new file mode 100644 index 00000000..1fdc2566 --- /dev/null +++ b/docs/code_examples/phoenix_channel_async.py @@ -0,0 +1,30 @@ +import asyncio + +from gql import Client, gql +from gql.transport.phoenix_channel_websockets import PhoenixChannelWebsocketsTransport + + +async def main(): + + transport = PhoenixChannelWebsocketsTransport( + channel_name="YOUR_CHANNEL", url="wss://YOUR_URL/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 yourQuery { + ... + } + """ + ) + + result = await session.execute(query) + print(result) + + +asyncio.run(main()) diff --git a/docs/transports/phoenix.rst b/docs/transports/phoenix.rst index 7fb4a90c..b03c2b93 100644 --- a/docs/transports/phoenix.rst +++ b/docs/transports/phoenix.rst @@ -10,6 +10,8 @@ framework `channels`_. Reference: :class:`gql.transport.phoenix_channel_websockets.PhoenixChannelWebsocketsTransport` +.. literalinclude:: ../code_examples/phoenix_channel_async.py + .. _Absinthe: http://absinthe-graphql.org .. _Phoenix: https://www.phoenixframework.org .. _channels: https://hexdocs.pm/phoenix/Phoenix.Channel.html#content