|
| 1 | +"""GraphQL module.""" |
| 2 | + |
| 3 | +from typing import Any, Dict, Generator |
| 4 | + |
| 5 | +from kili.core.graphql.graphql_client import GraphQLClient |
| 6 | +from kili.domain.event import QueryOptions |
| 7 | + |
| 8 | + |
| 9 | +class PaginatedGraphQLQuery: |
| 10 | + """Query class for querying Kili objects. |
| 11 | +
|
| 12 | + It factorizes code for executing paginated queries. |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, graphql_client: GraphQLClient) -> None: |
| 16 | + """Initialize the paginator.""" |
| 17 | + self._graphql_client = graphql_client |
| 18 | + |
| 19 | + # pylint: disable=too-many-arguments |
| 20 | + def execute_query_from_paginated_call( |
| 21 | + self, |
| 22 | + query: str, |
| 23 | + where: Dict[str, Any], |
| 24 | + pagination: Dict[str, Any], |
| 25 | + options: QueryOptions, |
| 26 | + ) -> Generator[Dict, None, None]: |
| 27 | + """Build a row generator from paginated query calls with the first and skip pattern. |
| 28 | +
|
| 29 | + Args: |
| 30 | + query: The object query to execute and to send to graphQL, in string format |
| 31 | + where: The where payload to send in the graphQL query |
| 32 | + pagination: The where pagination payload to send in the graphQL query |
| 33 | + options: The query options with skip and first and disable_tqdm |
| 34 | + """ |
| 35 | + count_elements_retrieved = 0 |
| 36 | + while True: |
| 37 | + skip = count_elements_retrieved + options.skip |
| 38 | + first = options.batch_size |
| 39 | + order = options.order |
| 40 | + |
| 41 | + payload = { |
| 42 | + "where": where, |
| 43 | + "pagination": {"skip": skip, "first": first, **pagination}, |
| 44 | + "order": order, |
| 45 | + } |
| 46 | + elements = self._graphql_client.execute(query, payload)["data"] |
| 47 | + if not isinstance(elements, list): |
| 48 | + raise TypeError( |
| 49 | + "PaginatedGraphQLQuery only support operations returning a list of objects" |
| 50 | + ) |
| 51 | + |
| 52 | + if len(elements) == 0: |
| 53 | + break |
| 54 | + |
| 55 | + yield from elements |
| 56 | + |
| 57 | + count_elements_retrieved += len(elements) |
| 58 | + |
| 59 | + if len(elements) < first: |
| 60 | + break |
0 commit comments