1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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 = gql("""
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.
|