Skip to content

Commit 474fdee

Browse files
committed
Async usage more prominent in the docs
1 parent 07bda9a commit 474fdee

File tree

10 files changed

+102
-64
lines changed

10 files changed

+102
-64
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ The complete documentation for GQL can be found at
3737
* AWS AppSync realtime protocol (experimental)
3838
* Possibility to [validate the queries locally](https://gql.readthedocs.io/en/latest/usage/validation.html) using a GraphQL schema provided locally or fetched from the backend using an instrospection query
3939
* Supports GraphQL queries, mutations and [subscriptions](https://gql.readthedocs.io/en/latest/usage/subscriptions.html)
40-
* Supports [sync or async usage](https://gql.readthedocs.io/en/latest/async/index.html), [allowing concurrent requests](https://gql.readthedocs.io/en/latest/advanced/async_advanced_usage.html#async-advanced-usage)
40+
* Supports [sync](https://gql.readthedocs.io/en/latest/usage/sync_usage.html) or [async](https://gql.readthedocs.io/en/latest/usage/async_usage.html) usage, [allowing concurrent requests](https://gql.readthedocs.io/en/latest/advanced/async_advanced_usage.html#async-advanced-usage)
4141
* Supports [File uploads](https://gql.readthedocs.io/en/latest/usage/file_upload.html)
4242
* Supports [Custom scalars / Enums](https://gql.readthedocs.io/en/latest/usage/custom_scalars_and_enums.html)
4343
* Supports [Batching requests](https://gql.readthedocs.io/en/latest/advanced/batching_requests.html)

docs/async/async_intro.rst

Lines changed: 0 additions & 18 deletions
This file was deleted.

docs/async/index.rst

Lines changed: 0 additions & 10 deletions
This file was deleted.

docs/code_examples/aiohttp_async.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,29 @@
66

77
async def main():
88

9+
# Select your transport with a defined url endpoint
910
transport = AIOHTTPTransport(url="https://countries.trevorblades.com/graphql")
1011

12+
# Create a GraphQL client using the defined transport
13+
client = Client(transport=transport)
14+
15+
# Provide a GraphQL query
16+
query = gql(
17+
"""
18+
query getContinents {
19+
continents {
20+
code
21+
name
22+
}
23+
}
24+
"""
25+
)
26+
1127
# Using `async with` on the client will start a connection on the transport
1228
# and provide a `session` variable to execute queries on this connection
13-
async with Client(
14-
transport=transport,
15-
fetch_schema_from_transport=True,
16-
) as session:
17-
18-
# Execute single query
19-
query = gql(
20-
"""
21-
query getContinents {
22-
continents {
23-
code
24-
name
25-
}
26-
}
27-
"""
28-
)
29+
async with client as session:
2930

31+
# Execute the query
3032
result = await session.execute(query)
3133
print(result)
3234

docs/code_examples/aiohttp_sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
transport = AIOHTTPTransport(url="https://countries.trevorblades.com/")
66

77
# Create a GraphQL client using the defined transport
8-
client = Client(transport=transport, fetch_schema_from_transport=True)
8+
client = Client(transport=transport)
99

1010
# Provide a GraphQL query
1111
query = gql(

docs/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ Contents
99

1010
intro
1111
usage/index
12-
async/index
1312
transports/index
1413
advanced/index
1514
gql-cli/intro

docs/async/async_usage.rst renamed to docs/usage/async_usage.rst

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
.. _async_usage:
22

3-
Async Usage
3+
Async usage
44
===========
55

6+
On previous versions of GQL, the code was `sync` only , it means that when you ran
7+
`execute` on the Client, you could do nothing else in the current Thread and had to wait for
8+
an answer or a timeout from the backend to continue. The only http library was `requests`, allowing only sync usage.
9+
10+
From the version 3 of GQL, we support `sync` and `async` :ref:`transports <transports>` using `asyncio`_.
11+
12+
With the :ref:`async transports <async_transports>`, there is now the possibility to execute GraphQL requests
13+
asynchronously, :ref:`allowing to execute multiple requests in parallel if needed <async_advanced_usage>`.
14+
15+
If you don't care or need async functionality, it is still possible, with :ref:`async transports <async_transports>`,
16+
to run the `execute` or `subscribe` methods directly from the Client
17+
(as described in the :ref:`Sync Usage <sync_usage>` example) and GQL will execute the request
18+
in a synchronous manner by running an asyncio event loop itself.
19+
20+
This won't work though if you already have an asyncio event loop running. In that case you should use the async
21+
methods.
22+
23+
Example
24+
-------
25+
626
If you use an :ref:`async transport <async_transports>`, you can use GQL asynchronously using `asyncio`_.
727

828
* put your code in an asyncio coroutine (method starting with :code:`async def`)
929
* use :code:`async with client as session:` to connect to the backend and provide a session instance
1030
* use the :code:`await` keyword to execute requests: :code:`await session.execute(...)`
1131
* then run your coroutine in an asyncio event loop by running :code:`asyncio.run`
1232

13-
Example:
14-
1533
.. literalinclude:: ../code_examples/aiohttp_async.py
1634

1735
IPython

docs/usage/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Usage
44
.. toctree::
55
:maxdepth: 2
66

7-
basic_usage
7+
sync_usage
8+
async_usage
89
validation
910
subscriptions
1011
variables

docs/usage/subscriptions.rst

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,76 @@
11
Subscriptions
22
=============
33

4-
Using the :ref:`websockets transport <websockets_transport>`, it is possible to execute GraphQL subscriptions:
4+
Using the :ref:`websockets transport <websockets_transport>`, it is possible to execute GraphQL subscriptions,
5+
either using the sync or async usage.
6+
7+
The async usage is recommended for any non-trivial tasks (it allows efficient concurrent queries and subscriptions).
8+
9+
See :ref:`Async permanent session <async_permanent_session>` and :ref:`Async advanced usage <async_advanced_usage>`
10+
for more advanced examples.
11+
12+
.. note::
13+
14+
The websockets transport can also execute queries or mutations, it is not restricted to subscriptions.
15+
16+
Sync
17+
----
518

619
.. code-block:: python
720
8-
from gql import gql, Client
21+
from gql import Client, gql
922
from gql.transport.websockets import WebsocketsTransport
1023
24+
# Select your transport with a defined url endpoint
1125
transport = WebsocketsTransport(url='wss://your_server/graphql')
1226
13-
client = Client(
14-
transport=transport,
15-
fetch_schema_from_transport=True,
16-
)
27+
# Create a GraphQL client using the defined transport
28+
client = Client(transport=transport)
1729
30+
# Provide a GraphQL subscription query
1831
query = gql('''
1932
subscription yourSubscription {
2033
...
2134
}
2235
''')
2336
37+
# Connect and subscribe to the results using a simple 'for'
2438
for result in client.subscribe(query):
2539
print (result)
2640
27-
.. note::
41+
Async
42+
-----
43+
44+
.. code-block:: python
45+
46+
import asyncio
47+
48+
from gql import Client, gql
49+
from gql.transport.websockets import WebsocketsTransport
50+
51+
52+
async def main():
53+
54+
# Select your transport with a defined url endpoint
55+
transport = WebsocketsTransport(url='wss://your_server/graphql')
56+
57+
# Create a GraphQL client using the defined transport
58+
client = Client(transport=transport)
59+
60+
# Provide a GraphQL subscription query
61+
query = gql('''
62+
subscription yourSubscription {
63+
...
64+
}
65+
''')
66+
67+
# Using `async with` on the client will start a connection on the transport
68+
# and provide a `session` variable to execute queries on this connection
69+
async with client as session:
70+
71+
# Then get the results using 'async for'
72+
async for result in client.subscribe(query):
73+
print (result)
74+
2875
29-
The websockets transport can also execute queries or mutations, it is not restricted to subscriptions
76+
asyncio.run(main())

docs/usage/basic_usage.rst renamed to docs/usage/sync_usage.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
.. _basic_usage:
1+
.. _sync_usage:
22

3-
Basic usage
4-
-----------
3+
Sync usage
4+
==========
55

6-
In order to execute a GraphQL request against a GraphQL API:
6+
To execute a GraphQL request against a GraphQL API:
77

88
* create your gql :ref:`transport <transports>` in order to choose the destination url
99
and the protocol used to communicate with it
@@ -18,4 +18,3 @@ In order to execute a GraphQL request against a GraphQL API:
1818
Please note that this basic example won't work if you have an asyncio event loop running. In some
1919
python environments (as with Jupyter which uses IPython) an asyncio event loop is created for you.
2020
In that case you should use instead the :ref:`Async Usage example<async_usage>`.
21-

0 commit comments

Comments
 (0)