10000 Implementation of automatic batching for async by leszekhanusz · Pull Request #554 · graphql-python/gql · GitHub
[go: up one dir, main page]

Skip to content

Implementation of automatic batching for async #554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Addind Batching docs
  • Loading branch information
leszekhanusz committed May 27, 2025
commit 93274eadc8f6fe36238cc95628890ef64d5fb782
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ The complete documentation for GQL can be found at
* 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)
* Supports [File uploads](https://gql.readthedocs.io/en/latest/usage/file_upload.html)
* Supports [Custom scalars / Enums](https://gql.readthedocs.io/en/latest/usage/custom_scalars_and_enums.html)
* Supports [Batching requests](https://gql.readthedocs.io/en/latest/advanced/batching_requests.html)
* [gql-cli script](https://gql.readthedocs.io/en/latest/gql-cli/intro.html) to execute GraphQL queries or download schemas from the command line
* [DSL module](https://gql.readthedocs.io/en/latest/advanced/dsl_module.html) to compose GraphQL queries dynamically

Expand Down
96 changes: 96 additions & 0 deletions docs/advanced/batching_requests.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
.. _batching_requests:

Batching requests
=================

If you need to send multiple GraphQL queries to a backend,
and if the backend supports batch requests,
then you might want to send those requests in a batch instead of
making multiple execution requests.

.. warning::
- Some backends do not support batch requests
- File uploads and subscriptions are not supported with batch requests

Batching requests manually
^^^^^^^^^^^^^^^^^^^^^^^^^^

To execute a batch of requests manually:

- First Make a list of :class:`GraphQLRequest <gql.GraphQLRequest>` objects, containing:
* your GraphQL query
* Optional variable_values
* Optional operation_name

.. code-block:: python

request1 = GraphQLRequest("""
query getContinents {
continents {
code
name
}
}
"""
)

request2 = GraphQLRequest("""
query getContinentName ($code: ID!) {
continent (code: $code) {
name
}
}
""",
variable_values={
"code": "AF",
},
)

requests = [request1, request2]

- Then use one of the `execute_batch` methods, either on Client,
or in a sync or async session

**Sync**:

.. code-block:: python

transport = RequestsHTTPTransport(url=url)
# Or transport = HTTPXTransport(url=url)

with Client(transport=transport) as session:

results = session.execute_batch(requests)

result1 = results[0]
result2 = results[1]

**Async**:

.. code-block:: python

transport = AIOHTTPTransport(url=url)
# Or transport = HTTPXAsyncTransport(url=url)

async with Client(transport=transport) as session:

results = await session.execute_batch(requests)

result1 = results[0]
result2 = results[1]

.. note::
If any request in the batch returns an error, then a TransportQueryError will be raised
with the first error found.

Automatic Batching of requests
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If your code execute multiple requests independently in a short time
(either from different threads in sync code, or from different asyncio tasks in async code),
then you can use gql automatic batching of request functionality.

You define a :code:`batching_interval` in your :class:`Client <gql.Client>`
and each time a new execution request is received through an `execute` method,
we will wait that interval (in seconds) for other requests to arrive
before sending all the requests received in that interval in a single batch.
1 change: 1 addition & 0 deletions docs/advanced/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Advanced

async_advanced_usage
async_permanent_session
batching_requests
logging
error_handling
local_schema
Expand Down
0