Skip to content

Commit ad1b7c7

Browse files
committed
fields_on_correct_type: improve type coverage
Replicates graphql/graphql-js@4589229
1 parent 7a72e39 commit ad1b7c7

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/graphql/validation/rules/fields_on_correct_type.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from collections import defaultdict
22
from functools import cmp_to_key
3-
from typing import Dict, List, cast
3+
from typing import Dict, List, Set, Union, cast
44

55
from ...type import (
66
GraphQLAbstractType,
7+
GraphQLInterfaceType,
8+
GraphQLObjectType,
79
GraphQLOutputType,
810
GraphQLSchema,
911
is_abstract_type,
@@ -70,7 +72,7 @@ def get_suggested_type_names(
7072
return []
7173

7274
type_ = cast(GraphQLAbstractType, type_)
73-
suggested_types = set()
75+
suggested_types: Set[Union[GraphQLObjectType, GraphQLInterfaceType]] = set()
7476
usage_count: Dict[str, int] = defaultdict(int)
7577
for possible_type in schema.get_possible_types(type_):
7678
if field_name not in possible_type.fields:
@@ -88,23 +90,29 @@ def get_suggested_type_names(
8890
suggested_types.add(possible_interface)
8991
usage_count[possible_interface.name] += 1
9092

91-
def cmp(type_a, type_b) -> int:
93+
def cmp(
94+
type_a: Union[GraphQLObjectType, GraphQLInterfaceType],
95+
type_b: Union[GraphQLObjectType, GraphQLInterfaceType],
96+
) -> int: # pragma: no cover
9297
# Suggest both interface and object types based on how common they are.
9398
usage_count_diff = usage_count[type_b.name] - usage_count[type_a.name]
9499
if usage_count_diff:
95100
return usage_count_diff
96101

97102
# Suggest super types first followed by subtypes
98-
if is_abstract_type(type_a) and schema.is_sub_type(type_a, type_b):
103+
if is_interface_type(type_a) and schema.is_sub_type(
104+
cast(GraphQLInterfaceType, type_a), type_b
105+
):
99106
return -1
100-
if is_abstract_type(type_b) and schema.is_sub_type(type_b, type_a):
107+
if is_interface_type(type_b) and schema.is_sub_type(
108+
cast(GraphQLInterfaceType, type_b), type_a
109+
):
101110
return 1
102111

103112
if type_a.name > type_b.name:
104113
return 1
105-
elif type_a.name < type_b.name:
114+
if type_a.name < type_b.name:
106115
return -1
107-
108116
return 0
109117

110118
return [type_.name for type_ in sorted(suggested_types, key=cmp_to_key(cmp))]

0 commit comments

Comments
 (0)