|
| 1 | +from graphql_relay.connection.arrayconnection import ( |
| 2 | + get_offset_with_default, |
| 3 | + offset_to_cursor, |
| 4 | +) |
| 5 | +from graphql_relay.connection.connectiontypes import Connection, Edge, PageInfo |
| 6 | + |
| 7 | + |
| 8 | +def connection_from_list(data, args=None, **kwargs): |
| 9 | + """ |
| 10 | + Replace graphql_relay.connection.arrayconnection.connection_from_list. |
| 11 | +
|
| 12 | + This can be removed, when (or better if) |
| 13 | + https://github.com/graphql-python/graphql-relay-py/issues/12 |
| 14 | + is resolved. |
| 15 | +
|
| 16 | + A simple function that accepts an array and connection arguments, and returns |
| 17 | + a connection object for use in GraphQL. It uses array offsets as pagination, |
| 18 | + so pagination will only work if the array is static. |
| 19 | + """ |
| 20 | + _len = len(data) |
| 21 | + return connection_from_list_slice( |
| 22 | + data, args, slice_start=0, list_length=_len, list_slice_length=_len, **kwargs |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +def connection_from_list_slice( |
| 27 | + list_slice, |
| 28 | + args=None, |
| 29 | + connection_type=None, |
| 30 | + edge_type=None, |
| 31 | + pageinfo_type=None, |
| 32 | + slice_start=0, |
| 33 | + list_length=0, |
| 34 | + list_slice_length=None, |
| 35 | +): |
| 36 | + """ |
| 37 | + Replace graphql_relay.connection.arrayconnection.connection_from_list_slice. |
| 38 | +
|
| 39 | + This can be removed, when (or better if) |
| 40 | + https://github.com/graphql-python/graphql-relay-py/issues/12 |
| 41 | + is resolved. |
| 42 | +
|
| 43 | + Given a slice (subset) of an array, returns a connection object for use in |
| 44 | + GraphQL. |
| 45 | + This function is similar to `connectionFromArray`, but is intended for use |
| 46 | + cases where you know the cardinality of the connection, consider it too large |
| 47 | + to materialize the entire array, and instead wish pass in a slice of the |
| 48 | + total result large enough to cover the range specified in `args`. |
| 49 | + """ |
| 50 | + connection_type = connection_type or Connection |
| 51 | + edge_type = edge_type or Edge |
| 52 | + pageinfo_type = pageinfo_type or PageInfo |
| 53 | + |
| 54 | + args = args or {} |
| 55 | + |
| 56 | + before = args.get("before") |
| 57 | + after = args.get("after") |
| 58 | + first = args.get("first") |
| 59 | + last = args.get("last") |
| 60 | + if list_slice_length is None: # pragma: no cover |
| 61 | + list_slice_length = len(list_slice) |
| 62 | + slice_end = slice_start + list_slice_length |
| 63 | + before_offset = get_offset_with_default(before, list_length) |
| 64 | + after_offset = get_offset_with_default(after, -1) |
| 65 | + |
| 66 | + start_offset = max(slice_start - 1, after_offset, -1) + 1 |
| 67 | + end_offset = min(slice_end, before_offset, list_length) |
| 68 | + if isinstance(first, int): |
| 69 | + end_offset = min(end_offset, start_offset + first) |
| 70 | + if isinstance(last, int): |
| 71 | + start_offset = max(start_offset, end_offset - last) |
| 72 | + |
| 73 | + # If supplied slice is too large, trim it down before mapping over it. |
| 74 | + _slice = list_slice[ |
| 75 | + max(start_offset - slice_start, 0) : list_slice_length |
| 76 | + - (slice_end - end_offset) |
| 77 | + ] |
| 78 | + edges = [ |
| 79 | + edge_type(node=node, cursor=offset_to_cursor(start_offset + i)) |
| 80 | + for i, node in enumerate(_slice) |
| 81 | + ] |
| 82 | + |
| 83 | + first_edge_cursor = edges[0].cursor if edges else None |
| 84 | + last_edge_cursor = edges[-1].cursor if edges else None |
| 85 | + |
| 86 | + return connection_type( |
| 87 | + edges=edges, |
| 88 | + page_info=pageinfo_type( |
| 89 | + start_cursor=first_edge_cursor, |
| 90 | + end_cursor=last_edge_cursor, |
| 91 | + has_previous_page=start_offset > 0, |
| 92 | + has_next_page=end_offset < list_length, |
| 93 | + ), |
| 94 | + ) |
0 commit comments