Skip to content

Commit 93274ea

Browse files
committed
Addind Batching docs
1 parent 1480d3b commit 93274ea

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ The complete documentation for GQL can be found at
4040
* 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)
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)
43+
* Supports [Batching requests](https://gql.readthedocs.io/en/latest/advanced/batching_requests.html)
4344
* [gql-cli script](https://gql.readthedocs.io/en/latest/gql-cli/intro.html) to execute GraphQL queries or download schemas from the command line
4445
* [DSL module](https://gql.readthedocs.io/en/latest/advanced/dsl_module.html) to compose GraphQL queries dynamically
4546

docs/advanced/batching_requests.rst

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
.. _batching_requests:
2+
3+
Batching requests
4+
=================
5+
6+
If you need to send multiple GraphQL queries to a backend,
7+
and if the backend supports batch requests,
8+
then you might want to send those requests in a batch instead of
9+
making multiple execution requests.
10+
11+
.. warning::
12+
- Some backends do not support batch requests
13+
- File uploads and subscriptions are not supported with batch requests
14+
15+
Batching requests manually
16+
^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
18+
To execute a batch of requests manually:
19+
20+
- First Make a list of :class:`GraphQLRequest <gql.GraphQLRequest>` objects, containing:
21+
* your GraphQL query
22+
* Optional variable_values
23+
* Optional operation_name
24+
25+
.. code-block:: python
26+
27+
request1 = GraphQLRequest("""
28+
query getContinents {
29+
continents {
30+
code
31+
name
32+
}
33+
}
34+
"""
35+
)
36+
37+
request2 = GraphQLRequest("""
38+
query getContinentName ($code: ID!) {
39+
continent (code: $code) {
40+
name
41+
}
42+
}
43+
""",
44+
variable_values={
45+
"code": "AF",
46+
},
47+
)
48+
49+
requests = [request1, request2]
50+
51+
- Then use one of the `execute_batch` methods, either on Client,
52+
or in a sync or async session
53+
54+
**Sync**:
55+
56+
.. code-block:: python
57+
58+
transport = RequestsHTTPTransport(url=url)
59+
# Or transport = HTTPXTransport(url=url)
60+
61+
with Client(transport=transport) as session:
62+
63+
results = session.execute_batch(requests)
64+
65+
result1 = results[0]
66+
result2 = results[1]
67+
68+
**Async**:
69+
70+
.. code-block:: python
71+
72+
transport = AIOHTTPTransport(url=url)
73+
# Or transport = HTTPXAsyncTransport(url=url)
74+
75+
async with Client(transport=transport) as session:
76+
77+
results = await session.execute_batch(requests)
78+
79+
result1 = results[0]
80+
result2 = results[1]
81+
82+
.. note::
83+
If any request in the batch returns an error, then a TransportQueryError will be raised
84+
with the first error found.
85+
86+
Automatic Batching of requests
87+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
88+
89+
If your code execute multiple requests independently in a short time
90+
(either from different threads in sync code, or from different asyncio tasks in async code),
91+
then you can use gql automatic batching of request functionality.
92+
93+
You define a :code:`batching_interval` in your :class:`Client <gql.Client>`
94+
and each time a new execution request is received through an `execute` method,
95+
we will wait that interval (in seconds) for other requests to arrive
96+
before sending all the requests received in that interval in a single batch.

docs/advanced/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Advanced
66

77
async_advanced_usage
88
async_permanent_session
9+
batching_requests
910
logging
1011
error_handling
1112
local_schema

0 commit comments

Comments
 (0)