Skip to content

Commit dba4953

Browse files
authored
Documentation improvements (#561)
1 parent a90f923 commit dba4953

File tree

13 files changed

+158
-78
lines changed

13 files changed

+158
-78
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ assignees: ''
99

1010
**Common problems**
1111
- If you receive a TransportQueryError, it means the error is coming from the backend (See [Error Handling](https://gql.readthedocs.io/en/latest/advanced/error_handling.html)) and has probably nothing to do with gql
12-
- If you use IPython (Jupyter, Spyder), then [you need to use the async version](https://gql.readthedocs.io/en/latest/async/async_usage.html#ipython)
12+
- If you use IPython (Jupyter, Spyder), then [you need to use the async version](https://gql.readthedocs.io/en/latest/usage/async_usage.html#ipython)
1313
- Before sending a bug report, please consider [activating debug logs](https://gql.readthedocs.io/en/latest/advanced/logging.html) to see the messages exchanged between the client and the backend
1414

1515
**Describe the bug**

README.md

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# GQL
22

3-
This is a GraphQL client for Python 3.8+.
4-
Plays nicely with `graphene`, `graphql-core`, `graphql-js` and any other GraphQL implementation compatible with the spec.
3+
This is a GraphQL client for Python.
4+
Plays nicely with `graphene`, `graphql-core`, `graphql-js` and any other GraphQL implementation
5+
compatible with the [GraphQL specification](https://spec.graphql.org).
56

67
GQL architecture is inspired by `React-Relay` and `Apollo-Client`.
78

@@ -37,7 +38,7 @@ The complete documentation for GQL can be found at
3738
* AWS AppSync realtime protocol (experimental)
3839
* 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
3940
* 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)
41+
* 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)
4142
* Supports [File uploads](https://gql.readthedocs.io/en/latest/usage/file_upload.html)
4243
* Supports [Custom scalars / Enums](https://gql.readthedocs.io/en/latest/usage/custom_scalars_and_enums.html)
4344
* Supports [Batching requests](https://gql.readthedocs.io/en/latest/advanced/batching_requests.html)
@@ -57,17 +58,17 @@ pip install "gql[all]"
5758
5859
## Usage
5960

60-
### Basic usage
61+
### Sync usage
6162

6263
```python
63-
from gql import gql, Client
64+
from gql import Client, gql
6465
from gql.transport.aiohttp import AIOHTTPTransport
6566

6667
# Select your transport with a defined url endpoint
6768
transport = AIOHTTPTransport(url="https://countries.trevorblades.com/")
6869

6970
# Create a GraphQL client using the defined transport
70-
client = Client(transport=transport, fetch_schema_from_transport=True)
71+
client = Client(transport=transport)
7172

7273
# Provide a GraphQL query
7374
query = gql(
@@ -95,7 +96,48 @@ $ python basic_example.py
9596

9697
> **WARNING**: Please note that this basic example won't work if you have an asyncio event loop running. In some
9798
> python environments (as with Jupyter which uses IPython) an asyncio event loop is created for you. In that case you
98-
> should use instead the [async usage example](https://gql.readthedocs.io/en/latest/async/async_usage.html#async-usage).
99+
> should use instead the [async usage example](https://gql.readthedocs.io/en/latest/usage/async_usage.html#async-usage).
100+
101+
### Async usage
102+
103+
```python
104+
import asyncio
105+
106+
from gql import Client, gql
107+
from gql.transport.aiohttp import AIOHTTPTransport
108+
109+
110+
async def main():
111+
112+
# Select your transport with a defined url endpoint
113+
transport = AIOHTTPTransport(url="https://countries.trevorblades.com/graphql")
114+
115+
# Create a GraphQL client using the defined transport
116+
client = Client(transport=transport)
117+
118+
# Provide a GraphQL query
119+
query = gql(
120+
"""
121+
query getContinents {
122+
continents {
123+
code
124+
name
125+
}
126+
}
127+
"""
128+
)
129+
130+
# Using `async with` on the client will start a connection on the transport
131+
# and provide a `session` variable to execute queries on this connection
132+
async with client as session:
133+
134+
# Execute the query
135+
result = await session.execute(query)
136+
print(result)
137+
138+
139+
asyncio.run(main())
140+
```
99141

100142
## Contributing
101143
See [CONTRIBUTING.md](CONTRIBUTING.md)

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/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
# -- Project information -----------------------------------------------------
1919

20-
project = 'gql 3'
21-
copyright = '2020, graphql-python.org'
20+
project = 'gql'
21+
copyright = '2025, graphql-python.org'
2222
author = 'graphql-python.org'
2323

2424
# The full version, including alpha/beta/rc tags

docs/index.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Welcome to GQL 3 documentation!
2-
===============================
1+
GQL documentation
2+
=================
33

44
Contents
55
--------
@@ -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/intro.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Introduction
22
============
33

4-
`GQL 3`_ is a `GraphQL`_ Client for Python 3.8+ which plays nicely with other
4+
`GQL`_ is a `GraphQL`_ Client for Python which plays nicely with other
55
graphql implementations compatible with the spec.
66

77
Under the hood, it uses `GraphQL-core`_ which is a Python port of `GraphQL.js`_,
@@ -10,7 +10,7 @@ the JavaScript reference implementation for GraphQL.
1010
Installation
1111
------------
1212

13-
You can install GQL 3 and all the extra dependencies using pip_::
13+
You can install GQL and all the extra dependencies using pip_::
1414

1515
pip install "gql[all]"
1616

@@ -93,7 +93,7 @@ Please check the `Contributing`_ file to learn how to make a good pull request.
9393
.. _GraphQL: https://graphql.org/
9494
.. _GraphQL-core: https://github.com/graphql-python/graphql-core
9595
.. _GraphQL.js: https://github.com/graphql/graphql-js
96-
.. _GQL 3: https://github.com/graphql-python/gql
96+
.. _GQL: https://github.com/graphql-python/gql
9797
.. _pip: https://pip.pypa.io/
9898
.. _GitHub repository for gql: https://github.com/graphql-python/gql
9999
.. _Contributing: https://github.com/graphql-python/gql/blob/master/CONTRIBUTING.md

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

0 commit comments

Comments
 (0)