|
1 | | -from typing import Dict, List, Optional, Sequence, Tuple |
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Sequence |
2 | 4 |
|
3 | 5 | import graphql |
4 | 6 | from graphql import is_equal_type |
5 | 7 |
|
6 | 8 | from .strategy import make_type_name |
7 | 9 |
|
8 | 10 | # (field_name, on_type, children, arguments) |
9 | | -SelectionNode = Tuple[str, Optional[str], "List[SelectionNode]", List[graphql.ArgumentNode]] |
| 11 | +SelectionNode = tuple[str, str | None, "list[SelectionNode]", list[graphql.ArgumentNode]] |
10 | 12 |
|
11 | 13 |
|
12 | 14 | def _field_node( |
13 | | - name: str, children: Sequence[graphql.SelectionNode], args: List[graphql.ArgumentNode] |
| 15 | + name: str, children: Sequence[graphql.SelectionNode], args: list[graphql.ArgumentNode] |
14 | 16 | ) -> graphql.FieldNode: |
15 | 17 | selection_set = graphql.SelectionSetNode(selections=tuple(children)) if children else None |
16 | 18 | return graphql.FieldNode(name=graphql.NameNode(value=name), arguments=tuple(args), selection_set=selection_set) |
17 | 19 |
|
18 | 20 |
|
19 | 21 | def build_selection_set( |
20 | | - nodes: "List[SelectionNode]", type_map: Optional[Dict[str, graphql.GraphQLNamedType]] = None |
| 22 | + nodes: list[SelectionNode], type_map: dict[str, graphql.GraphQLNamedType] | None = None |
21 | 23 | ) -> graphql.SelectionSetNode: |
22 | | - selections: List[graphql.SelectionNode] = [] |
| 24 | + selections: list[graphql.SelectionNode] = [] |
23 | 25 | for name, on, children, args in nodes: |
24 | 26 | if on is not None: |
25 | 27 | continue |
26 | 28 | child = list(build_selection_set(children, type_map).selections) if children else [] |
27 | 29 | selections.append(_field_node(name, child, args)) |
28 | 30 |
|
29 | | - seen: Dict[str, graphql.GraphQLType] = {} |
30 | | - by_type: Dict[str, List[SelectionNode]] = {} |
| 31 | + seen: dict[str, graphql.GraphQLType] = {} |
| 32 | + by_type: dict[str, list[SelectionNode]] = {} |
31 | 33 | for node in nodes: |
32 | 34 | if node[1] is not None: |
33 | 35 | by_type.setdefault(node[1], []).append(node) |
34 | 36 | for on_type in sorted(by_type): |
35 | 37 | assert type_map is not None |
36 | 38 | fragment_type = type_map[on_type] |
37 | | - frag_fields: List[graphql.SelectionNode] = [] |
| 39 | + frag_fields: list[graphql.SelectionNode] = [] |
38 | 40 | for name, _on_type, children, args in by_type[on_type]: |
39 | 41 | field_type = fragment_type.fields[name].type |
40 | 42 | child = list(build_selection_set(children, type_map).selections) if children else [] |
|
0 commit comments