|
| 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. |
0 commit comments