Skip to content

Commit 35ce9d7

Browse files
committed
Use defaultdict in collect_fields
Note that a defaultdict(list) has the same functionality as the newly introduced AccumulatorMap in GraphQL-js. Replicates graphql/graphql-js@e59ae4c
1 parent 2d8699c commit 35ce9d7

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/graphql/execution/collect_fields.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections import defaultdict
12
from typing import Any, Dict, List, Set, Union, cast
23

34
from ..language import (
@@ -39,7 +40,7 @@ def collect_fields(
3940
4041
For internal use only.
4142
"""
42-
fields: Dict[str, List[FieldNode]] = {}
43+
fields: Dict[str, List[FieldNode]] = defaultdict(list)
4344
collect_fields_impl(
4445
schema, fragments, variable_values, runtime_type, selection_set, fields, set()
4546
)
@@ -64,7 +65,7 @@ def collect_sub_fields(
6465
6566
For internal use only.
6667
"""
67-
sub_field_nodes: Dict[str, List[FieldNode]] = {}
68+
sub_field_nodes: Dict[str, List[FieldNode]] = defaultdict(list)
6869
visited_fragment_names: Set[str] = set()
6970
for node in field_nodes:
7071
if node.selection_set:
@@ -94,8 +95,7 @@ def collect_fields_impl(
9495
if isinstance(selection, FieldNode):
9596
if not should_include_node(variable_values, selection):
9697
continue
97-
name = get_field_entry_key(selection)
98-
fields.setdefault(name, []).append(selection)
98+
fields[get_field_entry_key(selection)].append(selection)
9999
elif isinstance(selection, InlineFragmentNode):
100100
if not should_include_node(
101101
variable_values, selection

0 commit comments

Comments
 (0)